Skip to content

Commit

Permalink
[clangd] Add score extension to workspace/symbol response.
Browse files Browse the repository at this point in the history
The protocol doesn't really incorporate ranking.
As with code completion, most clients respect what the server sends, but
VSCode re-ranks items, with predictable results.
See clangd/vscode-clangd#81

There's no filterText field so we may be unable to construct a good workaround.
But expose the score so we may be able to do this on the client in future.

Differential Revision: https://reviews.llvm.org/D88844
  • Loading branch information
sam-mccall authored and memfrob committed Oct 4, 2022
1 parent 1ec8f39 commit ea3eee9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
15 changes: 9 additions & 6 deletions clang-tools-extra/clangd/FindSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ getWorkspaceSymbols(llvm::StringRef Query, int Limit,
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);
Expand All @@ -121,6 +122,8 @@ getWorkspaceSymbols(llvm::StringRef Query, int Limit,
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())
Expand Down
5 changes: 4 additions & 1 deletion clang-tools-extra/clangd/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,15 @@ bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R,
}

llvm::json::Value toJSON(const SymbolInformation &P) {
return llvm::json::Object{
llvm::json::Object O{
{"name", P.name},
{"kind", static_cast<int>(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,
Expand Down
18 changes: 13 additions & 5 deletions clang-tools-extra/clangd/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,14 @@ struct SymbolInformation {

/// 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<float> score;
};
llvm::json::Value toJSON(const SymbolInformation &);
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolInformation &);
Expand Down Expand Up @@ -1175,11 +1183,11 @@ struct CompletionItem {
/// 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
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/test/symbols.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:}
Expand Down

0 comments on commit ea3eee9

Please sign in to comment.