[clangd] Expose Code Completion score to the client

Summary:
Make it possible for the client to adjust the ranking by using the score Clangd
calculates for the completion items.

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D74547
This commit is contained in:
Kirill Bobyrev 2020-02-13 14:17:30 +01:00
parent b3a0c4d7dc
commit ff7b5bac04
No known key found for this signature in database
GPG Key ID: 2307C055C8384FA0
8 changed files with 21 additions and 0 deletions

View File

@ -1850,6 +1850,8 @@ CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
if (InsertInclude && InsertInclude->Insertion)
LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
LSP.score = Score.ExcludingName;
return LSP;
}

View File

@ -869,6 +869,7 @@ llvm::json::Value toJSON(const CompletionItem &CI) {
Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
if (CI.deprecated)
Result["deprecated"] = CI.deprecated;
Result["score"] = CI.score;
return std::move(Result);
}

View File

@ -1094,6 +1094,13 @@ 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.
float score = 0.f;
// TODO(krasimir): The following optional fields defined by the language
// server protocol are unsupported:
//

View File

@ -24,6 +24,7 @@
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " size",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}size",
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "size",
@ -46,6 +47,7 @@
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 10,
# CHECK-NEXT: "label": " default_capacity",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}default_capacity",
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "default_capacity",
@ -86,6 +88,7 @@
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 6,
# CHECK-NEXT: "label": " ns_member",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}ns_member",
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "ns_member",

View File

@ -33,6 +33,7 @@
# CHECK-NEXT: "insertTextFormat": 2,
# CHECK-NEXT: "kind": 3,
# CHECK-NEXT: "label": " func_with_args(int a, int b)",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}func_with_args"
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "func_with_args(${1:int a}, ${2:int b})",

View File

@ -17,6 +17,7 @@
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " a",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}a"
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "a",
@ -50,6 +51,7 @@
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " b",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}b"
# CHECK-NEXT: "textEdit": {
# CHECK-NEXT: "newText": "b",

View File

@ -39,6 +39,7 @@ Content-Length: 146
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " a",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}"
# CHECK: ]
# CHECK-NEXT: }
@ -68,6 +69,7 @@ Content-Length: 146
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " a",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}"
# CHECK: ]
# CHECK-NEXT: }
@ -97,6 +99,7 @@ Content-Length: 146
# CHECK-NEXT: "insertTextFormat": 1,
# CHECK-NEXT: "kind": 5,
# CHECK-NEXT: "label": " a",
# CHECK-NEXT: "score": {{[0-9]+.[0-9]+}},
# CHECK-NEXT: "sortText": "{{.*}}"
# CHECK: ]
# CHECK-NEXT: }

View File

@ -1627,6 +1627,7 @@ TEST(CompletionTest, Render) {
Include.Header = "\"foo.h\"";
C.Kind = CompletionItemKind::Method;
C.Score.Total = 1.0;
C.Score.ExcludingName = .5;
C.Origin = SymbolOrigin::AST | SymbolOrigin::Static;
CodeCompleteOptions Opts;
@ -1644,6 +1645,7 @@ TEST(CompletionTest, Render) {
EXPECT_THAT(R.additionalTextEdits, IsEmpty());
EXPECT_EQ(R.sortText, sortText(1.0, "x"));
EXPECT_FALSE(R.deprecated);
EXPECT_EQ(R.score, .5f);
Opts.EnableSnippets = true;
R = C.render(Opts);