1 //===--- CodeComplete.h ------------------------------------------*- 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 // Code completion provides suggestions for what the user might type next.
10 // After "std::string S; S." we might suggest members of std::string.
11 // Signature help describes the parameters of a function as you type them.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
16 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
17 
18 #include "ASTSignals.h"
19 #include "Compiler.h"
20 #include "Protocol.h"
21 #include "Quality.h"
22 #include "index/Index.h"
23 #include "index/Symbol.h"
24 #include "index/SymbolOrigin.h"
25 #include "support/Markup.h"
26 #include "support/Path.h"
27 #include "clang/Sema/CodeCompleteConsumer.h"
28 #include "clang/Sema/CodeCompleteOptions.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringRef.h"
32 #include <functional>
33 #include <future>
34 
35 namespace clang {
36 class NamedDecl;
37 namespace clangd {
38 struct PreambleData;
39 struct CodeCompletion;
40 
41 struct CodeCompleteOptions {
42   /// Returns options that can be passed to clang's completion engine.
43   clang::CodeCompleteOptions getClangCompleteOpts() const;
44 
45   /// When true, completion items will contain expandable code snippets in
46   /// completion (e.g.  `return ${1:expression}` or `foo(${1:int a}, ${2:int
47   /// b})).
48   bool EnableSnippets = false;
49 
50   /// Include results that are not legal completions in the current context.
51   /// For example, private members are usually inaccessible.
52   bool IncludeIneligibleResults = false;
53 
54   /// Combine overloads into a single completion item where possible.
55   /// If none, the implementation may choose an appropriate behavior.
56   /// (In practice, ClangdLSPServer enables bundling if the client claims
57   /// to supports signature help).
58   llvm::Optional<bool> BundleOverloads;
59 
60   /// Limit the number of results returned (0 means no limit).
61   /// If more results are available, we set CompletionList.isIncomplete.
62   size_t Limit = 0;
63 
64   /// Whether to present doc comments as plain-text or markdown.
65   MarkupKind DocumentationFormat = MarkupKind::PlainText;
66 
67   enum IncludeInsertion {
68     IWYU,
69     NeverInsert,
70   } InsertIncludes = IncludeInsertion::IWYU;
71 
72   /// A visual indicator to prepend to the completion label to indicate whether
73   /// completion result would trigger an #include insertion or not.
74   struct IncludeInsertionIndicator {
75     std::string Insert = "•";
76     std::string NoInsert = " ";
77   } IncludeIndicator;
78 
79   /// Expose origins of completion items in the label (for debugging).
80   bool ShowOrigins = false;
81 
82   // Populated internally by clangd, do not set.
83   /// If `Index` is set, it is used to augment the code completion
84   /// results.
85   /// FIXME(ioeric): we might want a better way to pass the index around inside
86   /// clangd.
87   const SymbolIndex *Index = nullptr;
88 
89   const ASTSignals *MainFileSignals = nullptr;
90   /// Include completions that require small corrections, e.g. change '.' to
91   /// '->' on member access etc.
92   bool IncludeFixIts = false;
93 
94   /// Whether to generate snippets for function arguments on code-completion.
95   /// Needs snippets to be enabled as well.
96   bool EnableFunctionArgSnippets = true;
97 
98   /// Whether to include index symbols that are not defined in the scopes
99   /// visible from the code completion point. This applies in contexts without
100   /// explicit scope qualifiers.
101   ///
102   /// Such completions can insert scope qualifiers.
103   bool AllScopes = false;
104 
105   /// Whether to use the clang parser, or fallback to text-based completion
106   /// (using identifiers in the current file and symbol indexes).
107   enum CodeCompletionParse {
108     /// Block until we can run the parser (e.g. preamble is built).
109     /// Return an error if this fails.
110     AlwaysParse,
111     /// Run the parser if inputs (preamble) are ready.
112     /// Otherwise, use text-based completion.
113     ParseIfReady,
114     /// Always use text-based completion.
115     NeverParse,
116   } RunParser = ParseIfReady;
117 
118   /// Callback invoked on all CompletionCandidate after they are scored and
119   /// before they are ranked (by -Score). Thus the results are yielded in
120   /// arbitrary order.
121   ///
122   /// This callbacks allows capturing various internal structures used by clangd
123   /// during code completion. Eg: Symbol quality and relevance signals.
124   std::function<void(const CodeCompletion &, const SymbolQualitySignals &,
125                      const SymbolRelevanceSignals &, float Score)>
126       RecordCCResult;
127 
128   /// Model to use for ranking code completion candidates.
129   enum CodeCompletionRankingModel {
130     Heuristics,
131     DecisionForest,
132   } RankingModel = DecisionForest;
133 
134   /// Callback used to score a CompletionCandidate if DecisionForest ranking
135   /// model is enabled.
136   /// This allows us to inject experimental models and compare them with
137   /// baseline model using A/B testing.
138   std::function<DecisionForestScores(
139       const SymbolQualitySignals &, const SymbolRelevanceSignals &, float Base)>
140       DecisionForestScorer = &evaluateDecisionForest;
141   /// Weight for combining NameMatch and Prediction of DecisionForest.
142   /// CompletionScore is NameMatch * pow(Base, Prediction).
143   /// The optimal value of Base largely depends on the semantics of the model
144   /// and prediction score (e.g. algorithm used during training, number of
145   /// trees, etc.). Usually if the range of Prediciton is [-20, 20] then a Base
146   /// in [1.2, 1.7] works fine.
147   /// Semantics: E.g. For Base = 1.3, if the Prediciton score reduces by 2.6
148   /// points then completion score reduces by 50% or 1.3^(-2.6).
149   float DecisionForestBase = 1.3f;
150 };
151 
152 // Semi-structured representation of a code-complete suggestion for our C++ API.
153 // We don't use the LSP structures here (unlike most features) as we want
154 // to expose more data to allow for more precise testing and evaluation.
155 struct CodeCompletion {
156   // The unqualified name of the symbol or other completion item.
157   std::string Name;
158   // The name of the symbol for filtering and sorting purposes. Typically the
159   // same as `Name`, but may be different e.g. for ObjC methods, `Name` is the
160   // first selector fragment but the `FilterText` is the entire selector.
161   std::string FilterText;
162   // The scope qualifier for the symbol name. e.g. "ns1::ns2::"
163   // Empty for non-symbol completions. Not inserted, but may be displayed.
164   std::string Scope;
165   // Text that must be inserted before the name, and displayed (e.g. base::).
166   std::string RequiredQualifier;
167   // Details to be displayed following the name. Not inserted.
168   std::string Signature;
169   // Text to be inserted following the name, in snippet format.
170   std::string SnippetSuffix;
171   // Type to be displayed for this completion.
172   std::string ReturnType;
173   // The parsed documentation comment.
174   llvm::Optional<markup::Document> Documentation;
175   CompletionItemKind Kind = CompletionItemKind::Missing;
176   // This completion item may represent several symbols that can be inserted in
177   // the same way, such as function overloads. In this case BundleSize > 1, and
178   // the following fields are summaries:
179   //  - Signature is e.g. "(...)" for functions.
180   //  - SnippetSuffix is similarly e.g. "(${0})".
181   //  - ReturnType may be empty
182   //  - Documentation may be from one symbol, or a combination of several
183   // Other fields should apply equally to all bundled completions.
184   unsigned BundleSize = 1;
185   SymbolOrigin Origin = SymbolOrigin::Unknown;
186 
187   struct IncludeCandidate {
188     // The header through which this symbol could be included.
189     // Quoted string as expected by an #include directive, e.g. "<memory>".
190     // Empty for non-symbol completions, or when not known.
191     std::string Header;
192     // Present if Header should be inserted to use this item.
193     llvm::Optional<TextEdit> Insertion;
194   };
195   // All possible include headers ranked by preference. By default, the first
196   // include is used.
197   // If we've bundled together overloads that have different sets of includes,
198   // thse includes may not be accurate for all of them.
199   llvm::SmallVector<IncludeCandidate, 1> Includes;
200 
201   /// Holds information about small corrections that needs to be done. Like
202   /// converting '->' to '.' on member access.
203   std::vector<TextEdit> FixIts;
204 
205   /// Holds the range of the token we are going to replace with this completion.
206   Range CompletionTokenRange;
207 
208   // Scores are used to rank completion items.
209   struct Scores {
210     // The score that items are ranked by.
211     float Total = 0.f;
212 
213     // The finalScore with the fuzzy name match score excluded.
214     // When filtering client-side, editors should calculate the new fuzzy score,
215     // whose scale is 0-1 (with 1 = prefix match, special case 2 = exact match),
216     // and recompute finalScore = fuzzyScore * symbolScore.
217     float ExcludingName = 0.f;
218 
219     // Component scores that contributed to the final score:
220 
221     // Quality describes how important we think this candidate is,
222     // independent of the query.
223     // e.g. symbols with lots of incoming references have higher quality.
224     float Quality = 0.f;
225     // Relevance describes how well this candidate matched the query.
226     // e.g. symbols from nearby files have higher relevance.
227     float Relevance = 0.f;
228   };
229   Scores Score;
230 
231   /// Indicates if this item is deprecated.
232   bool Deprecated = false;
233 
234   // Serialize this to an LSP completion item. This is a lossy operation.
235   CompletionItem render(const CodeCompleteOptions &) const;
236 };
237 raw_ostream &operator<<(raw_ostream &, const CodeCompletion &);
238 struct CodeCompleteResult {
239   std::vector<CodeCompletion> Completions;
240   bool HasMore = false;
241   CodeCompletionContext::Kind Context = CodeCompletionContext::CCC_Other;
242   // The text that is being directly completed.
243   // Example: foo.pb^ -> foo.push_back()
244   //              ~~
245   // Typically matches the textEdit.range of Completions, but not guaranteed to.
246   llvm::Optional<Range> CompletionRange;
247   // Usually the source will be parsed with a real C++ parser.
248   // But heuristics may be used instead if e.g. the preamble is not ready.
249   bool RanParser = true;
250 };
251 raw_ostream &operator<<(raw_ostream &, const CodeCompleteResult &);
252 
253 /// A speculative and asynchronous fuzzy find index request (based on cached
254 /// request) that can be sent before parsing sema. This would reduce completion
255 /// latency if the speculation succeeds.
256 struct SpeculativeFuzzyFind {
257   /// A cached request from past code completions.
258   /// Set by caller of `codeComplete()`.
259   llvm::Optional<FuzzyFindRequest> CachedReq;
260   /// The actual request used by `codeComplete()`.
261   /// Set by `codeComplete()`. This can be used by callers to update cache.
262   llvm::Optional<FuzzyFindRequest> NewReq;
263   /// The result is consumed by `codeComplete()` if speculation succeeded.
264   /// NOTE: the destructor will wait for the async call to finish.
265   std::future<SymbolSlab> Result;
266 };
267 
268 /// Gets code completions at a specified \p Pos in \p FileName.
269 ///
270 /// If \p Preamble is nullptr, this runs code completion without compiling the
271 /// code.
272 ///
273 /// If \p SpecFuzzyFind is set, a speculative and asynchronous fuzzy find index
274 /// request (based on cached request) will be run before parsing sema. In case
275 /// the speculative result is used by code completion (e.g. speculation failed),
276 /// the speculative result is not consumed, and `SpecFuzzyFind` is only
277 /// destroyed when the async request finishes.
278 CodeCompleteResult codeComplete(PathRef FileName, Position Pos,
279                                 const PreambleData *Preamble,
280                                 const ParseInputs &ParseInput,
281                                 CodeCompleteOptions Opts,
282                                 SpeculativeFuzzyFind *SpecFuzzyFind = nullptr);
283 
284 /// Get signature help at a specified \p Pos in \p FileName.
285 SignatureHelp signatureHelp(PathRef FileName, Position Pos,
286                             const PreambleData &Preamble,
287                             const ParseInputs &ParseInput,
288                             MarkupKind DocumentationFormat);
289 
290 // For index-based completion, we only consider:
291 //   * symbols in namespaces or translation unit scopes (e.g. no class
292 //     members, no locals)
293 //   * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
294 //   * primary templates (no specializations)
295 // For the other cases, we let Clang do the completion because it does not
296 // need any non-local information and it will be much better at following
297 // lookup rules. Other symbols still appear in the index for other purposes,
298 // like workspace/symbols or textDocument/definition, but are not used for code
299 // completion.
300 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx);
301 
302 // Text immediately before the completion point that should be completed.
303 // This is heuristically derived from the source code, and is used when:
304 //   - semantic analysis fails
305 //   - semantic analysis may be slow, and we speculatively query the index
306 struct CompletionPrefix {
307   // The unqualified partial name.
308   // If there is none, begin() == end() == completion position.
309   llvm::StringRef Name;
310   // The spelled scope qualifier, such as Foo::.
311   // If there is none, begin() == end() == Name.begin().
312   llvm::StringRef Qualifier;
313 };
314 // Heuristically parses before Offset to determine what should be completed.
315 CompletionPrefix guessCompletionPrefix(llvm::StringRef Content,
316                                        unsigned Offset);
317 
318 // Whether it makes sense to complete at the point based on typed characters.
319 // For instance, we implicitly trigger at `a->^` but not at `a>^`.
320 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset);
321 
322 } // namespace clangd
323 } // namespace clang
324 
325 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
326