diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -96,12 +96,13 @@ return; } - SymbolKind SK = indexSymbolKindToSymbolKind(Sym.SymInfo.Kind); - std::string Scope = std::string(Sym.Scope); - llvm::StringRef ScopeRef = Scope; - ScopeRef.consume_back("::"); - SymbolInformation Info = {(Sym.Name + Sym.TemplateSpecializationArgs).str(), - SK, *Loc, std::string(ScopeRef)}; + llvm::StringRef Scope = Sym.Scope; + Scope.consume_back("::"); + SymbolInformation Info; + Info.name = (Sym.Name + Sym.TemplateSpecializationArgs).str(); + Info.kind = indexSymbolKindToSymbolKind(Sym.SymInfo.Kind); + Info.location = *Loc; + Info.containerName = Scope.str(); SymbolQualitySignals Quality; Quality.merge(Sym); @@ -121,6 +122,8 @@ dlog("FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name, Score, Quality, Relevance); + // Exposed score excludes fuzzy-match component, for client-side re-ranking. + Info.score = Score / Relevance.NameMatch; Top.push({Score, std::move(Info)}); }); for (auto &R : std::move(Top).items()) diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -1015,6 +1015,14 @@ /// The name of the symbol containing this symbol. std::string containerName; + + /// The score that clangd calculates to rank the returned symbols. + /// This excludes the fuzzy-matching score between `name` and the query. + /// (Specifically, the last ::-separated component). + /// This can be used to re-rank results as the user types, using client-side + /// fuzzy-matching (that score should be multiplied with this one). + /// This is a clangd extension, set only for workspace/symbol responses. + llvm::Optional score; }; llvm::json::Value toJSON(const SymbolInformation &); llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolInformation &); @@ -1175,11 +1183,11 @@ /// Indicates if this item is deprecated. bool deprecated = false; - /// This is Clangd extension. - /// The score that Clangd calculates to rank completion items. This score can - /// be used to adjust the ranking on the client side. - /// NOTE: This excludes fuzzy matching score which is typically calculated on - /// the client side. + /// The score that clangd calculates to rank the returned completions. + /// This excludes the fuzzy-match between `filterText` and the partial word. + /// This can be used to re-rank results as the user types, using client-side + /// fuzzy-matching (that score should be multiplied with this one). + /// This is a clangd extension. float score = 0.f; // TODO: Add custom commitCharacters for some of the completion items. For diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp --- a/clang-tools-extra/clangd/Protocol.cpp +++ b/clang-tools-extra/clangd/Protocol.cpp @@ -662,12 +662,15 @@ } llvm::json::Value toJSON(const SymbolInformation &P) { - return llvm::json::Object{ + llvm::json::Object O{ {"name", P.name}, {"kind", static_cast(P.kind)}, {"location", P.location}, {"containerName", P.containerName}, }; + if (P.score) + O["score"] = *P.score; + return std::move(O); } llvm::raw_ostream &operator<<(llvm::raw_ostream &O, diff --git a/clang-tools-extra/clangd/test/symbols.test b/clang-tools-extra/clangd/test/symbols.test --- a/clang-tools-extra/clangd/test/symbols.test +++ b/clang-tools-extra/clangd/test/symbols.test @@ -23,7 +23,8 @@ # CHECK-NEXT: }, # CHECK-NEXT: "uri": "file://{{.*}}/vector.h" # CHECK-NEXT: }, -# CHECK-NEXT: "name": "vector" +# CHECK-NEXT: "name": "vector", +# CHECK-NEXT: "score": {{.*}} # CHECK-NEXT: } # CHECK-NEXT: ] # CHECK-NEXT:}