blob: 65c415fbacab62efaebe9edea7b4b9c5004a3393 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- CodeComplete.cpp ----------------------------------------*- C++-*-===//
Sam McCall98775c52017-12-04 13:49:59 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
Sam McCall98775c52017-12-04 13:49:59 +00009//
Sam McCallc18c2802018-06-15 11:06:29 +000010// Code completion has several moving parts:
11// - AST-based completions are provided using the completion hooks in Sema.
12// - external completions are retrieved from the index (using hints from Sema)
13// - the two sources overlap, and must be merged and overloads bundled
14// - results must be scored and ranked (see Quality.h) before rendering
Sam McCall98775c52017-12-04 13:49:59 +000015//
Sam McCallc18c2802018-06-15 11:06:29 +000016// Signature help works in a similar way as code completion, but it is simpler:
17// it's purely AST-based, and there are few candidates.
Sam McCall98775c52017-12-04 13:49:59 +000018//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000019//===----------------------------------------------------------------------===//
Sam McCall98775c52017-12-04 13:49:59 +000020
21#include "CodeComplete.h"
Eric Liu7ad16962018-06-22 10:46:59 +000022#include "AST.h"
Eric Liu63696e12017-12-20 17:24:31 +000023#include "CodeCompletionStrings.h"
Sam McCall98775c52017-12-04 13:49:59 +000024#include "Compiler.h"
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000025#include "Diagnostics.h"
Sam McCall3f0243f2018-07-03 08:09:29 +000026#include "FileDistance.h"
Sam McCall84652cc2018-01-12 16:16:09 +000027#include "FuzzyMatch.h"
Eric Liu63f419a2018-05-15 15:29:32 +000028#include "Headers.h"
Eric Liu6f648df2017-12-19 16:50:37 +000029#include "Logger.h"
Sam McCallc5707b62018-05-15 17:43:27 +000030#include "Quality.h"
Eric Liuc5105f92018-02-16 14:15:55 +000031#include "SourceCode.h"
Eric Liu25d74e92018-08-24 11:23:56 +000032#include "TUScheduler.h"
Sam McCall2b780162018-01-30 17:20:54 +000033#include "Trace.h"
Eric Liu63f419a2018-05-15 15:29:32 +000034#include "URI.h"
Eric Liu6f648df2017-12-19 16:50:37 +000035#include "index/Index.h"
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000036#include "clang/ASTMatchers/ASTMatchFinder.h"
Ilya Biryukovc22d3442018-05-16 12:32:49 +000037#include "clang/Basic/LangOptions.h"
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +000038#include "clang/Basic/SourceLocation.h"
Eric Liuc5105f92018-02-16 14:15:55 +000039#include "clang/Format/Format.h"
Sam McCall98775c52017-12-04 13:49:59 +000040#include "clang/Frontend/CompilerInstance.h"
41#include "clang/Frontend/FrontendActions.h"
Sam McCall545a20d2018-01-19 14:34:02 +000042#include "clang/Index/USRGeneration.h"
Sam McCall98775c52017-12-04 13:49:59 +000043#include "clang/Sema/CodeCompleteConsumer.h"
44#include "clang/Sema/Sema.h"
Eric Liuc5105f92018-02-16 14:15:55 +000045#include "clang/Tooling/Core/Replacement.h"
Eric Liu25d74e92018-08-24 11:23:56 +000046#include "llvm/ADT/Optional.h"
47#include "llvm/Support/Error.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000048#include "llvm/Support/Format.h"
Eric Liubc25ef72018-07-05 08:29:33 +000049#include "llvm/Support/FormatVariadic.h"
Sam McCall2161ec72018-07-05 06:20:41 +000050#include "llvm/Support/ScopedPrinter.h"
Sam McCall98775c52017-12-04 13:49:59 +000051#include <queue>
52
Sam McCallc5707b62018-05-15 17:43:27 +000053// We log detailed candidate here if you run with -debug-only=codecomplete.
Sam McCall27c979a2018-06-29 14:47:57 +000054#define DEBUG_TYPE "CodeComplete"
Sam McCallc5707b62018-05-15 17:43:27 +000055
Sam McCall98775c52017-12-04 13:49:59 +000056namespace clang {
57namespace clangd {
58namespace {
59
Eric Liu6f648df2017-12-19 16:50:37 +000060CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
61 using SK = index::SymbolKind;
62 switch (Kind) {
63 case SK::Unknown:
64 return CompletionItemKind::Missing;
65 case SK::Module:
66 case SK::Namespace:
67 case SK::NamespaceAlias:
68 return CompletionItemKind::Module;
69 case SK::Macro:
70 return CompletionItemKind::Text;
71 case SK::Enum:
72 return CompletionItemKind::Enum;
73 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
74 // protocol.
75 case SK::Struct:
76 case SK::Class:
77 case SK::Protocol:
78 case SK::Extension:
79 case SK::Union:
80 return CompletionItemKind::Class;
81 // FIXME(ioeric): figure out whether reference is the right type for aliases.
82 case SK::TypeAlias:
83 case SK::Using:
84 return CompletionItemKind::Reference;
85 case SK::Function:
86 // FIXME(ioeric): this should probably be an operator. This should be fixed
87 // when `Operator` is support type in the protocol.
88 case SK::ConversionFunction:
89 return CompletionItemKind::Function;
90 case SK::Variable:
91 case SK::Parameter:
92 return CompletionItemKind::Variable;
93 case SK::Field:
94 return CompletionItemKind::Field;
95 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
96 case SK::EnumConstant:
97 return CompletionItemKind::Value;
98 case SK::InstanceMethod:
99 case SK::ClassMethod:
100 case SK::StaticMethod:
101 case SK::Destructor:
102 return CompletionItemKind::Method;
103 case SK::InstanceProperty:
104 case SK::ClassProperty:
105 case SK::StaticProperty:
106 return CompletionItemKind::Property;
107 case SK::Constructor:
108 return CompletionItemKind::Constructor;
109 }
110 llvm_unreachable("Unhandled clang::index::SymbolKind.");
111}
112
Sam McCall83305892018-06-08 21:17:19 +0000113CompletionItemKind
114toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
115 const NamedDecl *Decl) {
116 if (Decl)
117 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
118 switch (ResKind) {
119 case CodeCompletionResult::RK_Declaration:
120 llvm_unreachable("RK_Declaration without Decl");
121 case CodeCompletionResult::RK_Keyword:
122 return CompletionItemKind::Keyword;
123 case CodeCompletionResult::RK_Macro:
124 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
125 // completion items in LSP.
126 case CodeCompletionResult::RK_Pattern:
127 return CompletionItemKind::Snippet;
128 }
129 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
130}
131
Sam McCall98775c52017-12-04 13:49:59 +0000132/// Get the optional chunk as a string. This function is possibly recursive.
133///
134/// The parameter info for each parameter is appended to the Parameters.
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000135std::string getOptionalParameters(const CodeCompletionString &CCS,
136 std::vector<ParameterInformation> &Parameters,
137 SignatureQualitySignals &Signal) {
Sam McCall98775c52017-12-04 13:49:59 +0000138 std::string Result;
139 for (const auto &Chunk : CCS) {
140 switch (Chunk.Kind) {
141 case CodeCompletionString::CK_Optional:
142 assert(Chunk.Optional &&
143 "Expected the optional code completion string to be non-null.");
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000144 Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal);
Sam McCall98775c52017-12-04 13:49:59 +0000145 break;
146 case CodeCompletionString::CK_VerticalSpace:
147 break;
148 case CodeCompletionString::CK_Placeholder:
149 // A string that acts as a placeholder for, e.g., a function call
150 // argument.
151 // Intentional fallthrough here.
152 case CodeCompletionString::CK_CurrentParameter: {
153 // A piece of text that describes the parameter that corresponds to
154 // the code-completion location within a function call, message send,
155 // macro invocation, etc.
156 Result += Chunk.Text;
157 ParameterInformation Info;
158 Info.label = Chunk.Text;
159 Parameters.push_back(std::move(Info));
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000160 Signal.ContainsActiveParameter = true;
161 Signal.NumberOfOptionalParameters++;
Sam McCall98775c52017-12-04 13:49:59 +0000162 break;
163 }
164 default:
165 Result += Chunk.Text;
166 break;
167 }
168 }
169 return Result;
170}
171
Eric Liu63f419a2018-05-15 15:29:32 +0000172/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
173/// include.
174static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
175 llvm::StringRef HintPath) {
176 if (isLiteralInclude(Header))
177 return HeaderFile{Header.str(), /*Verbatim=*/true};
178 auto U = URI::parse(Header);
179 if (!U)
180 return U.takeError();
181
182 auto IncludePath = URI::includeSpelling(*U);
183 if (!IncludePath)
184 return IncludePath.takeError();
185 if (!IncludePath->empty())
186 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
187
188 auto Resolved = URI::resolve(*U, HintPath);
189 if (!Resolved)
190 return Resolved.takeError();
191 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
192}
193
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000194// First traverses all method definitions inside current class/struct/union
195// definition. Than traverses base classes to find virtual methods that haven't
196// been overriden within current context.
197// FIXME(kadircet): Currently we cannot see declarations below completion point.
198// It is because Sema gets run only upto completion point. Need to find a
199// solution to run it for the whole class/struct/union definition.
200static std::vector<CodeCompletionResult>
201getNonOverridenMethodCompletionResults(const DeclContext *DC, Sema *S) {
202 const auto *CR = llvm::dyn_cast<CXXRecordDecl>(DC);
203 // If not inside a class/struct/union return empty.
204 if (!CR)
205 return {};
206 // First store overrides within current class.
207 // These are stored by name to make querying fast in the later step.
208 llvm::StringMap<std::vector<FunctionDecl *>> Overrides;
209 for (auto *Method : CR->methods()) {
210 if (!Method->isVirtual())
211 continue;
212 Overrides[Method->getName()].push_back(Method);
213 }
214
215 std::vector<CodeCompletionResult> Results;
216 for (const auto &Base : CR->bases()) {
217 const auto *BR = Base.getType().getTypePtr()->getAsCXXRecordDecl();
218 if (!BR)
219 continue;
220 for (auto *Method : BR->methods()) {
221 if (!Method->isVirtual())
222 continue;
223 const auto it = Overrides.find(Method->getName());
224 bool IsOverriden = false;
225 if (it != Overrides.end()) {
226 for (auto *MD : it->second) {
227 // If the method in current body is not an overload of this virtual
228 // function, that it overrides this one.
229 if (!S->IsOverload(MD, Method, false)) {
230 IsOverriden = true;
231 break;
232 }
233 }
234 }
235 if (!IsOverriden)
236 Results.emplace_back(Method, 0);
237 }
238 }
239
240 return Results;
241}
242
Sam McCall545a20d2018-01-19 14:34:02 +0000243/// A code completion result, in clang-native form.
Sam McCall98775c52017-12-04 13:49:59 +0000244/// It may be promoted to a CompletionItem if it's among the top-ranked results.
245struct CompletionCandidate {
Sam McCall545a20d2018-01-19 14:34:02 +0000246 llvm::StringRef Name; // Used for filtering and sorting.
247 // We may have a result from Sema, from the index, or both.
248 const CodeCompletionResult *SemaResult = nullptr;
249 const Symbol *IndexResult = nullptr;
Sam McCall98775c52017-12-04 13:49:59 +0000250
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000251 // States whether this item is an override suggestion.
252 bool IsOverride = false;
253
Sam McCallc18c2802018-06-15 11:06:29 +0000254 // Returns a token identifying the overload set this is part of.
255 // 0 indicates it's not part of any overload set.
256 size_t overloadSet() const {
257 SmallString<256> Scratch;
258 if (IndexResult) {
259 switch (IndexResult->SymInfo.Kind) {
260 case index::SymbolKind::ClassMethod:
261 case index::SymbolKind::InstanceMethod:
262 case index::SymbolKind::StaticMethod:
263 assert(false && "Don't expect members from index in code completion");
264 // fall through
265 case index::SymbolKind::Function:
266 // We can't group overloads together that need different #includes.
267 // This could break #include insertion.
268 return hash_combine(
269 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
270 headerToInsertIfNotPresent().getValueOr(""));
271 default:
272 return 0;
273 }
274 }
275 assert(SemaResult);
276 // We need to make sure we're consistent with the IndexResult case!
277 const NamedDecl *D = SemaResult->Declaration;
278 if (!D || !D->isFunctionOrFunctionTemplate())
279 return 0;
280 {
281 llvm::raw_svector_ostream OS(Scratch);
282 D->printQualifiedName(OS);
283 }
284 return hash_combine(Scratch, headerToInsertIfNotPresent().getValueOr(""));
285 }
286
287 llvm::Optional<llvm::StringRef> headerToInsertIfNotPresent() const {
288 if (!IndexResult || !IndexResult->Detail ||
289 IndexResult->Detail->IncludeHeader.empty())
290 return llvm::None;
291 if (SemaResult && SemaResult->Declaration) {
292 // Avoid inserting new #include if the declaration is found in the current
293 // file e.g. the symbol is forward declared.
294 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
295 for (const Decl *RD : SemaResult->Declaration->redecls())
Stephen Kelly43465bf2018-08-09 22:42:26 +0000296 if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
Sam McCallc18c2802018-06-15 11:06:29 +0000297 return llvm::None;
298 }
299 return IndexResult->Detail->IncludeHeader;
300 }
301
Sam McCallc18c2802018-06-15 11:06:29 +0000302 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
Sam McCall98775c52017-12-04 13:49:59 +0000303};
Sam McCallc18c2802018-06-15 11:06:29 +0000304using ScoredBundle =
Sam McCall27c979a2018-06-29 14:47:57 +0000305 std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
Sam McCallc18c2802018-06-15 11:06:29 +0000306struct ScoredBundleGreater {
307 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
Sam McCall27c979a2018-06-29 14:47:57 +0000308 if (L.second.Total != R.second.Total)
309 return L.second.Total > R.second.Total;
Sam McCallc18c2802018-06-15 11:06:29 +0000310 return L.first.front().Name <
311 R.first.front().Name; // Earlier name is better.
312 }
313};
Sam McCall98775c52017-12-04 13:49:59 +0000314
Sam McCall27c979a2018-06-29 14:47:57 +0000315// Assembles a code completion out of a bundle of >=1 completion candidates.
316// Many of the expensive strings are only computed at this point, once we know
317// the candidate bundle is going to be returned.
318//
319// Many fields are the same for all candidates in a bundle (e.g. name), and are
320// computed from the first candidate, in the constructor.
321// Others vary per candidate, so add() must be called for remaining candidates.
322struct CodeCompletionBuilder {
323 CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C,
324 CodeCompletionString *SemaCCS,
325 const IncludeInserter &Includes, StringRef FileName,
326 const CodeCompleteOptions &Opts)
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000327 : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments),
328 EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) {
Sam McCall27c979a2018-06-29 14:47:57 +0000329 add(C, SemaCCS);
330 if (C.SemaResult) {
Sam McCall4e5742a2018-07-06 11:50:49 +0000331 Completion.Origin |= SymbolOrigin::AST;
Sam McCall27c979a2018-06-29 14:47:57 +0000332 Completion.Name = llvm::StringRef(SemaCCS->getTypedText());
Eric Liuf433c2d2018-07-18 15:31:14 +0000333 if (Completion.Scope.empty()) {
334 if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
335 (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
Sam McCall27c979a2018-06-29 14:47:57 +0000336 if (const auto *D = C.SemaResult->getDeclaration())
337 if (const auto *ND = llvm::dyn_cast<NamedDecl>(D))
338 Completion.Scope =
339 splitQualifiedName(printQualifiedName(*ND)).first;
Eric Liuf433c2d2018-07-18 15:31:14 +0000340 }
Sam McCall27c979a2018-06-29 14:47:57 +0000341 Completion.Kind =
342 toCompletionItemKind(C.SemaResult->Kind, C.SemaResult->Declaration);
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000343 for (const auto &FixIt : C.SemaResult->FixIts) {
344 Completion.FixIts.push_back(
345 toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts()));
346 }
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000347 std::sort(Completion.FixIts.begin(), Completion.FixIts.end(),
348 [](const TextEdit &X, const TextEdit &Y) {
349 return std::tie(X.range.start.line, X.range.start.character) <
350 std::tie(Y.range.start.line, Y.range.start.character);
351 });
Sam McCall27c979a2018-06-29 14:47:57 +0000352 }
353 if (C.IndexResult) {
Sam McCall4e5742a2018-07-06 11:50:49 +0000354 Completion.Origin |= C.IndexResult->Origin;
Sam McCall27c979a2018-06-29 14:47:57 +0000355 if (Completion.Scope.empty())
356 Completion.Scope = C.IndexResult->Scope;
357 if (Completion.Kind == CompletionItemKind::Missing)
358 Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
359 if (Completion.Name.empty())
360 Completion.Name = C.IndexResult->Name;
361 }
362 if (auto Inserted = C.headerToInsertIfNotPresent()) {
363 // Turn absolute path into a literal string that can be #included.
364 auto Include = [&]() -> Expected<std::pair<std::string, bool>> {
365 auto ResolvedDeclaring =
366 toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
367 if (!ResolvedDeclaring)
368 return ResolvedDeclaring.takeError();
369 auto ResolvedInserted = toHeaderFile(*Inserted, FileName);
370 if (!ResolvedInserted)
371 return ResolvedInserted.takeError();
372 return std::make_pair(Includes.calculateIncludePath(*ResolvedDeclaring,
373 *ResolvedInserted),
374 Includes.shouldInsertInclude(*ResolvedDeclaring,
375 *ResolvedInserted));
376 }();
377 if (Include) {
378 Completion.Header = Include->first;
379 if (Include->second)
380 Completion.HeaderInsertion = Includes.insert(Include->first);
381 } else
Sam McCallbed58852018-07-11 10:35:11 +0000382 log("Failed to generate include insertion edits for adding header "
Sam McCall27c979a2018-06-29 14:47:57 +0000383 "(FileURI='{0}', IncludeHeader='{1}') into {2}",
384 C.IndexResult->CanonicalDeclaration.FileURI,
Sam McCallbed58852018-07-11 10:35:11 +0000385 C.IndexResult->Detail->IncludeHeader, FileName);
Sam McCall27c979a2018-06-29 14:47:57 +0000386 }
387 }
388
389 void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
390 assert(bool(C.SemaResult) == bool(SemaCCS));
391 Bundled.emplace_back();
392 BundledEntry &S = Bundled.back();
393 if (C.SemaResult) {
394 getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
395 &Completion.RequiredQualifier);
396 S.ReturnType = getReturnType(*SemaCCS);
397 } else if (C.IndexResult) {
398 S.Signature = C.IndexResult->Signature;
399 S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix;
400 if (auto *D = C.IndexResult->Detail)
401 S.ReturnType = D->ReturnType;
402 }
403 if (ExtractDocumentation && Completion.Documentation.empty()) {
404 if (C.IndexResult && C.IndexResult->Detail)
405 Completion.Documentation = C.IndexResult->Detail->Documentation;
406 else if (C.SemaResult)
407 Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult,
408 /*CommentsFromHeader=*/false);
409 }
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000410 if (C.IsOverride)
411 S.OverrideSuffix = true;
Sam McCall27c979a2018-06-29 14:47:57 +0000412 }
413
414 CodeCompletion build() {
415 Completion.ReturnType = summarizeReturnType();
416 Completion.Signature = summarizeSignature();
417 Completion.SnippetSuffix = summarizeSnippet();
418 Completion.BundleSize = Bundled.size();
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000419 if (summarizeOverride()) {
420 Completion.Name = Completion.ReturnType + ' ' +
421 std::move(Completion.Name) +
422 std::move(Completion.Signature) + " override";
423 Completion.Signature.clear();
424 }
Sam McCall27c979a2018-06-29 14:47:57 +0000425 return std::move(Completion);
426 }
427
428private:
429 struct BundledEntry {
430 std::string SnippetSuffix;
431 std::string Signature;
432 std::string ReturnType;
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000433 bool OverrideSuffix;
Sam McCall27c979a2018-06-29 14:47:57 +0000434 };
435
436 // If all BundledEntrys have the same value for a property, return it.
437 template <std::string BundledEntry::*Member>
438 const std::string *onlyValue() const {
439 auto B = Bundled.begin(), E = Bundled.end();
440 for (auto I = B + 1; I != E; ++I)
441 if (I->*Member != B->*Member)
442 return nullptr;
443 return &(B->*Member);
444 }
445
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000446 template <bool BundledEntry::*Member> const bool *onlyValue() const {
447 auto B = Bundled.begin(), E = Bundled.end();
448 for (auto I = B + 1; I != E; ++I)
449 if (I->*Member != B->*Member)
450 return nullptr;
451 return &(B->*Member);
452 }
453
Sam McCall27c979a2018-06-29 14:47:57 +0000454 std::string summarizeReturnType() const {
455 if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
456 return *RT;
457 return "";
458 }
459
460 std::string summarizeSnippet() const {
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000461 auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
462 if (!Snippet)
463 // All bundles are function calls.
464 return "($0)";
465 if (!Snippet->empty() && !EnableFunctionArgSnippets &&
466 ((Completion.Kind == CompletionItemKind::Function) ||
467 (Completion.Kind == CompletionItemKind::Method)) &&
468 (Snippet->front() == '(') && (Snippet->back() == ')'))
469 // Check whether function has any parameters or not.
470 return Snippet->size() > 2 ? "($0)" : "()";
471 return *Snippet;
Sam McCall27c979a2018-06-29 14:47:57 +0000472 }
473
474 std::string summarizeSignature() const {
475 if (auto *Signature = onlyValue<&BundledEntry::Signature>())
476 return *Signature;
477 // All bundles are function calls.
478 return "(…)";
479 }
480
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +0000481 bool summarizeOverride() const {
482 if (auto *OverrideSuffix = onlyValue<&BundledEntry::OverrideSuffix>())
483 return *OverrideSuffix;
484 return false;
485 }
486
Sam McCall27c979a2018-06-29 14:47:57 +0000487 ASTContext &ASTCtx;
488 CodeCompletion Completion;
489 SmallVector<BundledEntry, 1> Bundled;
490 bool ExtractDocumentation;
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +0000491 bool EnableFunctionArgSnippets;
Sam McCall27c979a2018-06-29 14:47:57 +0000492};
493
Sam McCall545a20d2018-01-19 14:34:02 +0000494// Determine the symbol ID for a Sema code completion result, if possible.
495llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) {
496 switch (R.Kind) {
497 case CodeCompletionResult::RK_Declaration:
498 case CodeCompletionResult::RK_Pattern: {
Haojian Wuc6ddb462018-08-07 08:57:52 +0000499 return clang::clangd::getSymbolID(R.Declaration);
Sam McCall545a20d2018-01-19 14:34:02 +0000500 }
501 case CodeCompletionResult::RK_Macro:
502 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info.
503 case CodeCompletionResult::RK_Keyword:
504 return None;
505 }
506 llvm_unreachable("unknown CodeCompletionResult kind");
507}
508
Haojian Wu061c73e2018-01-23 11:37:26 +0000509// Scopes of the paritial identifier we're trying to complete.
510// It is used when we query the index for more completion results.
Eric Liu6f648df2017-12-19 16:50:37 +0000511struct SpecifiedScope {
Haojian Wu061c73e2018-01-23 11:37:26 +0000512 // The scopes we should look in, determined by Sema.
513 //
514 // If the qualifier was fully resolved, we look for completions in these
515 // scopes; if there is an unresolved part of the qualifier, it should be
516 // resolved within these scopes.
517 //
518 // Examples of qualified completion:
519 //
520 // "::vec" => {""}
521 // "using namespace std; ::vec^" => {"", "std::"}
522 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
523 // "std::vec^" => {""} // "std" unresolved
524 //
525 // Examples of unqualified completion:
526 //
527 // "vec^" => {""}
528 // "using namespace std; vec^" => {"", "std::"}
529 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
530 //
531 // "" for global namespace, "ns::" for normal namespace.
532 std::vector<std::string> AccessibleScopes;
533 // The full scope qualifier as typed by the user (without the leading "::").
534 // Set if the qualifier is not fully resolved by Sema.
535 llvm::Optional<std::string> UnresolvedQualifier;
Sam McCall545a20d2018-01-19 14:34:02 +0000536
Haojian Wu061c73e2018-01-23 11:37:26 +0000537 // Construct scopes being queried in indexes.
538 // This method format the scopes to match the index request representation.
539 std::vector<std::string> scopesForIndexQuery() {
540 std::vector<std::string> Results;
541 for (llvm::StringRef AS : AccessibleScopes) {
542 Results.push_back(AS);
543 if (UnresolvedQualifier)
544 Results.back() += *UnresolvedQualifier;
545 }
546 return Results;
Sam McCall545a20d2018-01-19 14:34:02 +0000547 }
Eric Liu6f648df2017-12-19 16:50:37 +0000548};
549
Haojian Wu061c73e2018-01-23 11:37:26 +0000550// Get all scopes that will be queried in indexes.
551std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000552 const SourceManager &SM) {
553 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000554 SpecifiedScope Info;
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000555 for (auto *Context : CCContext.getVisitedContexts()) {
Haojian Wu061c73e2018-01-23 11:37:26 +0000556 if (isa<TranslationUnitDecl>(Context))
557 Info.AccessibleScopes.push_back(""); // global namespace
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000558 else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
Haojian Wu061c73e2018-01-23 11:37:26 +0000559 Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
560 }
561 return Info;
562 };
563
564 auto SS = CCContext.getCXXScopeSpecifier();
565
566 // Unqualified completion (e.g. "vec^").
567 if (!SS) {
568 // FIXME: Once we can insert namespace qualifiers and use the in-scope
569 // namespaces for scoring, search in all namespaces.
570 // FIXME: Capture scopes and use for scoring, for example,
571 // "using namespace std; namespace foo {v^}" =>
572 // foo::value > std::vector > boost::variant
573 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
574 }
575
576 // Qualified completion ("std::vec^"), we have two cases depending on whether
577 // the qualifier can be resolved by Sema.
578 if ((*SS)->isValid()) { // Resolved qualifier.
Haojian Wu061c73e2018-01-23 11:37:26 +0000579 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
580 }
581
582 // Unresolved qualifier.
583 // FIXME: When Sema can resolve part of a scope chain (e.g.
584 // "known::unknown::id"), we should expand the known part ("known::") rather
585 // than treating the whole thing as unknown.
586 SpecifiedScope Info;
587 Info.AccessibleScopes.push_back(""); // global namespace
588
589 Info.UnresolvedQualifier =
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000590 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
591 clang::LangOptions())
592 .ltrim("::");
Haojian Wu061c73e2018-01-23 11:37:26 +0000593 // Sema excludes the trailing "::".
594 if (!Info.UnresolvedQualifier->empty())
595 *Info.UnresolvedQualifier += "::";
596
597 return Info.scopesForIndexQuery();
598}
599
Eric Liu42abe412018-05-24 11:20:19 +0000600// Should we perform index-based completion in a context of the specified kind?
601// FIXME: consider allowing completion, but restricting the result types.
602bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
603 switch (K) {
604 case CodeCompletionContext::CCC_TopLevel:
605 case CodeCompletionContext::CCC_ObjCInterface:
606 case CodeCompletionContext::CCC_ObjCImplementation:
607 case CodeCompletionContext::CCC_ObjCIvarList:
608 case CodeCompletionContext::CCC_ClassStructUnion:
609 case CodeCompletionContext::CCC_Statement:
610 case CodeCompletionContext::CCC_Expression:
611 case CodeCompletionContext::CCC_ObjCMessageReceiver:
612 case CodeCompletionContext::CCC_EnumTag:
613 case CodeCompletionContext::CCC_UnionTag:
614 case CodeCompletionContext::CCC_ClassOrStructTag:
615 case CodeCompletionContext::CCC_ObjCProtocolName:
616 case CodeCompletionContext::CCC_Namespace:
617 case CodeCompletionContext::CCC_Type:
618 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
619 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
620 case CodeCompletionContext::CCC_ParenthesizedExpression:
621 case CodeCompletionContext::CCC_ObjCInterfaceName:
622 case CodeCompletionContext::CCC_ObjCCategoryName:
623 return true;
624 case CodeCompletionContext::CCC_Other: // Be conservative.
625 case CodeCompletionContext::CCC_OtherWithMacros:
626 case CodeCompletionContext::CCC_DotMemberAccess:
627 case CodeCompletionContext::CCC_ArrowMemberAccess:
628 case CodeCompletionContext::CCC_ObjCPropertyAccess:
629 case CodeCompletionContext::CCC_MacroName:
630 case CodeCompletionContext::CCC_MacroNameUse:
631 case CodeCompletionContext::CCC_PreprocessorExpression:
632 case CodeCompletionContext::CCC_PreprocessorDirective:
633 case CodeCompletionContext::CCC_NaturalLanguage:
634 case CodeCompletionContext::CCC_SelectorName:
635 case CodeCompletionContext::CCC_TypeQualifiers:
636 case CodeCompletionContext::CCC_ObjCInstanceMessage:
637 case CodeCompletionContext::CCC_ObjCClassMessage:
638 case CodeCompletionContext::CCC_Recovery:
639 return false;
640 }
641 llvm_unreachable("unknown code completion context");
642}
643
Sam McCall4caa8512018-06-07 12:49:17 +0000644// Some member calls are blacklisted because they're so rarely useful.
645static bool isBlacklistedMember(const NamedDecl &D) {
646 // Destructor completion is rarely useful, and works inconsistently.
647 // (s.^ completes ~string, but s.~st^ is an error).
648 if (D.getKind() == Decl::CXXDestructor)
649 return true;
650 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
651 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
652 if (R->isInjectedClassName())
653 return true;
654 // Explicit calls to operators are also rare.
655 auto NameKind = D.getDeclName().getNameKind();
656 if (NameKind == DeclarationName::CXXOperatorName ||
657 NameKind == DeclarationName::CXXLiteralOperatorName ||
658 NameKind == DeclarationName::CXXConversionFunctionName)
659 return true;
660 return false;
661}
662
Sam McCall545a20d2018-01-19 14:34:02 +0000663// The CompletionRecorder captures Sema code-complete output, including context.
664// It filters out ignored results (but doesn't apply fuzzy-filtering yet).
665// It doesn't do scoring or conversion to CompletionItem yet, as we want to
666// merge with index results first.
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000667// Generally the fields and methods of this object should only be used from
668// within the callback.
Sam McCall545a20d2018-01-19 14:34:02 +0000669struct CompletionRecorder : public CodeCompleteConsumer {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000670 CompletionRecorder(const CodeCompleteOptions &Opts,
Benjamin Kramerc36c09f2018-07-03 20:59:33 +0000671 llvm::unique_function<void()> ResultsCallback)
Sam McCall545a20d2018-01-19 14:34:02 +0000672 : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
Sam McCall98775c52017-12-04 13:49:59 +0000673 /*OutputIsBinary=*/false),
Sam McCall545a20d2018-01-19 14:34:02 +0000674 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
675 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000676 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
677 assert(this->ResultsCallback);
678 }
679
Sam McCall545a20d2018-01-19 14:34:02 +0000680 std::vector<CodeCompletionResult> Results;
681 CodeCompletionContext CCContext;
682 Sema *CCSema = nullptr; // Sema that created the results.
683 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
Sam McCall98775c52017-12-04 13:49:59 +0000684
Sam McCall545a20d2018-01-19 14:34:02 +0000685 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
686 CodeCompletionResult *InResults,
Sam McCall98775c52017-12-04 13:49:59 +0000687 unsigned NumResults) override final {
Eric Liu485074f2018-07-11 13:15:31 +0000688 // Results from recovery mode are generally useless, and the callback after
689 // recovery (if any) is usually more interesting. To make sure we handle the
690 // future callback from sema, we just ignore all callbacks in recovery mode,
691 // as taking only results from recovery mode results in poor completion
692 // results.
693 // FIXME: in case there is no future sema completion callback after the
694 // recovery mode, we might still want to provide some results (e.g. trivial
695 // identifier-based completion).
696 if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
697 log("Code complete: Ignoring sema code complete callback with Recovery "
698 "context.");
699 return;
700 }
Eric Liu42abe412018-05-24 11:20:19 +0000701 // If a callback is called without any sema result and the context does not
702 // support index-based completion, we simply skip it to give way to
703 // potential future callbacks with results.
704 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
705 return;
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000706 if (CCSema) {
Sam McCallbed58852018-07-11 10:35:11 +0000707 log("Multiple code complete callbacks (parser backtracked?). "
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000708 "Dropping results from context {0}, keeping results from {1}.",
Eric Liu42abe412018-05-24 11:20:19 +0000709 getCompletionKindString(Context.getKind()),
Sam McCallbed58852018-07-11 10:35:11 +0000710 getCompletionKindString(this->CCContext.getKind()));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000711 return;
712 }
Sam McCall545a20d2018-01-19 14:34:02 +0000713 // Record the completion context.
Sam McCall545a20d2018-01-19 14:34:02 +0000714 CCSema = &S;
715 CCContext = Context;
Eric Liu6f648df2017-12-19 16:50:37 +0000716
Sam McCall545a20d2018-01-19 14:34:02 +0000717 // Retain the results we might want.
Sam McCall98775c52017-12-04 13:49:59 +0000718 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCall545a20d2018-01-19 14:34:02 +0000719 auto &Result = InResults[I];
720 // Drop hidden items which cannot be found by lookup after completion.
721 // Exception: some items can be named by using a qualifier.
Ilya Biryukovf60bf342018-01-10 13:51:09 +0000722 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
723 continue;
Sam McCall545a20d2018-01-19 14:34:02 +0000724 if (!Opts.IncludeIneligibleResults &&
Sam McCall98775c52017-12-04 13:49:59 +0000725 (Result.Availability == CXAvailability_NotAvailable ||
726 Result.Availability == CXAvailability_NotAccessible))
727 continue;
Sam McCall4caa8512018-06-07 12:49:17 +0000728 if (Result.Declaration &&
729 !Context.getBaseType().isNull() // is this a member-access context?
730 && isBlacklistedMember(*Result.Declaration))
Sam McCalld2a95922018-01-22 21:05:00 +0000731 continue;
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000732 // We choose to never append '::' to completion results in clangd.
733 Result.StartsNestedNameSpecifier = false;
Sam McCall545a20d2018-01-19 14:34:02 +0000734 Results.push_back(Result);
Sam McCall98775c52017-12-04 13:49:59 +0000735 }
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000736 ResultsCallback();
Sam McCall98775c52017-12-04 13:49:59 +0000737 }
738
Sam McCall545a20d2018-01-19 14:34:02 +0000739 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
Sam McCall98775c52017-12-04 13:49:59 +0000740 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
741
Sam McCall545a20d2018-01-19 14:34:02 +0000742 // Returns the filtering/sorting name for Result, which must be from Results.
743 // Returned string is owned by this recorder (or the AST).
744 llvm::StringRef getName(const CodeCompletionResult &Result) {
Sam McCall98775c52017-12-04 13:49:59 +0000745 switch (Result.Kind) {
746 case CodeCompletionResult::RK_Declaration:
747 if (auto *ID = Result.Declaration->getIdentifier())
Sam McCall545a20d2018-01-19 14:34:02 +0000748 return ID->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000749 break;
750 case CodeCompletionResult::RK_Keyword:
Sam McCall545a20d2018-01-19 14:34:02 +0000751 return Result.Keyword;
Sam McCall98775c52017-12-04 13:49:59 +0000752 case CodeCompletionResult::RK_Macro:
Sam McCall545a20d2018-01-19 14:34:02 +0000753 return Result.Macro->getName();
Sam McCall98775c52017-12-04 13:49:59 +0000754 case CodeCompletionResult::RK_Pattern:
Sam McCall545a20d2018-01-19 14:34:02 +0000755 return Result.Pattern->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000756 }
Ilya Biryukov43714502018-05-16 12:32:44 +0000757 auto *CCS = codeCompletionString(Result);
Sam McCall545a20d2018-01-19 14:34:02 +0000758 return CCS->getTypedText();
Sam McCall98775c52017-12-04 13:49:59 +0000759 }
760
Sam McCall545a20d2018-01-19 14:34:02 +0000761 // Build a CodeCompletion string for R, which must be from Results.
762 // The CCS will be owned by this recorder.
Ilya Biryukov43714502018-05-16 12:32:44 +0000763 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
Sam McCall545a20d2018-01-19 14:34:02 +0000764 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
765 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
Ilya Biryukov43714502018-05-16 12:32:44 +0000766 *CCSema, CCContext, *CCAllocator, CCTUInfo,
767 /*IncludeBriefComments=*/false);
Sam McCall98775c52017-12-04 13:49:59 +0000768 }
769
Sam McCall545a20d2018-01-19 14:34:02 +0000770private:
771 CodeCompleteOptions Opts;
772 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
Sam McCall98775c52017-12-04 13:49:59 +0000773 CodeCompletionTUInfo CCTUInfo;
Benjamin Kramerc36c09f2018-07-03 20:59:33 +0000774 llvm::unique_function<void()> ResultsCallback;
Sam McCall545a20d2018-01-19 14:34:02 +0000775};
776
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000777struct ScoredSignature {
778 // When set, requires documentation to be requested from the index with this
779 // ID.
780 llvm::Optional<SymbolID> IDForDoc;
781 SignatureInformation Signature;
782 SignatureQualitySignals Quality;
783};
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000784
Sam McCall98775c52017-12-04 13:49:59 +0000785class SignatureHelpCollector final : public CodeCompleteConsumer {
Sam McCall98775c52017-12-04 13:49:59 +0000786public:
787 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000788 SymbolIndex *Index, SignatureHelp &SigHelp)
789 : CodeCompleteConsumer(CodeCompleteOpts,
790 /*OutputIsBinary=*/false),
Sam McCall98775c52017-12-04 13:49:59 +0000791 SigHelp(SigHelp),
792 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000793 CCTUInfo(Allocator), Index(Index) {}
Sam McCall98775c52017-12-04 13:49:59 +0000794
795 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
796 OverloadCandidate *Candidates,
797 unsigned NumCandidates) override {
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000798 std::vector<ScoredSignature> ScoredSignatures;
Sam McCall98775c52017-12-04 13:49:59 +0000799 SigHelp.signatures.reserve(NumCandidates);
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000800 ScoredSignatures.reserve(NumCandidates);
Sam McCall98775c52017-12-04 13:49:59 +0000801 // FIXME(rwols): How can we determine the "active overload candidate"?
802 // Right now the overloaded candidates seem to be provided in a "best fit"
803 // order, so I'm not too worried about this.
804 SigHelp.activeSignature = 0;
805 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
806 "too many arguments");
807 SigHelp.activeParameter = static_cast<int>(CurrentArg);
808 for (unsigned I = 0; I < NumCandidates; ++I) {
Ilya Biryukov8fd44bb2018-08-14 09:36:32 +0000809 OverloadCandidate Candidate = Candidates[I];
810 // We want to avoid showing instantiated signatures, because they may be
811 // long in some cases (e.g. when 'T' is substituted with 'std::string', we
812 // would get 'std::basic_string<char>').
813 if (auto *Func = Candidate.getFunction()) {
814 if (auto *Pattern = Func->getTemplateInstantiationPattern())
815 Candidate = OverloadCandidate(Pattern);
816 }
817
Sam McCall98775c52017-12-04 13:49:59 +0000818 const auto *CCS = Candidate.CreateSignatureString(
819 CurrentArg, S, *Allocator, CCTUInfo, true);
820 assert(CCS && "Expected the CodeCompletionString to be non-null");
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000821 ScoredSignatures.push_back(processOverloadCandidate(
Ilya Biryukov43714502018-05-16 12:32:44 +0000822 Candidate, *CCS,
Ilya Biryukov5f4a3512018-08-17 09:29:38 +0000823 Candidate.getFunction()
824 ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
825 : ""));
Sam McCall98775c52017-12-04 13:49:59 +0000826 }
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000827
828 // Sema does not load the docs from the preamble, so we need to fetch extra
829 // docs from the index instead.
830 llvm::DenseMap<SymbolID, std::string> FetchedDocs;
831 if (Index) {
832 LookupRequest IndexRequest;
833 for (const auto &S : ScoredSignatures) {
834 if (!S.IDForDoc)
835 continue;
836 IndexRequest.IDs.insert(*S.IDForDoc);
837 }
838 Index->lookup(IndexRequest, [&](const Symbol &S) {
839 if (!S.Detail || S.Detail->Documentation.empty())
840 return;
841 FetchedDocs[S.ID] = S.Detail->Documentation;
842 });
843 log("SigHelp: requested docs for {0} symbols from the index, got {1} "
844 "symbols with non-empty docs in the response",
845 IndexRequest.IDs.size(), FetchedDocs.size());
846 }
847
848 std::sort(
849 ScoredSignatures.begin(), ScoredSignatures.end(),
850 [](const ScoredSignature &L, const ScoredSignature &R) {
851 // Ordering follows:
852 // - Less number of parameters is better.
853 // - Function is better than FunctionType which is better than
854 // Function Template.
855 // - High score is better.
856 // - Shorter signature is better.
857 // - Alphebatically smaller is better.
858 if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
859 return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
860 if (L.Quality.NumberOfOptionalParameters !=
861 R.Quality.NumberOfOptionalParameters)
862 return L.Quality.NumberOfOptionalParameters <
863 R.Quality.NumberOfOptionalParameters;
864 if (L.Quality.Kind != R.Quality.Kind) {
865 using OC = CodeCompleteConsumer::OverloadCandidate;
866 switch (L.Quality.Kind) {
867 case OC::CK_Function:
868 return true;
869 case OC::CK_FunctionType:
870 return R.Quality.Kind != OC::CK_Function;
871 case OC::CK_FunctionTemplate:
872 return false;
873 }
874 llvm_unreachable("Unknown overload candidate type.");
875 }
876 if (L.Signature.label.size() != R.Signature.label.size())
877 return L.Signature.label.size() < R.Signature.label.size();
878 return L.Signature.label < R.Signature.label;
879 });
880
881 for (auto &SS : ScoredSignatures) {
882 auto IndexDocIt =
883 SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end();
884 if (IndexDocIt != FetchedDocs.end())
885 SS.Signature.documentation = IndexDocIt->second;
886
887 SigHelp.signatures.push_back(std::move(SS.Signature));
888 }
Sam McCall98775c52017-12-04 13:49:59 +0000889 }
890
891 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
892
893 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
894
895private:
Eric Liu63696e12017-12-20 17:24:31 +0000896 // FIXME(ioeric): consider moving CodeCompletionString logic here to
897 // CompletionString.h.
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000898 ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
899 const CodeCompletionString &CCS,
900 llvm::StringRef DocComment) const {
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000901 SignatureInformation Signature;
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000902 SignatureQualitySignals Signal;
Sam McCall98775c52017-12-04 13:49:59 +0000903 const char *ReturnType = nullptr;
904
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000905 Signature.documentation = formatDocumentation(CCS, DocComment);
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000906 Signal.Kind = Candidate.getKind();
Sam McCall98775c52017-12-04 13:49:59 +0000907
908 for (const auto &Chunk : CCS) {
909 switch (Chunk.Kind) {
910 case CodeCompletionString::CK_ResultType:
911 // A piece of text that describes the type of an entity or,
912 // for functions and methods, the return type.
913 assert(!ReturnType && "Unexpected CK_ResultType");
914 ReturnType = Chunk.Text;
915 break;
916 case CodeCompletionString::CK_Placeholder:
917 // A string that acts as a placeholder for, e.g., a function call
918 // argument.
919 // Intentional fallthrough here.
920 case CodeCompletionString::CK_CurrentParameter: {
921 // A piece of text that describes the parameter that corresponds to
922 // the code-completion location within a function call, message send,
923 // macro invocation, etc.
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000924 Signature.label += Chunk.Text;
Sam McCall98775c52017-12-04 13:49:59 +0000925 ParameterInformation Info;
926 Info.label = Chunk.Text;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000927 Signature.parameters.push_back(std::move(Info));
Kadir Cetinkayae486e372018-08-13 08:40:05 +0000928 Signal.NumberOfParameters++;
929 Signal.ContainsActiveParameter = true;
Sam McCall98775c52017-12-04 13:49:59 +0000930 break;
931 }
932 case CodeCompletionString::CK_Optional: {
933 // The rest of the parameters are defaulted/optional.
934 assert(Chunk.Optional &&
935 "Expected the optional code completion string to be non-null.");
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000936 Signature.label += getOptionalParameters(*Chunk.Optional,
937 Signature.parameters, Signal);
Sam McCall98775c52017-12-04 13:49:59 +0000938 break;
939 }
940 case CodeCompletionString::CK_VerticalSpace:
941 break;
942 default:
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000943 Signature.label += Chunk.Text;
Sam McCall98775c52017-12-04 13:49:59 +0000944 break;
945 }
946 }
947 if (ReturnType) {
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000948 Signature.label += " -> ";
949 Signature.label += ReturnType;
Sam McCall98775c52017-12-04 13:49:59 +0000950 }
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000951 dlog("Signal for {0}: {1}", Signature, Signal);
952 ScoredSignature Result;
953 Result.Signature = std::move(Signature);
954 Result.Quality = Signal;
955 Result.IDForDoc =
956 Result.Signature.documentation.empty() && Candidate.getFunction()
957 ? clangd::getSymbolID(Candidate.getFunction())
958 : llvm::None;
959 return Result;
Sam McCall98775c52017-12-04 13:49:59 +0000960 }
961
962 SignatureHelp &SigHelp;
963 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
964 CodeCompletionTUInfo CCTUInfo;
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +0000965 const SymbolIndex *Index;
Sam McCall98775c52017-12-04 13:49:59 +0000966}; // SignatureHelpCollector
967
Sam McCall545a20d2018-01-19 14:34:02 +0000968struct SemaCompleteInput {
969 PathRef FileName;
970 const tooling::CompileCommand &Command;
971 PrecompiledPreamble const *Preamble;
972 StringRef Contents;
973 Position Pos;
974 IntrusiveRefCntPtr<vfs::FileSystem> VFS;
975 std::shared_ptr<PCHContainerOperations> PCHs;
976};
977
978// Invokes Sema code completion on a file.
Sam McCall3f0243f2018-07-03 08:09:29 +0000979// If \p Includes is set, it will be updated based on the compiler invocation.
Sam McCalld1a7a372018-01-31 13:40:48 +0000980bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Sam McCall545a20d2018-01-19 14:34:02 +0000981 const clang::CodeCompleteOptions &Options,
Eric Liu63f419a2018-05-15 15:29:32 +0000982 const SemaCompleteInput &Input,
Sam McCall3f0243f2018-07-03 08:09:29 +0000983 IncludeStructure *Includes = nullptr) {
Ilya Biryukovddf6a332018-03-02 12:28:27 +0000984 trace::Span Tracer("Sema completion");
Sam McCall98775c52017-12-04 13:49:59 +0000985 std::vector<const char *> ArgStrs;
Sam McCall545a20d2018-01-19 14:34:02 +0000986 for (const auto &S : Input.Command.CommandLine)
Sam McCall98775c52017-12-04 13:49:59 +0000987 ArgStrs.push_back(S.c_str());
988
Ilya Biryukova9cf3112018-02-13 17:15:06 +0000989 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
990 log("Couldn't set working directory");
991 // We run parsing anyway, our lit-tests rely on results for non-existing
992 // working dirs.
993 }
Sam McCall98775c52017-12-04 13:49:59 +0000994
995 IgnoreDiagnostics DummyDiagsConsumer;
996 auto CI = createInvocationFromCommandLine(
997 ArgStrs,
998 CompilerInstance::createDiagnostics(new DiagnosticOptions,
999 &DummyDiagsConsumer, false),
Sam McCall545a20d2018-01-19 14:34:02 +00001000 Input.VFS);
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +00001001 if (!CI) {
Sam McCallbed58852018-07-11 10:35:11 +00001002 elog("Couldn't create CompilerInvocation");
Ilya Biryukovb6ad25c2018-02-09 13:51:57 +00001003 return false;
1004 }
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001005 auto &FrontendOpts = CI->getFrontendOpts();
1006 FrontendOpts.DisableFree = false;
Sam McCall98775c52017-12-04 13:49:59 +00001007 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001008 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
1009 // Disable typo correction in Sema.
1010 CI->getLangOpts()->SpellChecking = false;
1011 // Setup code completion.
Sam McCall98775c52017-12-04 13:49:59 +00001012 FrontendOpts.CodeCompleteOpts = Options;
Sam McCall545a20d2018-01-19 14:34:02 +00001013 FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
Sam McCalla4962cc2018-04-27 11:59:28 +00001014 auto Offset = positionToOffset(Input.Contents, Input.Pos);
1015 if (!Offset) {
Sam McCallbed58852018-07-11 10:35:11 +00001016 elog("Code completion position was invalid {0}", Offset.takeError());
Sam McCalla4962cc2018-04-27 11:59:28 +00001017 return false;
1018 }
1019 std::tie(FrontendOpts.CodeCompletionAt.Line,
1020 FrontendOpts.CodeCompletionAt.Column) =
1021 offsetToClangLineColumn(Input.Contents, *Offset);
Sam McCall98775c52017-12-04 13:49:59 +00001022
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001023 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1024 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
1025 // The diagnostic options must be set before creating a CompilerInstance.
1026 CI->getDiagnosticOpts().IgnoreWarnings = true;
1027 // We reuse the preamble whether it's valid or not. This is a
1028 // correctness/performance tradeoff: building without a preamble is slow, and
1029 // completion is latency-sensitive.
1030 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1031 // the remapped buffers do not get freed.
1032 auto Clang = prepareCompilerInstance(
1033 std::move(CI), Input.Preamble, std::move(ContentsBuffer),
1034 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer);
Sam McCall98775c52017-12-04 13:49:59 +00001035 Clang->setCodeCompletionConsumer(Consumer.release());
1036
1037 SyntaxOnlyAction Action;
1038 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Sam McCallbed58852018-07-11 10:35:11 +00001039 log("BeginSourceFile() failed when running codeComplete for {0}",
Sam McCalld1a7a372018-01-31 13:40:48 +00001040 Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +00001041 return false;
1042 }
Sam McCall3f0243f2018-07-03 08:09:29 +00001043 if (Includes)
1044 Clang->getPreprocessor().addPPCallbacks(
1045 collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
Sam McCall98775c52017-12-04 13:49:59 +00001046 if (!Action.Execute()) {
Sam McCallbed58852018-07-11 10:35:11 +00001047 log("Execute() failed when running codeComplete for {0}", Input.FileName);
Sam McCall98775c52017-12-04 13:49:59 +00001048 return false;
1049 }
Sam McCall98775c52017-12-04 13:49:59 +00001050 Action.EndSourceFile();
1051
1052 return true;
1053}
1054
Ilya Biryukova907ba42018-05-14 10:50:04 +00001055// Should we allow index completions in the specified context?
1056bool allowIndex(CodeCompletionContext &CC) {
1057 if (!contextAllowsIndex(CC.getKind()))
1058 return false;
1059 // We also avoid ClassName::bar (but allow namespace::bar).
1060 auto Scope = CC.getCXXScopeSpecifier();
1061 if (!Scope)
1062 return true;
1063 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1064 if (!NameSpec)
1065 return true;
1066 // We only query the index when qualifier is a namespace.
1067 // If it's a class, we rely solely on sema completions.
1068 switch (NameSpec->getKind()) {
1069 case NestedNameSpecifier::Global:
1070 case NestedNameSpecifier::Namespace:
1071 case NestedNameSpecifier::NamespaceAlias:
1072 return true;
1073 case NestedNameSpecifier::Super:
1074 case NestedNameSpecifier::TypeSpec:
1075 case NestedNameSpecifier::TypeSpecWithTemplate:
1076 // Unresolved inside a template.
1077 case NestedNameSpecifier::Identifier:
1078 return false;
1079 }
Ilya Biryukova6556e22018-05-14 11:47:30 +00001080 llvm_unreachable("invalid NestedNameSpecifier kind");
Ilya Biryukova907ba42018-05-14 10:50:04 +00001081}
1082
Eric Liu25d74e92018-08-24 11:23:56 +00001083std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1084 const FuzzyFindRequest &Req) {
1085 return runAsync<SymbolSlab>([&Index, Req]() {
1086 trace::Span Tracer("Async fuzzyFind");
1087 SymbolSlab::Builder Syms;
1088 Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1089 return std::move(Syms).build();
1090 });
1091}
1092
1093// Creates a `FuzzyFindRequest` based on the cached index request from the
1094// last completion, if any, and the speculated completion filter text in the
1095// source code.
1096llvm::Optional<FuzzyFindRequest> speculativeFuzzyFindRequestForCompletion(
1097 FuzzyFindRequest CachedReq, PathRef File, StringRef Content, Position Pos) {
1098 auto Filter = speculateCompletionFilter(Content, Pos);
1099 if (!Filter) {
1100 elog("Failed to speculate filter text for code completion at Pos "
1101 "{0}:{1}: {2}",
1102 Pos.line, Pos.character, Filter.takeError());
1103 return llvm::None;
1104 }
1105 CachedReq.Query = *Filter;
1106 return CachedReq;
1107}
1108
Sam McCall98775c52017-12-04 13:49:59 +00001109} // namespace
1110
1111clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1112 clang::CodeCompleteOptions Result;
1113 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
1114 Result.IncludeMacros = IncludeMacros;
Sam McCalld8169a82018-01-18 15:31:30 +00001115 Result.IncludeGlobals = true;
Ilya Biryukov43714502018-05-16 12:32:44 +00001116 // We choose to include full comments and not do doxygen parsing in
1117 // completion.
1118 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1119 // formatting of the comments.
1120 Result.IncludeBriefComments = false;
Sam McCall98775c52017-12-04 13:49:59 +00001121
Sam McCall3d139c52018-01-12 18:30:08 +00001122 // When an is used, Sema is responsible for completing the main file,
1123 // the index can provide results from the preamble.
1124 // Tell Sema not to deserialize the preamble to look for results.
1125 Result.LoadExternal = !Index;
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001126 Result.IncludeFixIts = IncludeFixIts;
Eric Liu6f648df2017-12-19 16:50:37 +00001127
Sam McCall98775c52017-12-04 13:49:59 +00001128 return Result;
1129}
1130
Sam McCall545a20d2018-01-19 14:34:02 +00001131// Runs Sema-based (AST) and Index-based completion, returns merged results.
1132//
1133// There are a few tricky considerations:
1134// - the AST provides information needed for the index query (e.g. which
1135// namespaces to search in). So Sema must start first.
1136// - we only want to return the top results (Opts.Limit).
1137// Building CompletionItems for everything else is wasteful, so we want to
1138// preserve the "native" format until we're done with scoring.
1139// - the data underlying Sema completion items is owned by the AST and various
1140// other arenas, which must stay alive for us to build CompletionItems.
1141// - we may get duplicate results from Sema and the Index, we need to merge.
1142//
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001143// So we start Sema completion first, and do all our work in its callback.
Sam McCall545a20d2018-01-19 14:34:02 +00001144// We use the Sema context information to query the index.
1145// Then we merge the two result sets, producing items that are Sema/Index/Both.
1146// These items are scored, and the top N are synthesized into the LSP response.
1147// Finally, we can clean up the data structures created by Sema completion.
1148//
1149// Main collaborators are:
1150// - semaCodeComplete sets up the compiler machinery to run code completion.
1151// - CompletionRecorder captures Sema completion results, including context.
1152// - SymbolIndex (Opts.Index) provides index completion results as Symbols
1153// - CompletionCandidates are the result of merging Sema and Index results.
1154// Each candidate points to an underlying CodeCompletionResult (Sema), a
1155// Symbol (Index), or both. It computes the result quality score.
1156// CompletionCandidate also does conversion to CompletionItem (at the end).
1157// - FuzzyMatcher scores how the candidate matches the partial identifier.
1158// This score is combined with the result quality score for the final score.
1159// - TopN determines the results with the best score.
1160class CodeCompleteFlow {
Eric Liuc5105f92018-02-16 14:15:55 +00001161 PathRef FileName;
Sam McCall3f0243f2018-07-03 08:09:29 +00001162 IncludeStructure Includes; // Complete once the compiler runs.
Eric Liu25d74e92018-08-24 11:23:56 +00001163 SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
Sam McCall545a20d2018-01-19 14:34:02 +00001164 const CodeCompleteOptions &Opts;
Eric Liu25d74e92018-08-24 11:23:56 +00001165
Sam McCall545a20d2018-01-19 14:34:02 +00001166 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001167 CompletionRecorder *Recorder = nullptr;
Sam McCall545a20d2018-01-19 14:34:02 +00001168 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
1169 bool Incomplete = false; // Would more be available with a higher limit?
Eric Liu63f419a2018-05-15 15:29:32 +00001170 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
Eric Liubc25ef72018-07-05 08:29:33 +00001171 std::vector<std::string> QueryScopes; // Initialized once Sema runs.
Sam McCall3f0243f2018-07-03 08:09:29 +00001172 // Include-insertion and proximity scoring rely on the include structure.
1173 // This is available after Sema has run.
1174 llvm::Optional<IncludeInserter> Inserter; // Available during runWithSema.
1175 llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
Eric Liu25d74e92018-08-24 11:23:56 +00001176 /// Speculative request based on the cached request and the filter text before
1177 /// the cursor.
1178 /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1179 /// set and contains a cached request.
1180 llvm::Optional<FuzzyFindRequest> SpecReq;
Sam McCall545a20d2018-01-19 14:34:02 +00001181
1182public:
1183 // A CodeCompleteFlow object is only useful for calling run() exactly once.
Sam McCall3f0243f2018-07-03 08:09:29 +00001184 CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
Eric Liu25d74e92018-08-24 11:23:56 +00001185 SpeculativeFuzzyFind *SpecFuzzyFind,
Sam McCall3f0243f2018-07-03 08:09:29 +00001186 const CodeCompleteOptions &Opts)
Eric Liu25d74e92018-08-24 11:23:56 +00001187 : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1188 Opts(Opts) {}
Sam McCall545a20d2018-01-19 14:34:02 +00001189
Sam McCall27c979a2018-06-29 14:47:57 +00001190 CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
Sam McCalld1a7a372018-01-31 13:40:48 +00001191 trace::Span Tracer("CodeCompleteFlow");
Eric Liu25d74e92018-08-24 11:23:56 +00001192 if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) {
1193 assert(!SpecFuzzyFind->Result.valid());
1194 if ((SpecReq = speculativeFuzzyFindRequestForCompletion(
1195 *SpecFuzzyFind->CachedReq, SemaCCInput.FileName,
1196 SemaCCInput.Contents, SemaCCInput.Pos)))
1197 SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1198 }
Eric Liu63f419a2018-05-15 15:29:32 +00001199
Sam McCall545a20d2018-01-19 14:34:02 +00001200 // We run Sema code completion first. It builds an AST and calculates:
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001201 // - completion results based on the AST.
Sam McCall545a20d2018-01-19 14:34:02 +00001202 // - partial identifier and context. We need these for the index query.
Sam McCall27c979a2018-06-29 14:47:57 +00001203 CodeCompleteResult Output;
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001204 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
1205 assert(Recorder && "Recorder is not set");
Sam McCall3f0243f2018-07-03 08:09:29 +00001206 auto Style =
Eric Liu9338a882018-07-03 14:51:23 +00001207 format::getStyle(format::DefaultFormatStyle, SemaCCInput.FileName,
1208 format::DefaultFallbackStyle, SemaCCInput.Contents,
1209 SemaCCInput.VFS.get());
Sam McCall3f0243f2018-07-03 08:09:29 +00001210 if (!Style) {
Sam McCallbed58852018-07-11 10:35:11 +00001211 log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.",
1212 SemaCCInput.FileName, Style.takeError());
Sam McCall3f0243f2018-07-03 08:09:29 +00001213 Style = format::getLLVMStyle();
1214 }
Eric Liu63f419a2018-05-15 15:29:32 +00001215 // If preprocessor was run, inclusions from preprocessor callback should
Sam McCall3f0243f2018-07-03 08:09:29 +00001216 // already be added to Includes.
1217 Inserter.emplace(
1218 SemaCCInput.FileName, SemaCCInput.Contents, *Style,
1219 SemaCCInput.Command.Directory,
1220 Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1221 for (const auto &Inc : Includes.MainFileIncludes)
1222 Inserter->addExisting(Inc);
1223
1224 // Most of the cost of file proximity is in initializing the FileDistance
1225 // structures based on the observed includes, once per query. Conceptually
1226 // that happens here (though the per-URI-scheme initialization is lazy).
1227 // The per-result proximity scoring is (amortized) very cheap.
1228 FileDistanceOptions ProxOpts{}; // Use defaults.
1229 const auto &SM = Recorder->CCSema->getSourceManager();
1230 llvm::StringMap<SourceParams> ProxSources;
1231 for (auto &Entry : Includes.includeDepth(
1232 SM.getFileEntryForID(SM.getMainFileID())->getName())) {
1233 auto &Source = ProxSources[Entry.getKey()];
1234 Source.Cost = Entry.getValue() * ProxOpts.IncludeCost;
1235 // Symbols near our transitive includes are good, but only consider
1236 // things in the same directory or below it. Otherwise there can be
1237 // many false positives.
1238 if (Entry.getValue() > 0)
1239 Source.MaxUpTraversals = 1;
1240 }
1241 FileProximity.emplace(ProxSources, ProxOpts);
1242
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001243 Output = runWithSema();
Sam McCall3f0243f2018-07-03 08:09:29 +00001244 Inserter.reset(); // Make sure this doesn't out-live Clang.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001245 SPAN_ATTACH(Tracer, "sema_completion_kind",
1246 getCompletionKindString(Recorder->CCContext.getKind()));
Sam McCallbed58852018-07-11 10:35:11 +00001247 log("Code complete: sema context {0}, query scopes [{1}]",
Eric Liubc25ef72018-07-05 08:29:33 +00001248 getCompletionKindString(Recorder->CCContext.getKind()),
Sam McCallbed58852018-07-11 10:35:11 +00001249 llvm::join(QueryScopes.begin(), QueryScopes.end(), ","));
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001250 });
1251
1252 Recorder = RecorderOwner.get();
Eric Liu25d74e92018-08-24 11:23:56 +00001253
Sam McCalld1a7a372018-01-31 13:40:48 +00001254 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
Eric Liu63f419a2018-05-15 15:29:32 +00001255 SemaCCInput, &Includes);
Sam McCall545a20d2018-01-19 14:34:02 +00001256
Sam McCall2b780162018-01-30 17:20:54 +00001257 SPAN_ATTACH(Tracer, "sema_results", NSema);
1258 SPAN_ATTACH(Tracer, "index_results", NIndex);
1259 SPAN_ATTACH(Tracer, "merged_results", NBoth);
Sam McCalld20d7982018-07-09 14:25:59 +00001260 SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
Sam McCall27c979a2018-06-29 14:47:57 +00001261 SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
Sam McCallbed58852018-07-11 10:35:11 +00001262 log("Code complete: {0} results from Sema, {1} from Index, "
1263 "{2} matched, {3} returned{4}.",
1264 NSema, NIndex, NBoth, Output.Completions.size(),
1265 Output.HasMore ? " (incomplete)" : "");
Sam McCall27c979a2018-06-29 14:47:57 +00001266 assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
Sam McCall545a20d2018-01-19 14:34:02 +00001267 // We don't assert that isIncomplete means we hit a limit.
1268 // Indexes may choose to impose their own limits even if we don't have one.
1269 return Output;
1270 }
1271
1272private:
1273 // This is called by run() once Sema code completion is done, but before the
1274 // Sema data structures are torn down. It does all the real work.
Sam McCall27c979a2018-06-29 14:47:57 +00001275 CodeCompleteResult runWithSema() {
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001276 const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1277 Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1278 Range TextEditRange;
1279 // When we are getting completions with an empty identifier, for example
1280 // std::vector<int> asdf;
1281 // asdf.^;
1282 // Then the range will be invalid and we will be doing insertion, use
1283 // current cursor position in such cases as range.
1284 if (CodeCompletionRange.isValid()) {
1285 TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1286 CodeCompletionRange);
1287 } else {
1288 const auto &Pos = sourceLocToPosition(
1289 Recorder->CCSema->getSourceManager(),
1290 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1291 TextEditRange.start = TextEditRange.end = Pos;
1292 }
Sam McCall545a20d2018-01-19 14:34:02 +00001293 Filter = FuzzyMatcher(
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001294 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
Eric Liubc25ef72018-07-05 08:29:33 +00001295 QueryScopes = getQueryScopes(Recorder->CCContext,
1296 Recorder->CCSema->getSourceManager());
Sam McCall545a20d2018-01-19 14:34:02 +00001297 // Sema provides the needed context to query the index.
1298 // FIXME: in addition to querying for extra/overlapping symbols, we should
1299 // explicitly request symbols corresponding to Sema results.
1300 // We can use their signals even if the index can't suggest them.
1301 // We must copy index results to preserve them, but there are at most Limit.
Eric Liu8f3678d2018-06-15 13:34:18 +00001302 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1303 ? queryIndex()
1304 : SymbolSlab();
Eric Liu25d74e92018-08-24 11:23:56 +00001305 trace::Span Tracer("Populate CodeCompleteResult");
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001306 // Merge Sema, Index and Override results, score them, and pick the
1307 // winners.
1308 const auto Overrides = getNonOverridenMethodCompletionResults(
1309 Recorder->CCSema->CurContext, Recorder->CCSema);
1310 auto Top = mergeResults(Recorder->Results, IndexResults, Overrides);
Sam McCall27c979a2018-06-29 14:47:57 +00001311 CodeCompleteResult Output;
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001312
1313 // Convert the results to final form, assembling the expensive strings.
Sam McCall27c979a2018-06-29 14:47:57 +00001314 for (auto &C : Top) {
1315 Output.Completions.push_back(toCodeCompletion(C.first));
1316 Output.Completions.back().Score = C.second;
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001317 Output.Completions.back().CompletionTokenRange = TextEditRange;
Sam McCall27c979a2018-06-29 14:47:57 +00001318 }
1319 Output.HasMore = Incomplete;
Eric Liu5d2a8072018-07-23 10:56:37 +00001320 Output.Context = Recorder->CCContext.getKind();
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001321
Sam McCall545a20d2018-01-19 14:34:02 +00001322 return Output;
1323 }
1324
1325 SymbolSlab queryIndex() {
Sam McCalld1a7a372018-01-31 13:40:48 +00001326 trace::Span Tracer("Query index");
Sam McCalld20d7982018-07-09 14:25:59 +00001327 SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
Sam McCall2b780162018-01-30 17:20:54 +00001328
Sam McCall545a20d2018-01-19 14:34:02 +00001329 // Build the query.
1330 FuzzyFindRequest Req;
Haojian Wu48b48652018-01-25 09:20:09 +00001331 if (Opts.Limit)
1332 Req.MaxCandidateCount = Opts.Limit;
Sam McCall545a20d2018-01-19 14:34:02 +00001333 Req.Query = Filter->pattern();
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001334 Req.RestrictForCodeCompletion = true;
Eric Liubc25ef72018-07-05 08:29:33 +00001335 Req.Scopes = QueryScopes;
Sam McCall3f0243f2018-07-03 08:09:29 +00001336 // FIXME: we should send multiple weighted paths here.
Eric Liu6de95ec2018-06-12 08:48:20 +00001337 Req.ProximityPaths.push_back(FileName);
Sam McCallbed58852018-07-11 10:35:11 +00001338 vlog("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])", Req.Query,
1339 llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ","));
Eric Liu25d74e92018-08-24 11:23:56 +00001340
1341 if (SpecFuzzyFind)
1342 SpecFuzzyFind->NewReq = Req;
1343 if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1344 vlog("Code complete: speculative fuzzy request matches the actual index "
1345 "request. Waiting for the speculative index results.");
1346 SPAN_ATTACH(Tracer, "Speculative results", true);
1347
1348 trace::Span WaitSpec("Wait speculative results");
1349 return SpecFuzzyFind->Result.get();
1350 }
1351
1352 SPAN_ATTACH(Tracer, "Speculative results", false);
1353
Sam McCall545a20d2018-01-19 14:34:02 +00001354 // Run the query against the index.
Eric Liu25d74e92018-08-24 11:23:56 +00001355 SymbolSlab::Builder ResultsBuilder;
Sam McCallab8e3932018-02-19 13:04:41 +00001356 if (Opts.Index->fuzzyFind(
1357 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1358 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001359 return std::move(ResultsBuilder).build();
1360 }
1361
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001362 // Merges Sema, Index and Override results where possible, to form
1363 // CompletionCandidates. Groups overloads if desired, to form
1364 // CompletionCandidate::Bundles. The bundles are scored and top results are
1365 // returned, best to worst.
Sam McCallc18c2802018-06-15 11:06:29 +00001366 std::vector<ScoredBundle>
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001367 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1368 const SymbolSlab &IndexResults,
1369 const std::vector<CodeCompletionResult> &OverrideResults) {
Sam McCalld1a7a372018-01-31 13:40:48 +00001370 trace::Span Tracer("Merge and score results");
Sam McCallc18c2802018-06-15 11:06:29 +00001371 std::vector<CompletionCandidate::Bundle> Bundles;
1372 llvm::DenseMap<size_t, size_t> BundleLookup;
1373 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001374 const Symbol *IndexResult,
1375 bool IsOverride = false) {
Sam McCallc18c2802018-06-15 11:06:29 +00001376 CompletionCandidate C;
1377 C.SemaResult = SemaResult;
1378 C.IndexResult = IndexResult;
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001379 C.IsOverride = IsOverride;
Sam McCallc18c2802018-06-15 11:06:29 +00001380 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1381 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1382 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1383 if (Ret.second)
1384 Bundles.emplace_back();
1385 Bundles[Ret.first->second].push_back(std::move(C));
1386 } else {
1387 Bundles.emplace_back();
1388 Bundles.back().push_back(std::move(C));
1389 }
1390 };
Sam McCall545a20d2018-01-19 14:34:02 +00001391 llvm::DenseSet<const Symbol *> UsedIndexResults;
1392 auto CorrespondingIndexResult =
1393 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1394 if (auto SymID = getSymbolID(SemaResult)) {
1395 auto I = IndexResults.find(*SymID);
1396 if (I != IndexResults.end()) {
1397 UsedIndexResults.insert(&*I);
1398 return &*I;
1399 }
1400 }
1401 return nullptr;
1402 };
1403 // Emit all Sema results, merging them with Index results if possible.
Ilya Biryukovddf6a332018-03-02 12:28:27 +00001404 for (auto &SemaResult : Recorder->Results)
Sam McCallc18c2802018-06-15 11:06:29 +00001405 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
Kadir Cetinkayaf8b85a32018-08-23 13:14:50 +00001406 // Handle OverrideResults the same way we deal with SemaResults. Since these
1407 // results use the same structs as a SemaResult it is safe to do that, but
1408 // we need to make sure we dont' duplicate things in future if Sema starts
1409 // to provide them as well.
1410 for (auto &OverrideResult : OverrideResults)
1411 AddToBundles(&OverrideResult, CorrespondingIndexResult(OverrideResult),
1412 true);
Sam McCall545a20d2018-01-19 14:34:02 +00001413 // Now emit any Index-only results.
1414 for (const auto &IndexResult : IndexResults) {
1415 if (UsedIndexResults.count(&IndexResult))
1416 continue;
Sam McCallc18c2802018-06-15 11:06:29 +00001417 AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
Sam McCall545a20d2018-01-19 14:34:02 +00001418 }
Sam McCallc18c2802018-06-15 11:06:29 +00001419 // We only keep the best N results at any time, in "native" format.
1420 TopN<ScoredBundle, ScoredBundleGreater> Top(
1421 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1422 for (auto &Bundle : Bundles)
1423 addCandidate(Top, std::move(Bundle));
Sam McCall545a20d2018-01-19 14:34:02 +00001424 return std::move(Top).items();
1425 }
1426
Sam McCall80ad7072018-06-08 13:32:25 +00001427 Optional<float> fuzzyScore(const CompletionCandidate &C) {
1428 // Macros can be very spammy, so we only support prefix completion.
1429 // We won't end up with underfull index results, as macros are sema-only.
1430 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1431 !C.Name.startswith_lower(Filter->pattern()))
1432 return None;
1433 return Filter->match(C.Name);
1434 }
1435
Sam McCall545a20d2018-01-19 14:34:02 +00001436 // Scores a candidate and adds it to the TopN structure.
Sam McCallc18c2802018-06-15 11:06:29 +00001437 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1438 CompletionCandidate::Bundle Bundle) {
Sam McCallc5707b62018-05-15 17:43:27 +00001439 SymbolQualitySignals Quality;
1440 SymbolRelevanceSignals Relevance;
Eric Liu5d2a8072018-07-23 10:56:37 +00001441 Relevance.Context = Recorder->CCContext.getKind();
Sam McCalld9b54f02018-06-05 16:30:25 +00001442 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
Sam McCallf84dd022018-07-05 08:26:53 +00001443 Relevance.FileProximityMatch = FileProximity.getPointer();
Sam McCallc18c2802018-06-15 11:06:29 +00001444 auto &First = Bundle.front();
1445 if (auto FuzzyScore = fuzzyScore(First))
Sam McCallc5707b62018-05-15 17:43:27 +00001446 Relevance.NameMatch = *FuzzyScore;
Sam McCall545a20d2018-01-19 14:34:02 +00001447 else
1448 return;
Sam McCall2161ec72018-07-05 06:20:41 +00001449 SymbolOrigin Origin = SymbolOrigin::Unknown;
Sam McCall4e5742a2018-07-06 11:50:49 +00001450 bool FromIndex = false;
Sam McCallc18c2802018-06-15 11:06:29 +00001451 for (const auto &Candidate : Bundle) {
1452 if (Candidate.IndexResult) {
1453 Quality.merge(*Candidate.IndexResult);
1454 Relevance.merge(*Candidate.IndexResult);
Sam McCall4e5742a2018-07-06 11:50:49 +00001455 Origin |= Candidate.IndexResult->Origin;
1456 FromIndex = true;
Sam McCallc18c2802018-06-15 11:06:29 +00001457 }
1458 if (Candidate.SemaResult) {
1459 Quality.merge(*Candidate.SemaResult);
1460 Relevance.merge(*Candidate.SemaResult);
Sam McCall4e5742a2018-07-06 11:50:49 +00001461 Origin |= SymbolOrigin::AST;
Sam McCallc18c2802018-06-15 11:06:29 +00001462 }
Sam McCallc5707b62018-05-15 17:43:27 +00001463 }
1464
Sam McCall27c979a2018-06-29 14:47:57 +00001465 CodeCompletion::Scores Scores;
1466 Scores.Quality = Quality.evaluate();
1467 Scores.Relevance = Relevance.evaluate();
1468 Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1469 // NameMatch is in fact a multiplier on total score, so rescoring is sound.
1470 Scores.ExcludingName = Relevance.NameMatch
1471 ? Scores.Total / Relevance.NameMatch
1472 : Scores.Quality;
Sam McCallc5707b62018-05-15 17:43:27 +00001473
Sam McCallbed58852018-07-11 10:35:11 +00001474 dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1475 llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1476 llvm::to_string(Relevance));
Sam McCall545a20d2018-01-19 14:34:02 +00001477
Sam McCall2161ec72018-07-05 06:20:41 +00001478 NSema += bool(Origin & SymbolOrigin::AST);
Sam McCall4e5742a2018-07-06 11:50:49 +00001479 NIndex += FromIndex;
1480 NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex;
Sam McCallc18c2802018-06-15 11:06:29 +00001481 if (Candidates.push({std::move(Bundle), Scores}))
Sam McCallab8e3932018-02-19 13:04:41 +00001482 Incomplete = true;
Sam McCall545a20d2018-01-19 14:34:02 +00001483 }
1484
Sam McCall27c979a2018-06-29 14:47:57 +00001485 CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1486 llvm::Optional<CodeCompletionBuilder> Builder;
1487 for (const auto &Item : Bundle) {
1488 CodeCompletionString *SemaCCS =
1489 Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1490 : nullptr;
1491 if (!Builder)
1492 Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS,
Sam McCall3f0243f2018-07-03 08:09:29 +00001493 *Inserter, FileName, Opts);
Sam McCall27c979a2018-06-29 14:47:57 +00001494 else
1495 Builder->add(Item, SemaCCS);
Ilya Biryukov43714502018-05-16 12:32:44 +00001496 }
Sam McCall27c979a2018-06-29 14:47:57 +00001497 return Builder->build();
Sam McCall545a20d2018-01-19 14:34:02 +00001498 }
1499};
1500
Eric Liu25d74e92018-08-24 11:23:56 +00001501llvm::Expected<llvm::StringRef>
1502speculateCompletionFilter(llvm::StringRef Content, Position Pos) {
1503 auto Offset = positionToOffset(Content, Pos);
1504 if (!Offset)
1505 return llvm::make_error<llvm::StringError>(
1506 "Failed to convert position to offset in content.",
1507 llvm::inconvertibleErrorCode());
1508 if (*Offset == 0)
1509 return "";
1510
1511 // Start from the character before the cursor.
1512 int St = *Offset - 1;
1513 // FIXME(ioeric): consider UTF characters?
1514 auto IsValidIdentifierChar = [](char c) {
1515 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
1516 (c >= '0' && c <= '9') || (c == '_'));
1517 };
1518 size_t Len = 0;
1519 for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) {
1520 }
1521 if (Len > 0)
1522 St++; // Shift to the first valid character.
1523 return Content.substr(St, Len);
1524}
1525
1526CodeCompleteResult
1527codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
1528 PrecompiledPreamble const *Preamble,
1529 const IncludeStructure &PreambleInclusions, StringRef Contents,
1530 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1531 std::shared_ptr<PCHContainerOperations> PCHs,
1532 CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) {
1533 return CodeCompleteFlow(FileName, PreambleInclusions, SpecFuzzyFind, Opts)
Sam McCall3f0243f2018-07-03 08:09:29 +00001534 .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs});
Sam McCall98775c52017-12-04 13:49:59 +00001535}
1536
Sam McCalld1a7a372018-01-31 13:40:48 +00001537SignatureHelp signatureHelp(PathRef FileName,
Ilya Biryukov940901e2017-12-13 12:51:22 +00001538 const tooling::CompileCommand &Command,
1539 PrecompiledPreamble const *Preamble,
1540 StringRef Contents, Position Pos,
1541 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001542 std::shared_ptr<PCHContainerOperations> PCHs,
1543 SymbolIndex *Index) {
Sam McCall98775c52017-12-04 13:49:59 +00001544 SignatureHelp Result;
1545 clang::CodeCompleteOptions Options;
1546 Options.IncludeGlobals = false;
1547 Options.IncludeMacros = false;
1548 Options.IncludeCodePatterns = false;
Ilya Biryukov43714502018-05-16 12:32:44 +00001549 Options.IncludeBriefComments = false;
Sam McCall3f0243f2018-07-03 08:09:29 +00001550 IncludeStructure PreambleInclusions; // Unused for signatureHelp
Ilya Biryukov8a0f76b2018-08-17 09:32:30 +00001551 semaCodeComplete(
1552 llvm::make_unique<SignatureHelpCollector>(Options, Index, Result),
1553 Options,
1554 {FileName, Command, Preamble, Contents, Pos, std::move(VFS),
1555 std::move(PCHs)});
Sam McCall98775c52017-12-04 13:49:59 +00001556 return Result;
1557}
1558
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +00001559bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1560 using namespace clang::ast_matchers;
1561 auto InTopLevelScope = hasDeclContext(
1562 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1563 return !match(decl(anyOf(InTopLevelScope,
1564 hasDeclContext(
1565 enumDecl(InTopLevelScope, unless(isScoped()))))),
1566 ND, ASTCtx)
1567 .empty();
1568}
1569
Sam McCall27c979a2018-06-29 14:47:57 +00001570CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
1571 CompletionItem LSP;
1572 LSP.label = (HeaderInsertion ? Opts.IncludeIndicator.Insert
1573 : Opts.IncludeIndicator.NoInsert) +
Sam McCall2161ec72018-07-05 06:20:41 +00001574 (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
Sam McCall27c979a2018-06-29 14:47:57 +00001575 RequiredQualifier + Name + Signature;
Sam McCall2161ec72018-07-05 06:20:41 +00001576
Sam McCall27c979a2018-06-29 14:47:57 +00001577 LSP.kind = Kind;
1578 LSP.detail = BundleSize > 1 ? llvm::formatv("[{0} overloads]", BundleSize)
1579 : ReturnType;
1580 if (!Header.empty())
1581 LSP.detail += "\n" + Header;
1582 LSP.documentation = Documentation;
1583 LSP.sortText = sortText(Score.Total, Name);
1584 LSP.filterText = Name;
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001585 LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
1586 // Merge continious additionalTextEdits into main edit. The main motivation
1587 // behind this is to help LSP clients, it seems most of them are confused when
1588 // they are provided with additionalTextEdits that are consecutive to main
1589 // edit.
1590 // Note that we store additional text edits from back to front in a line. That
1591 // is mainly to help LSP clients again, so that changes do not effect each
1592 // other.
1593 for (const auto &FixIt : FixIts) {
1594 if (IsRangeConsecutive(FixIt.range, LSP.textEdit->range)) {
1595 LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
1596 LSP.textEdit->range.start = FixIt.range.start;
1597 } else {
1598 LSP.additionalTextEdits.push_back(FixIt);
1599 }
1600 }
Kadir Cetinkaya516fcda2018-08-23 12:19:39 +00001601 if (Opts.EnableSnippets)
1602 LSP.textEdit->newText += SnippetSuffix;
Kadir Cetinkaya6c9f15c2018-08-17 15:42:54 +00001603
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +00001604 // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
1605 // compatible with most of the editors.
1606 LSP.insertText = LSP.textEdit->newText;
Sam McCall27c979a2018-06-29 14:47:57 +00001607 LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
1608 : InsertTextFormat::PlainText;
1609 if (HeaderInsertion)
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +00001610 LSP.additionalTextEdits.push_back(*HeaderInsertion);
Sam McCall27c979a2018-06-29 14:47:57 +00001611 return LSP;
1612}
1613
Sam McCalle746a2b2018-07-02 11:13:16 +00001614raw_ostream &operator<<(raw_ostream &OS, const CodeCompletion &C) {
1615 // For now just lean on CompletionItem.
1616 return OS << C.render(CodeCompleteOptions());
1617}
1618
1619raw_ostream &operator<<(raw_ostream &OS, const CodeCompleteResult &R) {
1620 OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
Eric Liu5d2a8072018-07-23 10:56:37 +00001621 << " (" << getCompletionKindString(R.Context) << ")"
Sam McCalle746a2b2018-07-02 11:13:16 +00001622 << " items:\n";
1623 for (const auto &C : R.Completions)
1624 OS << C << "\n";
1625 return OS;
1626}
1627
Sam McCall98775c52017-12-04 13:49:59 +00001628} // namespace clangd
1629} // namespace clang