clang-tools  10.0.0svn
SyncAPI.cpp
Go to the documentation of this file.
1 //===--- SyncAPI.cpp - Sync version of ClangdServer's API --------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SyncAPI.h"
10 #include "index/Index.h"
11 
12 namespace clang {
13 namespace clangd {
14 
16  llvm::StringRef Contents, WantDiagnostics WantDiags) {
17  Server.addDocument(File, Contents, WantDiags);
18  if (!Server.blockUntilIdleForTest())
19  llvm_unreachable("not idle after addDocument");
20 }
21 
22 namespace {
23 /// A helper that waits for async callbacks to fire and exposes their result in
24 /// the output variable. Intended to be used in the following way:
25 /// T Result;
26 /// someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result));
27 template <typename T> struct CaptureProxy {
28  CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) {
29  assert(!Target.hasValue());
30  }
31 
32  CaptureProxy(const CaptureProxy &) = delete;
33  CaptureProxy &operator=(const CaptureProxy &) = delete;
34  // We need move ctor to return a value from the 'capture' helper.
35  CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) {
36  Other.Target = nullptr;
37  }
38  CaptureProxy &operator=(CaptureProxy &&) = delete;
39 
40  operator llvm::unique_function<void(T)>() && {
41  assert(!Future.valid() && "conversion to callback called multiple times");
42  Future = Promise.get_future();
43  return [Promise = std::move(Promise)](T Value) mutable {
44  Promise.set_value(std::make_shared<T>(std::move(Value)));
45  };
46  }
47 
48  ~CaptureProxy() {
49  if (!Target)
50  return;
51  assert(Future.valid() && "conversion to callback was not called");
52  assert(!Target->hasValue());
53  Target->emplace(std::move(*Future.get()));
54  }
55 
56 private:
57  llvm::Optional<T> *Target;
58  // Using shared_ptr to workaround compilation errors with MSVC.
59  // MSVC only allows default-construcitble and copyable objects as future<>
60  // arguments.
61  std::promise<std::shared_ptr<T>> Promise;
62  std::future<std::shared_ptr<T>> Future;
63 };
64 
65 template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) {
66  return CaptureProxy<T>(Target);
67 }
68 } // namespace
69 
70 llvm::Expected<CodeCompleteResult>
73  llvm::Optional<llvm::Expected<CodeCompleteResult>> Result;
74  Server.codeComplete(File, Pos, Opts, capture(Result));
75  return std::move(*Result);
76 }
77 
78 llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server,
79  PathRef File, Position Pos) {
80  llvm::Optional<llvm::Expected<SignatureHelp>> Result;
81  Server.signatureHelp(File, Pos, capture(Result));
82  return std::move(*Result);
83 }
84 
85 llvm::Expected<std::vector<LocatedSymbol>>
87  llvm::Optional<llvm::Expected<std::vector<LocatedSymbol>>> Result;
88  Server.locateSymbolAt(File, Pos, capture(Result));
89  return std::move(*Result);
90 }
91 
92 llvm::Expected<std::vector<DocumentHighlight>>
94  llvm::Optional<llvm::Expected<std::vector<DocumentHighlight>>> Result;
95  Server.findDocumentHighlights(File, Pos, capture(Result));
96  return std::move(*Result);
97 }
98 
99 llvm::Expected<std::vector<TextEdit>> runRename(ClangdServer &Server,
100  PathRef File, Position Pos,
101  llvm::StringRef NewName) {
102  llvm::Optional<llvm::Expected<std::vector<TextEdit>>> Result;
103  Server.rename(File, Pos, NewName, /*WantFormat=*/true, capture(Result));
104  return std::move(*Result);
105 }
106 
108  llvm::Optional<std::string> Result;
109  Server.dumpAST(File, capture(Result));
110  return std::move(*Result);
111 }
112 
113 llvm::Expected<std::vector<SymbolInformation>>
114 runWorkspaceSymbols(ClangdServer &Server, llvm::StringRef Query, int Limit) {
115  llvm::Optional<llvm::Expected<std::vector<SymbolInformation>>> Result;
116  Server.workspaceSymbols(Query, Limit, capture(Result));
117  return std::move(*Result);
118 }
119 
120 llvm::Expected<std::vector<DocumentSymbol>>
122  llvm::Optional<llvm::Expected<std::vector<DocumentSymbol>>> Result;
123  Server.documentSymbols(File, capture(Result));
124  return std::move(*Result);
125 }
126 
127 SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
128  FuzzyFindRequest Req;
129  Req.Query = Query;
130  Req.AnyScope = true;
131  return runFuzzyFind(Index, Req);
132 }
133 
136  Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
137  return std::move(Builder).build();
138 }
139 
141  RefsRequest Req;
142  Req.IDs = {ID};
143  RefSlab::Builder Slab;
144  Index.refs(Req, [&](const Ref &S) { Slab.insert(ID, S); });
145  return std::move(Slab).build();
146 }
147 
148 llvm::Expected<std::vector<Range>>
150  llvm::Optional<llvm::Expected<std::vector<Range>>> Result;
151  Server.semanticRanges(File, Pos, capture(Result));
152  return std::move(*Result);
153 }
154 
155 llvm::Expected<llvm::Optional<clangd::Path>>
157  llvm::Optional<llvm::Expected<llvm::Optional<clangd::Path>>> Result;
158  Server.switchSourceHeader(File, capture(Result));
159  return std::move(*Result);
160 }
161 
162 } // namespace clangd
163 } // namespace clang
int Limit
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:47
void rename(PathRef File, Position Pos, llvm::StringRef NewName, bool WantFormat, Callback< std::vector< TextEdit >> CB)
Rename all occurrences of the symbol at the Pos in File to NewName.
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:68
void addDocument(PathRef File, StringRef Contents, WantDiagnostics WD=WantDiagnostics::Auto)
Add a File to the list of tracked C++ files or update the contents if File is already tracked...
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
llvm::Expected< std::vector< Range > > runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:149
llvm::StringRef Contents
void codeComplete(PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, Callback< CodeCompleteResult > CB)
Run code completion for File at Pos.
llvm::Expected< CodeCompleteResult > runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, clangd::CodeCompleteOptions Opts)
Definition: SyncAPI.cpp:71
std::string runDumpAST(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:107
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
llvm::Expected< SignatureHelp > runSignatureHelp(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:78
llvm::Expected< std::vector< DocumentSymbol > > runDocumentSymbols(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:121
llvm::Expected< std::vector< LocatedSymbol > > runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:86
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning...
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
LLVM_NODISCARD bool blockUntilIdleForTest(llvm::Optional< double > TimeoutSeconds=10)
void findDocumentHighlights(PathRef File, Position Pos, Callback< std::vector< DocumentHighlight >> CB)
Get document highlights for a given position.
llvm::Expected< llvm::Optional< clangd::Path > > runSwitchHeaderSource(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:156
SymbolSlab::Builder is a mutable container that can &#39;freeze&#39; to SymbolSlab.
Definition: Symbol.h:199
SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query)
Definition: SyncAPI.cpp:127
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
virtual void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const =0
Finds all occurrences (e.g.
void switchSourceHeader(PathRef Path, Callback< llvm::Optional< clangd::Path >> CB)
Switch to a corresponding source file when given a header file, and vice versa.
void dumpAST(PathRef File, llvm::unique_function< void(std::string)> Callback)
Only for testing purposes.
void documentSymbols(StringRef File, Callback< std::vector< DocumentSymbol >> CB)
Retrieve the symbols within the specified file.
RefSlab::Builder is a mutable container that can &#39;freeze&#39; to RefSlab.
Definition: Ref.h:93
CodeCompletionBuilder Builder
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Expected< std::vector< SymbolInformation > > runWorkspaceSymbols(ClangdServer &Server, llvm::StringRef Query, int Limit)
Definition: SyncAPI.cpp:114
ClangdServer Server
llvm::Expected< std::vector< DocumentHighlight > > runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:93
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
Definition: Ref.cpp:35
void locateSymbolAt(PathRef File, Position Pos, Callback< std::vector< LocatedSymbol >> CB)
Find declaration/definition locations of symbol at a specified position.
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:78
void semanticRanges(PathRef File, Position Pos, Callback< std::vector< Range >> CB)
Get semantic ranges around a specified position in a file.
llvm::Expected< std::vector< TextEdit > > runRename(ClangdServer &Server, PathRef File, Position Pos, llvm::StringRef NewName)
Definition: SyncAPI.cpp:99
void workspaceSymbols(StringRef Query, int Limit, Callback< std::vector< SymbolInformation >> CB)
Retrieve the top symbols from the workspace matching a query.
void signatureHelp(PathRef File, Position Pos, Callback< SignatureHelp > CB)
Provide signature help for File at Pos.
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID)
Definition: SyncAPI.cpp:140
void runAddDocument(ClangdServer &Server, PathRef File, llvm::StringRef Contents, WantDiagnostics WantDiags)
Definition: SyncAPI.cpp:15
const SymbolIndex * Index
Definition: Dexp.cpp:84