Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- CodeComplete.cpp ----------------------------------------*- C++-*-===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 2 | // |
| 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 Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 9 | // |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 10 | // 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 McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 15 | // |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 16 | // 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 McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 18 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 20 | |
| 21 | #include "CodeComplete.h" |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 22 | #include "AST.h" |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 23 | #include "ClangdUnit.h" |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 24 | #include "CodeCompletionStrings.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 25 | #include "Compiler.h" |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 26 | #include "Diagnostics.h" |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 27 | #include "ExpectedTypes.h" |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 28 | #include "FileDistance.h" |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 29 | #include "FuzzyMatch.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 30 | #include "Headers.h" |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 31 | #include "Logger.h" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 32 | #include "Quality.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 33 | #include "SourceCode.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 34 | #include "TUScheduler.h" |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 35 | #include "Trace.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 36 | #include "URI.h" |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 37 | #include "index/Index.h" |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 38 | #include "clang/AST/Decl.h" |
| 39 | #include "clang/AST/DeclBase.h" |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 40 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 41 | #include "clang/Basic/LangOptions.h" |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 42 | #include "clang/Basic/SourceLocation.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 43 | #include "clang/Format/Format.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 44 | #include "clang/Frontend/CompilerInstance.h" |
| 45 | #include "clang/Frontend/FrontendActions.h" |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 46 | #include "clang/Lex/PreprocessorOptions.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 47 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 48 | #include "clang/Sema/Sema.h" |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/ArrayRef.h" |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/None.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/Optional.h" |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/SmallVector.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Error.h" |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Format.h" |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 55 | #include "llvm/Support/FormatVariadic.h" |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ScopedPrinter.h" |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 57 | #include <algorithm> |
| 58 | #include <iterator> |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 59 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 60 | // We log detailed candidate here if you run with -debug-only=codecomplete. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "CodeComplete" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 62 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 63 | using namespace llvm; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 64 | namespace clang { |
| 65 | namespace clangd { |
| 66 | namespace { |
| 67 | |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 68 | CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) { |
| 69 | using SK = index::SymbolKind; |
| 70 | switch (Kind) { |
| 71 | case SK::Unknown: |
| 72 | return CompletionItemKind::Missing; |
| 73 | case SK::Module: |
| 74 | case SK::Namespace: |
| 75 | case SK::NamespaceAlias: |
| 76 | return CompletionItemKind::Module; |
| 77 | case SK::Macro: |
| 78 | return CompletionItemKind::Text; |
| 79 | case SK::Enum: |
| 80 | return CompletionItemKind::Enum; |
| 81 | // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the |
| 82 | // protocol. |
| 83 | case SK::Struct: |
| 84 | case SK::Class: |
| 85 | case SK::Protocol: |
| 86 | case SK::Extension: |
| 87 | case SK::Union: |
| 88 | return CompletionItemKind::Class; |
| 89 | // FIXME(ioeric): figure out whether reference is the right type for aliases. |
| 90 | case SK::TypeAlias: |
| 91 | case SK::Using: |
| 92 | return CompletionItemKind::Reference; |
| 93 | case SK::Function: |
| 94 | // FIXME(ioeric): this should probably be an operator. This should be fixed |
| 95 | // when `Operator` is support type in the protocol. |
| 96 | case SK::ConversionFunction: |
| 97 | return CompletionItemKind::Function; |
| 98 | case SK::Variable: |
| 99 | case SK::Parameter: |
| 100 | return CompletionItemKind::Variable; |
| 101 | case SK::Field: |
| 102 | return CompletionItemKind::Field; |
| 103 | // FIXME(ioeric): use LSP enum constant when it is supported in the protocol. |
| 104 | case SK::EnumConstant: |
| 105 | return CompletionItemKind::Value; |
| 106 | case SK::InstanceMethod: |
| 107 | case SK::ClassMethod: |
| 108 | case SK::StaticMethod: |
| 109 | case SK::Destructor: |
| 110 | return CompletionItemKind::Method; |
| 111 | case SK::InstanceProperty: |
| 112 | case SK::ClassProperty: |
| 113 | case SK::StaticProperty: |
| 114 | return CompletionItemKind::Property; |
| 115 | case SK::Constructor: |
| 116 | return CompletionItemKind::Constructor; |
| 117 | } |
| 118 | llvm_unreachable("Unhandled clang::index::SymbolKind."); |
| 119 | } |
| 120 | |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 121 | CompletionItemKind |
| 122 | toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 123 | const NamedDecl *Decl, |
| 124 | CodeCompletionContext::Kind CtxKind) { |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 125 | if (Decl) |
| 126 | return toCompletionItemKind(index::getSymbolInfo(Decl).Kind); |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 127 | if (CtxKind == CodeCompletionContext::CCC_IncludedFile) |
| 128 | return CompletionItemKind::File; |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 129 | switch (ResKind) { |
| 130 | case CodeCompletionResult::RK_Declaration: |
| 131 | llvm_unreachable("RK_Declaration without Decl"); |
| 132 | case CodeCompletionResult::RK_Keyword: |
| 133 | return CompletionItemKind::Keyword; |
| 134 | case CodeCompletionResult::RK_Macro: |
| 135 | return CompletionItemKind::Text; // unfortunately, there's no 'Macro' |
| 136 | // completion items in LSP. |
| 137 | case CodeCompletionResult::RK_Pattern: |
| 138 | return CompletionItemKind::Snippet; |
| 139 | } |
| 140 | llvm_unreachable("Unhandled CodeCompletionResult::ResultKind."); |
| 141 | } |
| 142 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 143 | /// Get the optional chunk as a string. This function is possibly recursive. |
| 144 | /// |
| 145 | /// The parameter info for each parameter is appended to the Parameters. |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 146 | std::string getOptionalParameters(const CodeCompletionString &CCS, |
| 147 | std::vector<ParameterInformation> &Parameters, |
| 148 | SignatureQualitySignals &Signal) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 149 | std::string Result; |
| 150 | for (const auto &Chunk : CCS) { |
| 151 | switch (Chunk.Kind) { |
| 152 | case CodeCompletionString::CK_Optional: |
| 153 | assert(Chunk.Optional && |
| 154 | "Expected the optional code completion string to be non-null."); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 155 | Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 156 | break; |
| 157 | case CodeCompletionString::CK_VerticalSpace: |
| 158 | break; |
| 159 | case CodeCompletionString::CK_Placeholder: |
| 160 | // A string that acts as a placeholder for, e.g., a function call |
| 161 | // argument. |
| 162 | // Intentional fallthrough here. |
| 163 | case CodeCompletionString::CK_CurrentParameter: { |
| 164 | // A piece of text that describes the parameter that corresponds to |
| 165 | // the code-completion location within a function call, message send, |
| 166 | // macro invocation, etc. |
| 167 | Result += Chunk.Text; |
| 168 | ParameterInformation Info; |
| 169 | Info.label = Chunk.Text; |
| 170 | Parameters.push_back(std::move(Info)); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 171 | Signal.ContainsActiveParameter = true; |
| 172 | Signal.NumberOfOptionalParameters++; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | default: |
| 176 | Result += Chunk.Text; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | return Result; |
| 181 | } |
| 182 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 183 | /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal |
| 184 | /// include. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 185 | static Expected<HeaderFile> toHeaderFile(StringRef Header, StringRef HintPath) { |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 186 | if (isLiteralInclude(Header)) |
| 187 | return HeaderFile{Header.str(), /*Verbatim=*/true}; |
| 188 | auto U = URI::parse(Header); |
| 189 | if (!U) |
| 190 | return U.takeError(); |
| 191 | |
| 192 | auto IncludePath = URI::includeSpelling(*U); |
| 193 | if (!IncludePath) |
| 194 | return IncludePath.takeError(); |
| 195 | if (!IncludePath->empty()) |
| 196 | return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true}; |
| 197 | |
| 198 | auto Resolved = URI::resolve(*U, HintPath); |
| 199 | if (!Resolved) |
| 200 | return Resolved.takeError(); |
| 201 | return HeaderFile{std::move(*Resolved), /*Verbatim=*/false}; |
| 202 | } |
| 203 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 204 | /// A code completion result, in clang-native form. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 205 | /// It may be promoted to a CompletionItem if it's among the top-ranked results. |
| 206 | struct CompletionCandidate { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 207 | StringRef Name; // Used for filtering and sorting. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 208 | // We may have a result from Sema, from the index, or both. |
| 209 | const CodeCompletionResult *SemaResult = nullptr; |
| 210 | const Symbol *IndexResult = nullptr; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 211 | SmallVector<StringRef, 1> RankedIncludeHeaders; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 212 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 213 | // Returns a token identifying the overload set this is part of. |
| 214 | // 0 indicates it's not part of any overload set. |
| 215 | size_t overloadSet() const { |
| 216 | SmallString<256> Scratch; |
| 217 | if (IndexResult) { |
| 218 | switch (IndexResult->SymInfo.Kind) { |
| 219 | case index::SymbolKind::ClassMethod: |
| 220 | case index::SymbolKind::InstanceMethod: |
| 221 | case index::SymbolKind::StaticMethod: |
Fangrui Song | 12e4ee7 | 2018-11-02 05:59:29 +0000 | [diff] [blame] | 222 | #ifndef NDEBUG |
| 223 | llvm_unreachable("Don't expect members from index in code completion"); |
| 224 | #else |
Fangrui Song | a3ed05b | 2018-11-02 04:23:50 +0000 | [diff] [blame] | 225 | LLVM_FALLTHROUGH; |
Fangrui Song | 12e4ee7 | 2018-11-02 05:59:29 +0000 | [diff] [blame] | 226 | #endif |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 227 | case index::SymbolKind::Function: |
| 228 | // We can't group overloads together that need different #includes. |
| 229 | // This could break #include insertion. |
| 230 | return hash_combine( |
| 231 | (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch), |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 232 | headerToInsertIfAllowed().getValueOr("")); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 233 | default: |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | assert(SemaResult); |
| 238 | // We need to make sure we're consistent with the IndexResult case! |
| 239 | const NamedDecl *D = SemaResult->Declaration; |
| 240 | if (!D || !D->isFunctionOrFunctionTemplate()) |
| 241 | return 0; |
| 242 | { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 243 | raw_svector_ostream OS(Scratch); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 244 | D->printQualifiedName(OS); |
| 245 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 246 | return hash_combine(Scratch, headerToInsertIfAllowed().getValueOr("")); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 249 | // The best header to include if include insertion is allowed. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 250 | Optional<StringRef> headerToInsertIfAllowed() const { |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 251 | if (RankedIncludeHeaders.empty()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 252 | return None; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 253 | if (SemaResult && SemaResult->Declaration) { |
| 254 | // Avoid inserting new #include if the declaration is found in the current |
| 255 | // file e.g. the symbol is forward declared. |
| 256 | auto &SM = SemaResult->Declaration->getASTContext().getSourceManager(); |
| 257 | for (const Decl *RD : SemaResult->Declaration->redecls()) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 258 | if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc()))) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 259 | return None; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 260 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 261 | return RankedIncludeHeaders[0]; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 264 | using Bundle = SmallVector<CompletionCandidate, 4>; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 265 | }; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 266 | using ScoredBundle = |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 267 | std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 268 | struct ScoredBundleGreater { |
| 269 | bool operator()(const ScoredBundle &L, const ScoredBundle &R) { |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 270 | if (L.second.Total != R.second.Total) |
| 271 | return L.second.Total > R.second.Total; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 272 | return L.first.front().Name < |
| 273 | R.first.front().Name; // Earlier name is better. |
| 274 | } |
| 275 | }; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 276 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 277 | // Assembles a code completion out of a bundle of >=1 completion candidates. |
| 278 | // Many of the expensive strings are only computed at this point, once we know |
| 279 | // the candidate bundle is going to be returned. |
| 280 | // |
| 281 | // Many fields are the same for all candidates in a bundle (e.g. name), and are |
| 282 | // computed from the first candidate, in the constructor. |
| 283 | // Others vary per candidate, so add() must be called for remaining candidates. |
| 284 | struct CodeCompletionBuilder { |
| 285 | CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C, |
| 286 | CodeCompletionString *SemaCCS, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 287 | ArrayRef<std::string> QueryScopes, |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 288 | const IncludeInserter &Includes, StringRef FileName, |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 289 | CodeCompletionContext::Kind ContextKind, |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 290 | const CodeCompleteOptions &Opts) |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 291 | : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments), |
| 292 | EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) { |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 293 | add(C, SemaCCS); |
| 294 | if (C.SemaResult) { |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 295 | Completion.Origin |= SymbolOrigin::AST; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 296 | Completion.Name = StringRef(SemaCCS->getTypedText()); |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame] | 297 | if (Completion.Scope.empty()) { |
| 298 | if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) || |
| 299 | (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern)) |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 300 | if (const auto *D = C.SemaResult->getDeclaration()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 301 | if (const auto *ND = dyn_cast<NamedDecl>(D)) |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 302 | Completion.Scope = |
| 303 | splitQualifiedName(printQualifiedName(*ND)).first; |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame] | 304 | } |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 305 | Completion.Kind = toCompletionItemKind( |
| 306 | C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind); |
Kadir Cetinkaya | 0ed5d29 | 2018-09-27 14:21:07 +0000 | [diff] [blame] | 307 | // Sema could provide more info on whether the completion was a file or |
| 308 | // folder. |
| 309 | if (Completion.Kind == CompletionItemKind::File && |
| 310 | Completion.Name.back() == '/') |
| 311 | Completion.Kind = CompletionItemKind::Folder; |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 312 | for (const auto &FixIt : C.SemaResult->FixIts) { |
| 313 | Completion.FixIts.push_back( |
| 314 | toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts())); |
| 315 | } |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 316 | llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) { |
| 317 | return std::tie(X.range.start.line, X.range.start.character) < |
| 318 | std::tie(Y.range.start.line, Y.range.start.character); |
| 319 | }); |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 320 | Completion.Deprecated |= |
| 321 | (C.SemaResult->Availability == CXAvailability_Deprecated); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 322 | } |
| 323 | if (C.IndexResult) { |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 324 | Completion.Origin |= C.IndexResult->Origin; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 325 | if (Completion.Scope.empty()) |
| 326 | Completion.Scope = C.IndexResult->Scope; |
| 327 | if (Completion.Kind == CompletionItemKind::Missing) |
| 328 | Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind); |
| 329 | if (Completion.Name.empty()) |
| 330 | Completion.Name = C.IndexResult->Name; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 331 | // If the completion was visible to Sema, no qualifier is needed. This |
| 332 | // avoids unneeded qualifiers in cases like with `using ns::X`. |
| 333 | if (Completion.RequiredQualifier.empty() && !C.SemaResult) { |
| 334 | StringRef ShortestQualifier = C.IndexResult->Scope; |
| 335 | for (StringRef Scope : QueryScopes) { |
| 336 | StringRef Qualifier = C.IndexResult->Scope; |
| 337 | if (Qualifier.consume_front(Scope) && |
| 338 | Qualifier.size() < ShortestQualifier.size()) |
| 339 | ShortestQualifier = Qualifier; |
| 340 | } |
| 341 | Completion.RequiredQualifier = ShortestQualifier; |
| 342 | } |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 343 | Completion.Deprecated |= (C.IndexResult->Flags & Symbol::Deprecated); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 344 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 345 | |
| 346 | // Turn absolute path into a literal string that can be #included. |
| 347 | auto Inserted = |
| 348 | [&](StringRef Header) -> Expected<std::pair<std::string, bool>> { |
| 349 | auto ResolvedDeclaring = |
| 350 | toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName); |
| 351 | if (!ResolvedDeclaring) |
| 352 | return ResolvedDeclaring.takeError(); |
| 353 | auto ResolvedInserted = toHeaderFile(Header, FileName); |
| 354 | if (!ResolvedInserted) |
| 355 | return ResolvedInserted.takeError(); |
| 356 | return std::make_pair( |
| 357 | Includes.calculateIncludePath(*ResolvedDeclaring, *ResolvedInserted), |
| 358 | Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted)); |
| 359 | }; |
| 360 | bool ShouldInsert = C.headerToInsertIfAllowed().hasValue(); |
| 361 | // Calculate include paths and edits for all possible headers. |
| 362 | for (const auto &Inc : C.RankedIncludeHeaders) { |
| 363 | if (auto ToInclude = Inserted(Inc)) { |
| 364 | CodeCompletion::IncludeCandidate Include; |
| 365 | Include.Header = ToInclude->first; |
| 366 | if (ToInclude->second && ShouldInsert) |
| 367 | Include.Insertion = Includes.insert(ToInclude->first); |
| 368 | Completion.Includes.push_back(std::move(Include)); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 369 | } else |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 370 | log("Failed to generate include insertion edits for adding header " |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 371 | "(FileURI='{0}', IncludeHeader='{1}') into {2}", |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 372 | C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 373 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 374 | // Prefer includes that do not need edits (i.e. already exist). |
| 375 | std::stable_partition(Completion.Includes.begin(), |
| 376 | Completion.Includes.end(), |
| 377 | [](const CodeCompletion::IncludeCandidate &I) { |
| 378 | return !I.Insertion.hasValue(); |
| 379 | }); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) { |
| 383 | assert(bool(C.SemaResult) == bool(SemaCCS)); |
| 384 | Bundled.emplace_back(); |
| 385 | BundledEntry &S = Bundled.back(); |
| 386 | if (C.SemaResult) { |
| 387 | getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix, |
| 388 | &Completion.RequiredQualifier); |
| 389 | S.ReturnType = getReturnType(*SemaCCS); |
| 390 | } else if (C.IndexResult) { |
| 391 | S.Signature = C.IndexResult->Signature; |
| 392 | S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix; |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 393 | S.ReturnType = C.IndexResult->ReturnType; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 394 | } |
| 395 | if (ExtractDocumentation && Completion.Documentation.empty()) { |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 396 | if (C.IndexResult) |
| 397 | Completion.Documentation = C.IndexResult->Documentation; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 398 | else if (C.SemaResult) |
| 399 | Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult, |
| 400 | /*CommentsFromHeader=*/false); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | CodeCompletion build() { |
| 405 | Completion.ReturnType = summarizeReturnType(); |
| 406 | Completion.Signature = summarizeSignature(); |
| 407 | Completion.SnippetSuffix = summarizeSnippet(); |
| 408 | Completion.BundleSize = Bundled.size(); |
| 409 | return std::move(Completion); |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | struct BundledEntry { |
| 414 | std::string SnippetSuffix; |
| 415 | std::string Signature; |
| 416 | std::string ReturnType; |
| 417 | }; |
| 418 | |
| 419 | // If all BundledEntrys have the same value for a property, return it. |
| 420 | template <std::string BundledEntry::*Member> |
| 421 | const std::string *onlyValue() const { |
| 422 | auto B = Bundled.begin(), E = Bundled.end(); |
| 423 | for (auto I = B + 1; I != E; ++I) |
| 424 | if (I->*Member != B->*Member) |
| 425 | return nullptr; |
| 426 | return &(B->*Member); |
| 427 | } |
| 428 | |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 429 | template <bool BundledEntry::*Member> const bool *onlyValue() const { |
| 430 | auto B = Bundled.begin(), E = Bundled.end(); |
| 431 | for (auto I = B + 1; I != E; ++I) |
| 432 | if (I->*Member != B->*Member) |
| 433 | return nullptr; |
| 434 | return &(B->*Member); |
| 435 | } |
| 436 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 437 | std::string summarizeReturnType() const { |
| 438 | if (auto *RT = onlyValue<&BundledEntry::ReturnType>()) |
| 439 | return *RT; |
| 440 | return ""; |
| 441 | } |
| 442 | |
| 443 | std::string summarizeSnippet() const { |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 444 | auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>(); |
| 445 | if (!Snippet) |
| 446 | // All bundles are function calls. |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 447 | // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g. |
| 448 | // we need to complete 'forward<$1>($0)'. |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 449 | return "($0)"; |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 450 | if (EnableFunctionArgSnippets) |
| 451 | return *Snippet; |
| 452 | |
| 453 | // Replace argument snippets with a simplified pattern. |
| 454 | if (Snippet->empty()) |
| 455 | return ""; |
| 456 | if (Completion.Kind == CompletionItemKind::Function || |
| 457 | Completion.Kind == CompletionItemKind::Method) { |
| 458 | // Functions snippets can be of 2 types: |
| 459 | // - containing only function arguments, e.g. |
| 460 | // foo(${1:int p1}, ${2:int p2}); |
| 461 | // We transform this pattern to '($0)' or '()'. |
| 462 | // - template arguments and function arguments, e.g. |
| 463 | // foo<${1:class}>(${2:int p1}). |
| 464 | // We transform this pattern to '<$1>()$0' or '<$0>()'. |
| 465 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 466 | bool EmptyArgs = StringRef(*Snippet).endswith("()"); |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 467 | if (Snippet->front() == '<') |
| 468 | return EmptyArgs ? "<$1>()$0" : "<$1>($0)"; |
| 469 | if (Snippet->front() == '(') |
| 470 | return EmptyArgs ? "()" : "($0)"; |
| 471 | return *Snippet; // Not an arg snippet? |
| 472 | } |
| 473 | if (Completion.Kind == CompletionItemKind::Reference || |
| 474 | Completion.Kind == CompletionItemKind::Class) { |
| 475 | if (Snippet->front() != '<') |
| 476 | return *Snippet; // Not an arg snippet? |
| 477 | |
| 478 | // Classes and template using aliases can only have template arguments, |
| 479 | // e.g. Foo<${1:class}>. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 480 | if (StringRef(*Snippet).endswith("<>")) |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 481 | return "<>"; // can happen with defaulted template arguments. |
| 482 | return "<$0>"; |
| 483 | } |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 484 | return *Snippet; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | std::string summarizeSignature() const { |
| 488 | if (auto *Signature = onlyValue<&BundledEntry::Signature>()) |
| 489 | return *Signature; |
| 490 | // All bundles are function calls. |
| 491 | return "(…)"; |
| 492 | } |
| 493 | |
| 494 | ASTContext &ASTCtx; |
| 495 | CodeCompletion Completion; |
| 496 | SmallVector<BundledEntry, 1> Bundled; |
| 497 | bool ExtractDocumentation; |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 498 | bool EnableFunctionArgSnippets; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 501 | // Determine the symbol ID for a Sema code completion result, if possible. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 502 | Optional<SymbolID> getSymbolID(const CodeCompletionResult &R, |
| 503 | const SourceManager &SM) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 504 | switch (R.Kind) { |
| 505 | case CodeCompletionResult::RK_Declaration: |
| 506 | case CodeCompletionResult::RK_Pattern: { |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 507 | return clang::clangd::getSymbolID(R.Declaration); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 508 | } |
| 509 | case CodeCompletionResult::RK_Macro: |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 510 | return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 511 | case CodeCompletionResult::RK_Keyword: |
| 512 | return None; |
| 513 | } |
| 514 | llvm_unreachable("unknown CodeCompletionResult kind"); |
| 515 | } |
| 516 | |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 517 | // Scopes of the paritial identifier we're trying to complete. |
| 518 | // It is used when we query the index for more completion results. |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 519 | struct SpecifiedScope { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 520 | // The scopes we should look in, determined by Sema. |
| 521 | // |
| 522 | // If the qualifier was fully resolved, we look for completions in these |
| 523 | // scopes; if there is an unresolved part of the qualifier, it should be |
| 524 | // resolved within these scopes. |
| 525 | // |
| 526 | // Examples of qualified completion: |
| 527 | // |
| 528 | // "::vec" => {""} |
| 529 | // "using namespace std; ::vec^" => {"", "std::"} |
| 530 | // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"} |
| 531 | // "std::vec^" => {""} // "std" unresolved |
| 532 | // |
| 533 | // Examples of unqualified completion: |
| 534 | // |
| 535 | // "vec^" => {""} |
| 536 | // "using namespace std; vec^" => {"", "std::"} |
| 537 | // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""} |
| 538 | // |
| 539 | // "" for global namespace, "ns::" for normal namespace. |
| 540 | std::vector<std::string> AccessibleScopes; |
| 541 | // The full scope qualifier as typed by the user (without the leading "::"). |
| 542 | // Set if the qualifier is not fully resolved by Sema. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 543 | Optional<std::string> UnresolvedQualifier; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 544 | |
Eric Liu | abbd713 | 2018-11-06 11:17:40 +0000 | [diff] [blame] | 545 | // Construct scopes being queried in indexes. The results are deduplicated. |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 546 | // This method format the scopes to match the index request representation. |
| 547 | std::vector<std::string> scopesForIndexQuery() { |
Eric Liu | abbd713 | 2018-11-06 11:17:40 +0000 | [diff] [blame] | 548 | std::set<std::string> Results; |
| 549 | for (StringRef AS : AccessibleScopes) |
| 550 | Results.insert( |
| 551 | ((UnresolvedQualifier ? *UnresolvedQualifier : "") + AS).str()); |
| 552 | return {Results.begin(), Results.end()}; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 553 | } |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 554 | }; |
| 555 | |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 556 | // Get all scopes that will be queried in indexes and whether symbols from |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 557 | // any scope is allowed. The first scope in the list is the preferred scope |
| 558 | // (e.g. enclosing namespace). |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 559 | std::pair<std::vector<std::string>, bool> |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 560 | getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema, |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 561 | const CodeCompleteOptions &Opts) { |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 562 | auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 563 | SpecifiedScope Info; |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 564 | for (auto *Context : CCContext.getVisitedContexts()) { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 565 | if (isa<TranslationUnitDecl>(Context)) |
| 566 | Info.AccessibleScopes.push_back(""); // global namespace |
Mikael Holmen | fa41add | 2018-10-18 06:00:39 +0000 | [diff] [blame] | 567 | else if (isa<NamespaceDecl>(Context)) |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 568 | Info.AccessibleScopes.push_back(printNamespaceScope(*Context)); |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 569 | } |
| 570 | return Info; |
| 571 | }; |
| 572 | |
| 573 | auto SS = CCContext.getCXXScopeSpecifier(); |
| 574 | |
| 575 | // Unqualified completion (e.g. "vec^"). |
| 576 | if (!SS) { |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 577 | std::vector<std::string> Scopes; |
| 578 | std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext); |
| 579 | Scopes.push_back(EnclosingScope); |
| 580 | for (auto &S : GetAllAccessibleScopes(CCContext).scopesForIndexQuery()) { |
| 581 | if (EnclosingScope != S) |
| 582 | Scopes.push_back(std::move(S)); |
| 583 | } |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 584 | // Allow AllScopes completion only for there is no explicit scope qualifier. |
| 585 | return {Scopes, Opts.AllScopes}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // Qualified completion ("std::vec^"), we have two cases depending on whether |
| 589 | // the qualifier can be resolved by Sema. |
| 590 | if ((*SS)->isValid()) { // Resolved qualifier. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 591 | return {GetAllAccessibleScopes(CCContext).scopesForIndexQuery(), false}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Unresolved qualifier. |
| 595 | // FIXME: When Sema can resolve part of a scope chain (e.g. |
| 596 | // "known::unknown::id"), we should expand the known part ("known::") rather |
| 597 | // than treating the whole thing as unknown. |
| 598 | SpecifiedScope Info; |
| 599 | Info.AccessibleScopes.push_back(""); // global namespace |
| 600 | |
| 601 | Info.UnresolvedQualifier = |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 602 | Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), |
| 603 | CCSema.SourceMgr, clang::LangOptions()) |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 604 | .ltrim("::"); |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 605 | // Sema excludes the trailing "::". |
| 606 | if (!Info.UnresolvedQualifier->empty()) |
| 607 | *Info.UnresolvedQualifier += "::"; |
| 608 | |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 609 | return {Info.scopesForIndexQuery(), false}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 612 | // Should we perform index-based completion in a context of the specified kind? |
| 613 | // FIXME: consider allowing completion, but restricting the result types. |
| 614 | bool contextAllowsIndex(enum CodeCompletionContext::Kind K) { |
| 615 | switch (K) { |
| 616 | case CodeCompletionContext::CCC_TopLevel: |
| 617 | case CodeCompletionContext::CCC_ObjCInterface: |
| 618 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 619 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 620 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 621 | case CodeCompletionContext::CCC_Statement: |
| 622 | case CodeCompletionContext::CCC_Expression: |
| 623 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 624 | case CodeCompletionContext::CCC_EnumTag: |
| 625 | case CodeCompletionContext::CCC_UnionTag: |
| 626 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 627 | case CodeCompletionContext::CCC_ObjCProtocolName: |
| 628 | case CodeCompletionContext::CCC_Namespace: |
| 629 | case CodeCompletionContext::CCC_Type: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 630 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 631 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
| 632 | case CodeCompletionContext::CCC_ObjCCategoryName: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 633 | case CodeCompletionContext::CCC_Symbol: |
| 634 | case CodeCompletionContext::CCC_SymbolOrNewName: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 635 | return true; |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 636 | case CodeCompletionContext::CCC_OtherWithMacros: |
| 637 | case CodeCompletionContext::CCC_DotMemberAccess: |
| 638 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
| 639 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
| 640 | case CodeCompletionContext::CCC_MacroName: |
| 641 | case CodeCompletionContext::CCC_MacroNameUse: |
| 642 | case CodeCompletionContext::CCC_PreprocessorExpression: |
| 643 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 644 | case CodeCompletionContext::CCC_SelectorName: |
| 645 | case CodeCompletionContext::CCC_TypeQualifiers: |
| 646 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
| 647 | case CodeCompletionContext::CCC_ObjCClassMessage: |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 648 | case CodeCompletionContext::CCC_IncludedFile: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 649 | // FIXME: Provide identifier based completions for the following contexts: |
| 650 | case CodeCompletionContext::CCC_Other: // Be conservative. |
| 651 | case CodeCompletionContext::CCC_NaturalLanguage: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 652 | case CodeCompletionContext::CCC_Recovery: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 653 | case CodeCompletionContext::CCC_NewName: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | llvm_unreachable("unknown code completion context"); |
| 657 | } |
| 658 | |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 659 | // Some member calls are blacklisted because they're so rarely useful. |
| 660 | static bool isBlacklistedMember(const NamedDecl &D) { |
| 661 | // Destructor completion is rarely useful, and works inconsistently. |
| 662 | // (s.^ completes ~string, but s.~st^ is an error). |
| 663 | if (D.getKind() == Decl::CXXDestructor) |
| 664 | return true; |
| 665 | // Injected name may be useful for A::foo(), but who writes A::A::foo()? |
| 666 | if (auto *R = dyn_cast_or_null<RecordDecl>(&D)) |
| 667 | if (R->isInjectedClassName()) |
| 668 | return true; |
| 669 | // Explicit calls to operators are also rare. |
| 670 | auto NameKind = D.getDeclName().getNameKind(); |
| 671 | if (NameKind == DeclarationName::CXXOperatorName || |
| 672 | NameKind == DeclarationName::CXXLiteralOperatorName || |
| 673 | NameKind == DeclarationName::CXXConversionFunctionName) |
| 674 | return true; |
| 675 | return false; |
| 676 | } |
| 677 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 678 | // The CompletionRecorder captures Sema code-complete output, including context. |
| 679 | // It filters out ignored results (but doesn't apply fuzzy-filtering yet). |
| 680 | // It doesn't do scoring or conversion to CompletionItem yet, as we want to |
| 681 | // merge with index results first. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 682 | // Generally the fields and methods of this object should only be used from |
| 683 | // within the callback. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 684 | struct CompletionRecorder : public CodeCompleteConsumer { |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 685 | CompletionRecorder(const CodeCompleteOptions &Opts, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 686 | unique_function<void()> ResultsCallback) |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 687 | : CodeCompleteConsumer(Opts.getClangCompleteOpts(), |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 688 | /*OutputIsBinary=*/false), |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 689 | CCContext(CodeCompletionContext::CCC_Other), Opts(Opts), |
| 690 | CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()), |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 691 | CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) { |
| 692 | assert(this->ResultsCallback); |
| 693 | } |
| 694 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 695 | std::vector<CodeCompletionResult> Results; |
| 696 | CodeCompletionContext CCContext; |
| 697 | Sema *CCSema = nullptr; // Sema that created the results. |
| 698 | // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead? |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 699 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 700 | void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, |
| 701 | CodeCompletionResult *InResults, |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 702 | unsigned NumResults) override final { |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 703 | // Results from recovery mode are generally useless, and the callback after |
| 704 | // recovery (if any) is usually more interesting. To make sure we handle the |
| 705 | // future callback from sema, we just ignore all callbacks in recovery mode, |
| 706 | // as taking only results from recovery mode results in poor completion |
| 707 | // results. |
| 708 | // FIXME: in case there is no future sema completion callback after the |
| 709 | // recovery mode, we might still want to provide some results (e.g. trivial |
| 710 | // identifier-based completion). |
| 711 | if (Context.getKind() == CodeCompletionContext::CCC_Recovery) { |
| 712 | log("Code complete: Ignoring sema code complete callback with Recovery " |
| 713 | "context."); |
| 714 | return; |
| 715 | } |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 716 | // If a callback is called without any sema result and the context does not |
| 717 | // support index-based completion, we simply skip it to give way to |
| 718 | // potential future callbacks with results. |
| 719 | if (NumResults == 0 && !contextAllowsIndex(Context.getKind())) |
| 720 | return; |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 721 | if (CCSema) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 722 | log("Multiple code complete callbacks (parser backtracked?). " |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 723 | "Dropping results from context {0}, keeping results from {1}.", |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 724 | getCompletionKindString(Context.getKind()), |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 725 | getCompletionKindString(this->CCContext.getKind())); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 726 | return; |
| 727 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 728 | // Record the completion context. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 729 | CCSema = &S; |
| 730 | CCContext = Context; |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 731 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 732 | // Retain the results we might want. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 733 | for (unsigned I = 0; I < NumResults; ++I) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 734 | auto &Result = InResults[I]; |
Sam McCall | e8437cb | 2018-10-24 13:51:44 +0000 | [diff] [blame] | 735 | // Class members that are shadowed by subclasses are usually noise. |
| 736 | if (Result.Hidden && Result.Declaration && |
| 737 | Result.Declaration->isCXXClassMember()) |
Ilya Biryukov | f60bf34 | 2018-01-10 13:51:09 +0000 | [diff] [blame] | 738 | continue; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 739 | if (!Opts.IncludeIneligibleResults && |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 740 | (Result.Availability == CXAvailability_NotAvailable || |
| 741 | Result.Availability == CXAvailability_NotAccessible)) |
| 742 | continue; |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 743 | if (Result.Declaration && |
| 744 | !Context.getBaseType().isNull() // is this a member-access context? |
| 745 | && isBlacklistedMember(*Result.Declaration)) |
Sam McCall | d2a9592 | 2018-01-22 21:05:00 +0000 | [diff] [blame] | 746 | continue; |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 747 | // We choose to never append '::' to completion results in clangd. |
| 748 | Result.StartsNestedNameSpecifier = false; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 749 | Results.push_back(Result); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 750 | } |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 751 | ResultsCallback(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 754 | CodeCompletionAllocator &getAllocator() override { return *CCAllocator; } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 755 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
| 756 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 757 | // Returns the filtering/sorting name for Result, which must be from Results. |
| 758 | // Returned string is owned by this recorder (or the AST). |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 759 | StringRef getName(const CodeCompletionResult &Result) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 760 | switch (Result.Kind) { |
| 761 | case CodeCompletionResult::RK_Declaration: |
| 762 | if (auto *ID = Result.Declaration->getIdentifier()) |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 763 | return ID->getName(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 764 | break; |
| 765 | case CodeCompletionResult::RK_Keyword: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 766 | return Result.Keyword; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 767 | case CodeCompletionResult::RK_Macro: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 768 | return Result.Macro->getName(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 769 | case CodeCompletionResult::RK_Pattern: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 770 | return Result.Pattern->getTypedText(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 771 | } |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 772 | auto *CCS = codeCompletionString(Result); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 773 | return CCS->getTypedText(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 776 | // Build a CodeCompletion string for R, which must be from Results. |
| 777 | // The CCS will be owned by this recorder. |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 778 | CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 779 | // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway. |
| 780 | return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString( |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 781 | *CCSema, CCContext, *CCAllocator, CCTUInfo, |
| 782 | /*IncludeBriefComments=*/false); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 785 | private: |
| 786 | CodeCompleteOptions Opts; |
| 787 | std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 788 | CodeCompletionTUInfo CCTUInfo; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 789 | unique_function<void()> ResultsCallback; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 790 | }; |
| 791 | |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 792 | struct ScoredSignature { |
| 793 | // When set, requires documentation to be requested from the index with this |
| 794 | // ID. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 795 | Optional<SymbolID> IDForDoc; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 796 | SignatureInformation Signature; |
| 797 | SignatureQualitySignals Quality; |
| 798 | }; |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 799 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 800 | class SignatureHelpCollector final : public CodeCompleteConsumer { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 801 | public: |
| 802 | SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts, |
Sam McCall | 046557b | 2018-09-03 16:37:59 +0000 | [diff] [blame] | 803 | const SymbolIndex *Index, SignatureHelp &SigHelp) |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 804 | : CodeCompleteConsumer(CodeCompleteOpts, |
| 805 | /*OutputIsBinary=*/false), |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 806 | SigHelp(SigHelp), |
| 807 | Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 808 | CCTUInfo(Allocator), Index(Index) {} |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 809 | |
| 810 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 811 | OverloadCandidate *Candidates, |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 812 | unsigned NumCandidates, |
| 813 | SourceLocation OpenParLoc) override { |
| 814 | assert(!OpenParLoc.isInvalid()); |
| 815 | SourceManager &SrcMgr = S.getSourceManager(); |
| 816 | OpenParLoc = SrcMgr.getFileLoc(OpenParLoc); |
| 817 | if (SrcMgr.isInMainFile(OpenParLoc)) |
| 818 | SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc); |
| 819 | else |
| 820 | elog("Location oustide main file in signature help: {0}", |
| 821 | OpenParLoc.printToString(SrcMgr)); |
| 822 | |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 823 | std::vector<ScoredSignature> ScoredSignatures; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 824 | SigHelp.signatures.reserve(NumCandidates); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 825 | ScoredSignatures.reserve(NumCandidates); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 826 | // FIXME(rwols): How can we determine the "active overload candidate"? |
| 827 | // Right now the overloaded candidates seem to be provided in a "best fit" |
| 828 | // order, so I'm not too worried about this. |
| 829 | SigHelp.activeSignature = 0; |
| 830 | assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() && |
| 831 | "too many arguments"); |
| 832 | SigHelp.activeParameter = static_cast<int>(CurrentArg); |
| 833 | for (unsigned I = 0; I < NumCandidates; ++I) { |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 834 | OverloadCandidate Candidate = Candidates[I]; |
| 835 | // We want to avoid showing instantiated signatures, because they may be |
| 836 | // long in some cases (e.g. when 'T' is substituted with 'std::string', we |
| 837 | // would get 'std::basic_string<char>'). |
| 838 | if (auto *Func = Candidate.getFunction()) { |
| 839 | if (auto *Pattern = Func->getTemplateInstantiationPattern()) |
| 840 | Candidate = OverloadCandidate(Pattern); |
| 841 | } |
| 842 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 843 | const auto *CCS = Candidate.CreateSignatureString( |
| 844 | CurrentArg, S, *Allocator, CCTUInfo, true); |
| 845 | assert(CCS && "Expected the CodeCompletionString to be non-null"); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 846 | ScoredSignatures.push_back(processOverloadCandidate( |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 847 | Candidate, *CCS, |
Ilya Biryukov | 5f4a351 | 2018-08-17 09:29:38 +0000 | [diff] [blame] | 848 | Candidate.getFunction() |
| 849 | ? getDeclComment(S.getASTContext(), *Candidate.getFunction()) |
| 850 | : "")); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 851 | } |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 852 | |
| 853 | // Sema does not load the docs from the preamble, so we need to fetch extra |
| 854 | // docs from the index instead. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 855 | DenseMap<SymbolID, std::string> FetchedDocs; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 856 | if (Index) { |
| 857 | LookupRequest IndexRequest; |
| 858 | for (const auto &S : ScoredSignatures) { |
| 859 | if (!S.IDForDoc) |
| 860 | continue; |
| 861 | IndexRequest.IDs.insert(*S.IDForDoc); |
| 862 | } |
| 863 | Index->lookup(IndexRequest, [&](const Symbol &S) { |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 864 | if (!S.Documentation.empty()) |
| 865 | FetchedDocs[S.ID] = S.Documentation; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 866 | }); |
| 867 | log("SigHelp: requested docs for {0} symbols from the index, got {1} " |
| 868 | "symbols with non-empty docs in the response", |
| 869 | IndexRequest.IDs.size(), FetchedDocs.size()); |
| 870 | } |
| 871 | |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 872 | llvm::sort( |
| 873 | ScoredSignatures, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 874 | [](const ScoredSignature &L, const ScoredSignature &R) { |
| 875 | // Ordering follows: |
| 876 | // - Less number of parameters is better. |
| 877 | // - Function is better than FunctionType which is better than |
| 878 | // Function Template. |
| 879 | // - High score is better. |
| 880 | // - Shorter signature is better. |
| 881 | // - Alphebatically smaller is better. |
| 882 | if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters) |
| 883 | return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters; |
| 884 | if (L.Quality.NumberOfOptionalParameters != |
| 885 | R.Quality.NumberOfOptionalParameters) |
| 886 | return L.Quality.NumberOfOptionalParameters < |
| 887 | R.Quality.NumberOfOptionalParameters; |
| 888 | if (L.Quality.Kind != R.Quality.Kind) { |
| 889 | using OC = CodeCompleteConsumer::OverloadCandidate; |
| 890 | switch (L.Quality.Kind) { |
| 891 | case OC::CK_Function: |
| 892 | return true; |
| 893 | case OC::CK_FunctionType: |
| 894 | return R.Quality.Kind != OC::CK_Function; |
| 895 | case OC::CK_FunctionTemplate: |
| 896 | return false; |
| 897 | } |
| 898 | llvm_unreachable("Unknown overload candidate type."); |
| 899 | } |
| 900 | if (L.Signature.label.size() != R.Signature.label.size()) |
| 901 | return L.Signature.label.size() < R.Signature.label.size(); |
| 902 | return L.Signature.label < R.Signature.label; |
| 903 | }); |
| 904 | |
| 905 | for (auto &SS : ScoredSignatures) { |
| 906 | auto IndexDocIt = |
| 907 | SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end(); |
| 908 | if (IndexDocIt != FetchedDocs.end()) |
| 909 | SS.Signature.documentation = IndexDocIt->second; |
| 910 | |
| 911 | SigHelp.signatures.push_back(std::move(SS.Signature)); |
| 912 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; } |
| 916 | |
| 917 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
| 918 | |
| 919 | private: |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 920 | // FIXME(ioeric): consider moving CodeCompletionString logic here to |
| 921 | // CompletionString.h. |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 922 | ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate, |
| 923 | const CodeCompletionString &CCS, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 924 | StringRef DocComment) const { |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 925 | SignatureInformation Signature; |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 926 | SignatureQualitySignals Signal; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 927 | const char *ReturnType = nullptr; |
| 928 | |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 929 | Signature.documentation = formatDocumentation(CCS, DocComment); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 930 | Signal.Kind = Candidate.getKind(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 931 | |
| 932 | for (const auto &Chunk : CCS) { |
| 933 | switch (Chunk.Kind) { |
| 934 | case CodeCompletionString::CK_ResultType: |
| 935 | // A piece of text that describes the type of an entity or, |
| 936 | // for functions and methods, the return type. |
| 937 | assert(!ReturnType && "Unexpected CK_ResultType"); |
| 938 | ReturnType = Chunk.Text; |
| 939 | break; |
| 940 | case CodeCompletionString::CK_Placeholder: |
| 941 | // A string that acts as a placeholder for, e.g., a function call |
| 942 | // argument. |
| 943 | // Intentional fallthrough here. |
| 944 | case CodeCompletionString::CK_CurrentParameter: { |
| 945 | // A piece of text that describes the parameter that corresponds to |
| 946 | // the code-completion location within a function call, message send, |
| 947 | // macro invocation, etc. |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 948 | Signature.label += Chunk.Text; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 949 | ParameterInformation Info; |
| 950 | Info.label = Chunk.Text; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 951 | Signature.parameters.push_back(std::move(Info)); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 952 | Signal.NumberOfParameters++; |
| 953 | Signal.ContainsActiveParameter = true; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
| 956 | case CodeCompletionString::CK_Optional: { |
| 957 | // The rest of the parameters are defaulted/optional. |
| 958 | assert(Chunk.Optional && |
| 959 | "Expected the optional code completion string to be non-null."); |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 960 | Signature.label += getOptionalParameters(*Chunk.Optional, |
| 961 | Signature.parameters, Signal); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 962 | break; |
| 963 | } |
| 964 | case CodeCompletionString::CK_VerticalSpace: |
| 965 | break; |
| 966 | default: |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 967 | Signature.label += Chunk.Text; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | } |
| 971 | if (ReturnType) { |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 972 | Signature.label += " -> "; |
| 973 | Signature.label += ReturnType; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 974 | } |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 975 | dlog("Signal for {0}: {1}", Signature, Signal); |
| 976 | ScoredSignature Result; |
| 977 | Result.Signature = std::move(Signature); |
| 978 | Result.Quality = Signal; |
| 979 | Result.IDForDoc = |
| 980 | Result.Signature.documentation.empty() && Candidate.getFunction() |
| 981 | ? clangd::getSymbolID(Candidate.getFunction()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 982 | : None; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 983 | return Result; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | SignatureHelp &SigHelp; |
| 987 | std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; |
| 988 | CodeCompletionTUInfo CCTUInfo; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 989 | const SymbolIndex *Index; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 990 | }; // SignatureHelpCollector |
| 991 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 992 | struct SemaCompleteInput { |
| 993 | PathRef FileName; |
| 994 | const tooling::CompileCommand &Command; |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 995 | const PreambleData *Preamble; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 996 | StringRef Contents; |
| 997 | Position Pos; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 998 | IntrusiveRefCntPtr<vfs::FileSystem> VFS; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 999 | std::shared_ptr<PCHContainerOperations> PCHs; |
| 1000 | }; |
| 1001 | |
| 1002 | // Invokes Sema code completion on a file. |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1003 | // If \p Includes is set, it will be updated based on the compiler invocation. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1004 | bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1005 | const clang::CodeCompleteOptions &Options, |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1006 | const SemaCompleteInput &Input, |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1007 | IncludeStructure *Includes = nullptr) { |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1008 | trace::Span Tracer("Sema completion"); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1009 | std::vector<const char *> ArgStrs; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1010 | for (const auto &S : Input.Command.CommandLine) |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1011 | ArgStrs.push_back(S.c_str()); |
| 1012 | |
Ilya Biryukov | a9cf311 | 2018-02-13 17:15:06 +0000 | [diff] [blame] | 1013 | if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) { |
| 1014 | log("Couldn't set working directory"); |
| 1015 | // We run parsing anyway, our lit-tests rely on results for non-existing |
| 1016 | // working dirs. |
| 1017 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1018 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1019 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = Input.VFS; |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1020 | if (Input.Preamble && Input.Preamble->StatCache) |
| 1021 | VFS = Input.Preamble->StatCache->getConsumingFS(std::move(VFS)); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1022 | IgnoreDiagnostics DummyDiagsConsumer; |
| 1023 | auto CI = createInvocationFromCommandLine( |
| 1024 | ArgStrs, |
| 1025 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 1026 | &DummyDiagsConsumer, false), |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1027 | VFS); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 1028 | if (!CI) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1029 | elog("Couldn't create CompilerInvocation"); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | } |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1032 | auto &FrontendOpts = CI->getFrontendOpts(); |
| 1033 | FrontendOpts.DisableFree = false; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1034 | FrontendOpts.SkipFunctionBodies = true; |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1035 | CI->getLangOpts()->CommentOpts.ParseAllComments = true; |
| 1036 | // Disable typo correction in Sema. |
| 1037 | CI->getLangOpts()->SpellChecking = false; |
| 1038 | // Setup code completion. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1039 | FrontendOpts.CodeCompleteOpts = Options; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1040 | FrontendOpts.CodeCompletionAt.FileName = Input.FileName; |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 1041 | auto Offset = positionToOffset(Input.Contents, Input.Pos); |
| 1042 | if (!Offset) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1043 | elog("Code completion position was invalid {0}", Offset.takeError()); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 1044 | return false; |
| 1045 | } |
| 1046 | std::tie(FrontendOpts.CodeCompletionAt.Line, |
| 1047 | FrontendOpts.CodeCompletionAt.Column) = |
| 1048 | offsetToClangLineColumn(Input.Contents, *Offset); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1049 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1050 | std::unique_ptr<MemoryBuffer> ContentsBuffer = |
| 1051 | MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1052 | // The diagnostic options must be set before creating a CompilerInstance. |
| 1053 | CI->getDiagnosticOpts().IgnoreWarnings = true; |
| 1054 | // We reuse the preamble whether it's valid or not. This is a |
| 1055 | // correctness/performance tradeoff: building without a preamble is slow, and |
| 1056 | // completion is latency-sensitive. |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 1057 | // However, if we're completing *inside* the preamble section of the draft, |
| 1058 | // overriding the preamble will break sema completion. Fortunately we can just |
| 1059 | // skip all includes in this case; these completions are really simple. |
| 1060 | bool CompletingInPreamble = |
| 1061 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0).Size > |
| 1062 | *Offset; |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1063 | // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise |
| 1064 | // the remapped buffers do not get freed. |
| 1065 | auto Clang = prepareCompilerInstance( |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1066 | std::move(CI), |
| 1067 | (Input.Preamble && !CompletingInPreamble) ? &Input.Preamble->Preamble |
| 1068 | : nullptr, |
| 1069 | std::move(ContentsBuffer), std::move(Input.PCHs), std::move(VFS), |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 1070 | DummyDiagsConsumer); |
| 1071 | Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1072 | Clang->setCodeCompletionConsumer(Consumer.release()); |
| 1073 | |
| 1074 | SyntaxOnlyAction Action; |
| 1075 | if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1076 | log("BeginSourceFile() failed when running codeComplete for {0}", |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1077 | Input.FileName); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1078 | return false; |
| 1079 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1080 | if (Includes) |
| 1081 | Clang->getPreprocessor().addPPCallbacks( |
| 1082 | collectIncludeStructureCallback(Clang->getSourceManager(), Includes)); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1083 | if (!Action.Execute()) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1084 | log("Execute() failed when running codeComplete for {0}", Input.FileName); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1085 | return false; |
| 1086 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1087 | Action.EndSourceFile(); |
| 1088 | |
| 1089 | return true; |
| 1090 | } |
| 1091 | |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1092 | // Should we allow index completions in the specified context? |
| 1093 | bool allowIndex(CodeCompletionContext &CC) { |
| 1094 | if (!contextAllowsIndex(CC.getKind())) |
| 1095 | return false; |
| 1096 | // We also avoid ClassName::bar (but allow namespace::bar). |
| 1097 | auto Scope = CC.getCXXScopeSpecifier(); |
| 1098 | if (!Scope) |
| 1099 | return true; |
| 1100 | NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep(); |
| 1101 | if (!NameSpec) |
| 1102 | return true; |
| 1103 | // We only query the index when qualifier is a namespace. |
| 1104 | // If it's a class, we rely solely on sema completions. |
| 1105 | switch (NameSpec->getKind()) { |
| 1106 | case NestedNameSpecifier::Global: |
| 1107 | case NestedNameSpecifier::Namespace: |
| 1108 | case NestedNameSpecifier::NamespaceAlias: |
| 1109 | return true; |
| 1110 | case NestedNameSpecifier::Super: |
| 1111 | case NestedNameSpecifier::TypeSpec: |
| 1112 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1113 | // Unresolved inside a template. |
| 1114 | case NestedNameSpecifier::Identifier: |
| 1115 | return false; |
| 1116 | } |
Ilya Biryukov | a6556e2 | 2018-05-14 11:47:30 +0000 | [diff] [blame] | 1117 | llvm_unreachable("invalid NestedNameSpecifier kind"); |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1120 | std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index, |
| 1121 | const FuzzyFindRequest &Req) { |
| 1122 | return runAsync<SymbolSlab>([&Index, Req]() { |
| 1123 | trace::Span Tracer("Async fuzzyFind"); |
| 1124 | SymbolSlab::Builder Syms; |
| 1125 | Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); }); |
| 1126 | return std::move(Syms).build(); |
| 1127 | }); |
| 1128 | } |
| 1129 | |
| 1130 | // Creates a `FuzzyFindRequest` based on the cached index request from the |
| 1131 | // last completion, if any, and the speculated completion filter text in the |
| 1132 | // source code. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1133 | Optional<FuzzyFindRequest> speculativeFuzzyFindRequestForCompletion( |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1134 | FuzzyFindRequest CachedReq, PathRef File, StringRef Content, Position Pos) { |
| 1135 | auto Filter = speculateCompletionFilter(Content, Pos); |
| 1136 | if (!Filter) { |
| 1137 | elog("Failed to speculate filter text for code completion at Pos " |
| 1138 | "{0}:{1}: {2}", |
| 1139 | Pos.line, Pos.character, Filter.takeError()); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1140 | return None; |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1141 | } |
| 1142 | CachedReq.Query = *Filter; |
| 1143 | return CachedReq; |
| 1144 | } |
| 1145 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1146 | } // namespace |
| 1147 | |
| 1148 | clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const { |
| 1149 | clang::CodeCompleteOptions Result; |
| 1150 | Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns; |
| 1151 | Result.IncludeMacros = IncludeMacros; |
Sam McCall | d8169a8 | 2018-01-18 15:31:30 +0000 | [diff] [blame] | 1152 | Result.IncludeGlobals = true; |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 1153 | // We choose to include full comments and not do doxygen parsing in |
| 1154 | // completion. |
| 1155 | // FIXME: ideally, we should support doxygen in some form, e.g. do markdown |
| 1156 | // formatting of the comments. |
| 1157 | Result.IncludeBriefComments = false; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1158 | |
Sam McCall | 3d139c5 | 2018-01-12 18:30:08 +0000 | [diff] [blame] | 1159 | // When an is used, Sema is responsible for completing the main file, |
| 1160 | // the index can provide results from the preamble. |
| 1161 | // Tell Sema not to deserialize the preamble to look for results. |
| 1162 | Result.LoadExternal = !Index; |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 1163 | Result.IncludeFixIts = IncludeFixIts; |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 1164 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1165 | return Result; |
| 1166 | } |
| 1167 | |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1168 | // Returns the most popular include header for \p Sym. If two headers are |
| 1169 | // equally popular, prefer the shorter one. Returns empty string if \p Sym has |
| 1170 | // no include header. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1171 | SmallVector<StringRef, 1> getRankedIncludes(const Symbol &Sym) { |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1172 | auto Includes = Sym.IncludeHeaders; |
| 1173 | // Sort in descending order by reference count and header length. |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 1174 | llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS, |
| 1175 | const Symbol::IncludeHeaderWithReferences &RHS) { |
| 1176 | if (LHS.References == RHS.References) |
| 1177 | return LHS.IncludeHeader.size() < RHS.IncludeHeader.size(); |
| 1178 | return LHS.References > RHS.References; |
| 1179 | }); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1180 | SmallVector<StringRef, 1> Headers; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1181 | for (const auto &Include : Includes) |
| 1182 | Headers.push_back(Include.IncludeHeader); |
| 1183 | return Headers; |
| 1184 | } |
| 1185 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1186 | // Runs Sema-based (AST) and Index-based completion, returns merged results. |
| 1187 | // |
| 1188 | // There are a few tricky considerations: |
| 1189 | // - the AST provides information needed for the index query (e.g. which |
| 1190 | // namespaces to search in). So Sema must start first. |
| 1191 | // - we only want to return the top results (Opts.Limit). |
| 1192 | // Building CompletionItems for everything else is wasteful, so we want to |
| 1193 | // preserve the "native" format until we're done with scoring. |
| 1194 | // - the data underlying Sema completion items is owned by the AST and various |
| 1195 | // other arenas, which must stay alive for us to build CompletionItems. |
| 1196 | // - we may get duplicate results from Sema and the Index, we need to merge. |
| 1197 | // |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1198 | // So we start Sema completion first, and do all our work in its callback. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1199 | // We use the Sema context information to query the index. |
| 1200 | // Then we merge the two result sets, producing items that are Sema/Index/Both. |
| 1201 | // These items are scored, and the top N are synthesized into the LSP response. |
| 1202 | // Finally, we can clean up the data structures created by Sema completion. |
| 1203 | // |
| 1204 | // Main collaborators are: |
| 1205 | // - semaCodeComplete sets up the compiler machinery to run code completion. |
| 1206 | // - CompletionRecorder captures Sema completion results, including context. |
| 1207 | // - SymbolIndex (Opts.Index) provides index completion results as Symbols |
| 1208 | // - CompletionCandidates are the result of merging Sema and Index results. |
| 1209 | // Each candidate points to an underlying CodeCompletionResult (Sema), a |
| 1210 | // Symbol (Index), or both. It computes the result quality score. |
| 1211 | // CompletionCandidate also does conversion to CompletionItem (at the end). |
| 1212 | // - FuzzyMatcher scores how the candidate matches the partial identifier. |
| 1213 | // This score is combined with the result quality score for the final score. |
| 1214 | // - TopN determines the results with the best score. |
| 1215 | class CodeCompleteFlow { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 1216 | PathRef FileName; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1217 | IncludeStructure Includes; // Complete once the compiler runs. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1218 | SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1219 | const CodeCompleteOptions &Opts; |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1220 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1221 | // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1222 | CompletionRecorder *Recorder = nullptr; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1223 | int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging. |
| 1224 | bool Incomplete = false; // Would more be available with a higher limit? |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1225 | Optional<FuzzyMatcher> Filter; // Initialized once Sema runs. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1226 | std::vector<std::string> QueryScopes; // Initialized once Sema runs. |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1227 | // Initialized once QueryScopes is initialized, if there are scopes. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1228 | Optional<ScopeDistance> ScopeProximity; |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1229 | llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1230 | // Whether to query symbols from any scope. Initialized once Sema runs. |
| 1231 | bool AllScopes = false; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1232 | // Include-insertion and proximity scoring rely on the include structure. |
| 1233 | // This is available after Sema has run. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1234 | Optional<IncludeInserter> Inserter; // Available during runWithSema. |
| 1235 | Optional<URIDistance> FileProximity; // Initialized once Sema runs. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1236 | /// Speculative request based on the cached request and the filter text before |
| 1237 | /// the cursor. |
| 1238 | /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is |
| 1239 | /// set and contains a cached request. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1240 | Optional<FuzzyFindRequest> SpecReq; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1241 | |
| 1242 | public: |
| 1243 | // A CodeCompleteFlow object is only useful for calling run() exactly once. |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1244 | CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes, |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1245 | SpeculativeFuzzyFind *SpecFuzzyFind, |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1246 | const CodeCompleteOptions &Opts) |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1247 | : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind), |
| 1248 | Opts(Opts) {} |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1249 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1250 | CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1251 | trace::Span Tracer("CodeCompleteFlow"); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1252 | if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) { |
| 1253 | assert(!SpecFuzzyFind->Result.valid()); |
| 1254 | if ((SpecReq = speculativeFuzzyFindRequestForCompletion( |
| 1255 | *SpecFuzzyFind->CachedReq, SemaCCInput.FileName, |
| 1256 | SemaCCInput.Contents, SemaCCInput.Pos))) |
| 1257 | SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq); |
| 1258 | } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1259 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1260 | // We run Sema code completion first. It builds an AST and calculates: |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1261 | // - completion results based on the AST. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1262 | // - partial identifier and context. We need these for the index query. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1263 | CodeCompleteResult Output; |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1264 | auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() { |
| 1265 | assert(Recorder && "Recorder is not set"); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1266 | auto Style = |
Eric Liu | 9338a88 | 2018-07-03 14:51:23 +0000 | [diff] [blame] | 1267 | format::getStyle(format::DefaultFormatStyle, SemaCCInput.FileName, |
| 1268 | format::DefaultFallbackStyle, SemaCCInput.Contents, |
| 1269 | SemaCCInput.VFS.get()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1270 | if (!Style) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1271 | log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", |
| 1272 | SemaCCInput.FileName, Style.takeError()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1273 | Style = format::getLLVMStyle(); |
| 1274 | } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1275 | // If preprocessor was run, inclusions from preprocessor callback should |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1276 | // already be added to Includes. |
| 1277 | Inserter.emplace( |
| 1278 | SemaCCInput.FileName, SemaCCInput.Contents, *Style, |
| 1279 | SemaCCInput.Command.Directory, |
| 1280 | Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); |
| 1281 | for (const auto &Inc : Includes.MainFileIncludes) |
| 1282 | Inserter->addExisting(Inc); |
| 1283 | |
| 1284 | // Most of the cost of file proximity is in initializing the FileDistance |
| 1285 | // structures based on the observed includes, once per query. Conceptually |
| 1286 | // that happens here (though the per-URI-scheme initialization is lazy). |
| 1287 | // The per-result proximity scoring is (amortized) very cheap. |
| 1288 | FileDistanceOptions ProxOpts{}; // Use defaults. |
| 1289 | const auto &SM = Recorder->CCSema->getSourceManager(); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1290 | StringMap<SourceParams> ProxSources; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1291 | for (auto &Entry : Includes.includeDepth( |
| 1292 | SM.getFileEntryForID(SM.getMainFileID())->getName())) { |
| 1293 | auto &Source = ProxSources[Entry.getKey()]; |
| 1294 | Source.Cost = Entry.getValue() * ProxOpts.IncludeCost; |
| 1295 | // Symbols near our transitive includes are good, but only consider |
| 1296 | // things in the same directory or below it. Otherwise there can be |
| 1297 | // many false positives. |
| 1298 | if (Entry.getValue() > 0) |
| 1299 | Source.MaxUpTraversals = 1; |
| 1300 | } |
| 1301 | FileProximity.emplace(ProxSources, ProxOpts); |
| 1302 | |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1303 | Output = runWithSema(); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1304 | Inserter.reset(); // Make sure this doesn't out-live Clang. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1305 | SPAN_ATTACH(Tracer, "sema_completion_kind", |
| 1306 | getCompletionKindString(Recorder->CCContext.getKind())); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1307 | log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), " |
| 1308 | "expected type {3}", |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 1309 | getCompletionKindString(Recorder->CCContext.getKind()), |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1310 | join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes, |
| 1311 | PreferredType ? Recorder->CCContext.getPreferredType().getAsString() |
| 1312 | : "<none>"); |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1313 | }); |
| 1314 | |
| 1315 | Recorder = RecorderOwner.get(); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1316 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1317 | semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(), |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1318 | SemaCCInput, &Includes); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1319 | |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 1320 | SPAN_ATTACH(Tracer, "sema_results", NSema); |
| 1321 | SPAN_ATTACH(Tracer, "index_results", NIndex); |
| 1322 | SPAN_ATTACH(Tracer, "merged_results", NBoth); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 1323 | SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size())); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1324 | SPAN_ATTACH(Tracer, "incomplete", Output.HasMore); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1325 | log("Code complete: {0} results from Sema, {1} from Index, " |
| 1326 | "{2} matched, {3} returned{4}.", |
| 1327 | NSema, NIndex, NBoth, Output.Completions.size(), |
| 1328 | Output.HasMore ? " (incomplete)" : ""); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1329 | assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1330 | // We don't assert that isIncomplete means we hit a limit. |
| 1331 | // Indexes may choose to impose their own limits even if we don't have one. |
| 1332 | return Output; |
| 1333 | } |
| 1334 | |
| 1335 | private: |
| 1336 | // This is called by run() once Sema code completion is done, but before the |
| 1337 | // Sema data structures are torn down. It does all the real work. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1338 | CodeCompleteResult runWithSema() { |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1339 | const auto &CodeCompletionRange = CharSourceRange::getCharRange( |
| 1340 | Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange()); |
| 1341 | Range TextEditRange; |
| 1342 | // When we are getting completions with an empty identifier, for example |
| 1343 | // std::vector<int> asdf; |
| 1344 | // asdf.^; |
| 1345 | // Then the range will be invalid and we will be doing insertion, use |
| 1346 | // current cursor position in such cases as range. |
| 1347 | if (CodeCompletionRange.isValid()) { |
| 1348 | TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(), |
| 1349 | CodeCompletionRange); |
| 1350 | } else { |
| 1351 | const auto &Pos = sourceLocToPosition( |
| 1352 | Recorder->CCSema->getSourceManager(), |
| 1353 | Recorder->CCSema->getPreprocessor().getCodeCompletionLoc()); |
| 1354 | TextEditRange.start = TextEditRange.end = Pos; |
| 1355 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1356 | Filter = FuzzyMatcher( |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1357 | Recorder->CCSema->getPreprocessor().getCodeCompletionFilter()); |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1358 | std::tie(QueryScopes, AllScopes) = |
| 1359 | getQueryScopes(Recorder->CCContext, *Recorder->CCSema, Opts); |
| 1360 | if (!QueryScopes.empty()) |
| 1361 | ScopeProximity.emplace(QueryScopes); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1362 | PreferredType = |
| 1363 | OpaqueType::fromType(Recorder->CCSema->getASTContext(), |
| 1364 | Recorder->CCContext.getPreferredType()); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1365 | // Sema provides the needed context to query the index. |
| 1366 | // FIXME: in addition to querying for extra/overlapping symbols, we should |
| 1367 | // explicitly request symbols corresponding to Sema results. |
| 1368 | // We can use their signals even if the index can't suggest them. |
| 1369 | // We must copy index results to preserve them, but there are at most Limit. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 1370 | auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext)) |
| 1371 | ? queryIndex() |
| 1372 | : SymbolSlab(); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1373 | trace::Span Tracer("Populate CodeCompleteResult"); |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1374 | // Merge Sema and Index results, score them, and pick the winners. |
| 1375 | auto Top = mergeResults(Recorder->Results, IndexResults); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1376 | CodeCompleteResult Output; |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Convert the results to final form, assembling the expensive strings. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1379 | for (auto &C : Top) { |
| 1380 | Output.Completions.push_back(toCodeCompletion(C.first)); |
| 1381 | Output.Completions.back().Score = C.second; |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1382 | Output.Completions.back().CompletionTokenRange = TextEditRange; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1383 | } |
| 1384 | Output.HasMore = Incomplete; |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1385 | Output.Context = Recorder->CCContext.getKind(); |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1386 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1387 | return Output; |
| 1388 | } |
| 1389 | |
| 1390 | SymbolSlab queryIndex() { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1391 | trace::Span Tracer("Query index"); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 1392 | SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit)); |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 1393 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1394 | // Build the query. |
| 1395 | FuzzyFindRequest Req; |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 1396 | if (Opts.Limit) |
Kirill Bobyrev | e6dd080 | 2018-09-13 14:27:03 +0000 | [diff] [blame] | 1397 | Req.Limit = Opts.Limit; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1398 | Req.Query = Filter->pattern(); |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 1399 | Req.RestrictForCodeCompletion = true; |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 1400 | Req.Scopes = QueryScopes; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1401 | Req.AnyScope = AllScopes; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1402 | // FIXME: we should send multiple weighted paths here. |
Eric Liu | 6de95ec | 2018-06-12 08:48:20 +0000 | [diff] [blame] | 1403 | Req.ProximityPaths.push_back(FileName); |
Kirill Bobyrev | 09f00dc | 2018-09-10 11:51:05 +0000 | [diff] [blame] | 1404 | vlog("Code complete: fuzzyFind({0:2})", toJSON(Req)); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (SpecFuzzyFind) |
| 1407 | SpecFuzzyFind->NewReq = Req; |
| 1408 | if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) { |
| 1409 | vlog("Code complete: speculative fuzzy request matches the actual index " |
| 1410 | "request. Waiting for the speculative index results."); |
| 1411 | SPAN_ATTACH(Tracer, "Speculative results", true); |
| 1412 | |
| 1413 | trace::Span WaitSpec("Wait speculative results"); |
| 1414 | return SpecFuzzyFind->Result.get(); |
| 1415 | } |
| 1416 | |
| 1417 | SPAN_ATTACH(Tracer, "Speculative results", false); |
| 1418 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1419 | // Run the query against the index. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1420 | SymbolSlab::Builder ResultsBuilder; |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 1421 | if (Opts.Index->fuzzyFind( |
| 1422 | Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); })) |
| 1423 | Incomplete = true; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1424 | return std::move(ResultsBuilder).build(); |
| 1425 | } |
| 1426 | |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1427 | // Merges Sema and Index results where possible, to form CompletionCandidates. |
| 1428 | // Groups overloads if desired, to form CompletionCandidate::Bundles. The |
| 1429 | // bundles are scored and top results are returned, best to worst. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1430 | std::vector<ScoredBundle> |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1431 | mergeResults(const std::vector<CodeCompletionResult> &SemaResults, |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1432 | const SymbolSlab &IndexResults) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1433 | trace::Span Tracer("Merge and score results"); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1434 | std::vector<CompletionCandidate::Bundle> Bundles; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1435 | DenseMap<size_t, size_t> BundleLookup; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1436 | auto AddToBundles = [&](const CodeCompletionResult *SemaResult, |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1437 | const Symbol *IndexResult) { |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1438 | CompletionCandidate C; |
| 1439 | C.SemaResult = SemaResult; |
| 1440 | C.IndexResult = IndexResult; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1441 | if (C.IndexResult) |
| 1442 | C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1443 | C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult); |
| 1444 | if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) { |
| 1445 | auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size()); |
| 1446 | if (Ret.second) |
| 1447 | Bundles.emplace_back(); |
| 1448 | Bundles[Ret.first->second].push_back(std::move(C)); |
| 1449 | } else { |
| 1450 | Bundles.emplace_back(); |
| 1451 | Bundles.back().push_back(std::move(C)); |
| 1452 | } |
| 1453 | }; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1454 | DenseSet<const Symbol *> UsedIndexResults; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1455 | auto CorrespondingIndexResult = |
| 1456 | [&](const CodeCompletionResult &SemaResult) -> const Symbol * { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 1457 | if (auto SymID = |
| 1458 | getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1459 | auto I = IndexResults.find(*SymID); |
| 1460 | if (I != IndexResults.end()) { |
| 1461 | UsedIndexResults.insert(&*I); |
| 1462 | return &*I; |
| 1463 | } |
| 1464 | } |
| 1465 | return nullptr; |
| 1466 | }; |
| 1467 | // Emit all Sema results, merging them with Index results if possible. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1468 | for (auto &SemaResult : Recorder->Results) |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1469 | AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1470 | // Now emit any Index-only results. |
| 1471 | for (const auto &IndexResult : IndexResults) { |
| 1472 | if (UsedIndexResults.count(&IndexResult)) |
| 1473 | continue; |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1474 | AddToBundles(/*SemaResult=*/nullptr, &IndexResult); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1475 | } |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1476 | // We only keep the best N results at any time, in "native" format. |
| 1477 | TopN<ScoredBundle, ScoredBundleGreater> Top( |
| 1478 | Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit); |
| 1479 | for (auto &Bundle : Bundles) |
| 1480 | addCandidate(Top, std::move(Bundle)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1481 | return std::move(Top).items(); |
| 1482 | } |
| 1483 | |
Sam McCall | 80ad707 | 2018-06-08 13:32:25 +0000 | [diff] [blame] | 1484 | Optional<float> fuzzyScore(const CompletionCandidate &C) { |
| 1485 | // Macros can be very spammy, so we only support prefix completion. |
| 1486 | // We won't end up with underfull index results, as macros are sema-only. |
| 1487 | if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro && |
| 1488 | !C.Name.startswith_lower(Filter->pattern())) |
| 1489 | return None; |
| 1490 | return Filter->match(C.Name); |
| 1491 | } |
| 1492 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1493 | // Scores a candidate and adds it to the TopN structure. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1494 | void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates, |
| 1495 | CompletionCandidate::Bundle Bundle) { |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1496 | SymbolQualitySignals Quality; |
| 1497 | SymbolRelevanceSignals Relevance; |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1498 | Relevance.Context = Recorder->CCContext.getKind(); |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 1499 | Relevance.Query = SymbolRelevanceSignals::CodeComplete; |
Sam McCall | f84dd02 | 2018-07-05 08:26:53 +0000 | [diff] [blame] | 1500 | Relevance.FileProximityMatch = FileProximity.getPointer(); |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1501 | if (ScopeProximity) |
| 1502 | Relevance.ScopeProximityMatch = ScopeProximity.getPointer(); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1503 | if (PreferredType) |
| 1504 | Relevance.HadContextType = true; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1505 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1506 | auto &First = Bundle.front(); |
| 1507 | if (auto FuzzyScore = fuzzyScore(First)) |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1508 | Relevance.NameMatch = *FuzzyScore; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1509 | else |
| 1510 | return; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1511 | SymbolOrigin Origin = SymbolOrigin::Unknown; |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1512 | bool FromIndex = false; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1513 | for (const auto &Candidate : Bundle) { |
| 1514 | if (Candidate.IndexResult) { |
| 1515 | Quality.merge(*Candidate.IndexResult); |
| 1516 | Relevance.merge(*Candidate.IndexResult); |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1517 | Origin |= Candidate.IndexResult->Origin; |
| 1518 | FromIndex = true; |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1519 | if (!Candidate.IndexResult->Type.empty()) |
| 1520 | Relevance.HadSymbolType |= true; |
| 1521 | if (PreferredType && |
| 1522 | PreferredType->raw() == Candidate.IndexResult->Type) { |
| 1523 | Relevance.TypeMatchesPreferred = true; |
| 1524 | } |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1525 | } |
| 1526 | if (Candidate.SemaResult) { |
| 1527 | Quality.merge(*Candidate.SemaResult); |
| 1528 | Relevance.merge(*Candidate.SemaResult); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1529 | if (PreferredType) { |
| 1530 | if (auto CompletionType = OpaqueType::fromCompletionResult( |
| 1531 | Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) { |
| 1532 | Relevance.HadSymbolType |= true; |
| 1533 | if (PreferredType == CompletionType) |
| 1534 | Relevance.TypeMatchesPreferred = true; |
| 1535 | } |
| 1536 | } |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1537 | Origin |= SymbolOrigin::AST; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1538 | } |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1541 | CodeCompletion::Scores Scores; |
| 1542 | Scores.Quality = Quality.evaluate(); |
| 1543 | Scores.Relevance = Relevance.evaluate(); |
| 1544 | Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance); |
| 1545 | // NameMatch is in fact a multiplier on total score, so rescoring is sound. |
| 1546 | Scores.ExcludingName = Relevance.NameMatch |
| 1547 | ? Scores.Total / Relevance.NameMatch |
| 1548 | : Scores.Quality; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1549 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1550 | dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1551 | to_string(Origin), Scores.Total, to_string(Quality), |
| 1552 | to_string(Relevance)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1553 | |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1554 | NSema += bool(Origin & SymbolOrigin::AST); |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1555 | NIndex += FromIndex; |
| 1556 | NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1557 | if (Candidates.push({std::move(Bundle), Scores})) |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 1558 | Incomplete = true; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1561 | CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1562 | Optional<CodeCompletionBuilder> Builder; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1563 | for (const auto &Item : Bundle) { |
| 1564 | CodeCompletionString *SemaCCS = |
| 1565 | Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult) |
| 1566 | : nullptr; |
| 1567 | if (!Builder) |
| 1568 | Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS, |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1569 | QueryScopes, *Inserter, FileName, |
| 1570 | Recorder->CCContext.getKind(), Opts); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1571 | else |
| 1572 | Builder->add(Item, SemaCCS); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 1573 | } |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1574 | return Builder->build(); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1575 | } |
| 1576 | }; |
| 1577 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1578 | Expected<StringRef> speculateCompletionFilter(StringRef Content, Position Pos) { |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1579 | auto Offset = positionToOffset(Content, Pos); |
| 1580 | if (!Offset) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1581 | return make_error<StringError>( |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1582 | "Failed to convert position to offset in content.", |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1583 | inconvertibleErrorCode()); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1584 | if (*Offset == 0) |
| 1585 | return ""; |
| 1586 | |
| 1587 | // Start from the character before the cursor. |
| 1588 | int St = *Offset - 1; |
| 1589 | // FIXME(ioeric): consider UTF characters? |
| 1590 | auto IsValidIdentifierChar = [](char c) { |
| 1591 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 1592 | (c >= '0' && c <= '9') || (c == '_')); |
| 1593 | }; |
| 1594 | size_t Len = 0; |
| 1595 | for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) { |
| 1596 | } |
| 1597 | if (Len > 0) |
| 1598 | St++; // Shift to the first valid character. |
| 1599 | return Content.substr(St, Len); |
| 1600 | } |
| 1601 | |
| 1602 | CodeCompleteResult |
| 1603 | codeComplete(PathRef FileName, const tooling::CompileCommand &Command, |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1604 | const PreambleData *Preamble, StringRef Contents, Position Pos, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1605 | IntrusiveRefCntPtr<vfs::FileSystem> VFS, |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1606 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 1607 | CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) { |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1608 | return CodeCompleteFlow(FileName, |
| 1609 | Preamble ? Preamble->Includes : IncludeStructure(), |
| 1610 | SpecFuzzyFind, Opts) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1611 | .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs}); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1614 | SignatureHelp signatureHelp(PathRef FileName, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 1615 | const tooling::CompileCommand &Command, |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1616 | const PreambleData *Preamble, StringRef Contents, |
| 1617 | Position Pos, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1618 | IntrusiveRefCntPtr<vfs::FileSystem> VFS, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1619 | std::shared_ptr<PCHContainerOperations> PCHs, |
Sam McCall | 046557b | 2018-09-03 16:37:59 +0000 | [diff] [blame] | 1620 | const SymbolIndex *Index) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1621 | SignatureHelp Result; |
| 1622 | clang::CodeCompleteOptions Options; |
| 1623 | Options.IncludeGlobals = false; |
| 1624 | Options.IncludeMacros = false; |
| 1625 | Options.IncludeCodePatterns = false; |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 1626 | Options.IncludeBriefComments = false; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1627 | IncludeStructure PreambleInclusions; // Unused for signatureHelp |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1628 | semaCodeComplete( |
| 1629 | llvm::make_unique<SignatureHelpCollector>(Options, Index, Result), |
| 1630 | Options, |
| 1631 | {FileName, Command, Preamble, Contents, Pos, std::move(VFS), |
| 1632 | std::move(PCHs)}); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1633 | return Result; |
| 1634 | } |
| 1635 | |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 1636 | bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) { |
| 1637 | using namespace clang::ast_matchers; |
| 1638 | auto InTopLevelScope = hasDeclContext( |
| 1639 | anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl())); |
| 1640 | return !match(decl(anyOf(InTopLevelScope, |
| 1641 | hasDeclContext( |
| 1642 | enumDecl(InTopLevelScope, unless(isScoped()))))), |
| 1643 | ND, ASTCtx) |
| 1644 | .empty(); |
| 1645 | } |
| 1646 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1647 | CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const { |
| 1648 | CompletionItem LSP; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1649 | const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0]; |
| 1650 | LSP.label = ((InsertInclude && InsertInclude->Insertion) |
| 1651 | ? Opts.IncludeIndicator.Insert |
| 1652 | : Opts.IncludeIndicator.NoInsert) + |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1653 | (Opts.ShowOrigins ? "[" + to_string(Origin) + "]" : "") + |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1654 | RequiredQualifier + Name + Signature; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1655 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1656 | LSP.kind = Kind; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1657 | LSP.detail = |
| 1658 | BundleSize > 1 ? formatv("[{0} overloads]", BundleSize) : ReturnType; |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 1659 | LSP.deprecated = Deprecated; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1660 | if (InsertInclude) |
| 1661 | LSP.detail += "\n" + InsertInclude->Header; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1662 | LSP.documentation = Documentation; |
| 1663 | LSP.sortText = sortText(Score.Total, Name); |
| 1664 | LSP.filterText = Name; |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1665 | LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name}; |
Fangrui Song | 445bdd1 | 2018-09-05 08:01:37 +0000 | [diff] [blame] | 1666 | // Merge continuous additionalTextEdits into main edit. The main motivation |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1667 | // behind this is to help LSP clients, it seems most of them are confused when |
| 1668 | // they are provided with additionalTextEdits that are consecutive to main |
| 1669 | // edit. |
| 1670 | // Note that we store additional text edits from back to front in a line. That |
| 1671 | // is mainly to help LSP clients again, so that changes do not effect each |
| 1672 | // other. |
| 1673 | for (const auto &FixIt : FixIts) { |
| 1674 | if (IsRangeConsecutive(FixIt.range, LSP.textEdit->range)) { |
| 1675 | LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText; |
| 1676 | LSP.textEdit->range.start = FixIt.range.start; |
| 1677 | } else { |
| 1678 | LSP.additionalTextEdits.push_back(FixIt); |
| 1679 | } |
| 1680 | } |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1681 | if (Opts.EnableSnippets) |
| 1682 | LSP.textEdit->newText += SnippetSuffix; |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1683 | |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1684 | // FIXME(kadircet): Do not even fill insertText after making sure textEdit is |
| 1685 | // compatible with most of the editors. |
| 1686 | LSP.insertText = LSP.textEdit->newText; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1687 | LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet |
| 1688 | : InsertTextFormat::PlainText; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1689 | if (InsertInclude && InsertInclude->Insertion) |
| 1690 | LSP.additionalTextEdits.push_back(*InsertInclude->Insertion); |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 1691 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1692 | return LSP; |
| 1693 | } |
| 1694 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1695 | raw_ostream &operator<<(raw_ostream &OS, const CodeCompletion &C) { |
| 1696 | // For now just lean on CompletionItem. |
| 1697 | return OS << C.render(CodeCompleteOptions()); |
| 1698 | } |
| 1699 | |
| 1700 | raw_ostream &operator<<(raw_ostream &OS, const CodeCompleteResult &R) { |
| 1701 | OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "") |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1702 | << " (" << getCompletionKindString(R.Context) << ")" |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1703 | << " items:\n"; |
| 1704 | for (const auto &C : R.Completions) |
| 1705 | OS << C << "\n"; |
| 1706 | return OS; |
| 1707 | } |
| 1708 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1709 | } // namespace clangd |
| 1710 | } // namespace clang |