[clangd] Expose Code Completion score to the client
authorKirill Bobyrev <kbobyrev@google.com>
Thu, 13 Feb 2020 13:17:30 +0000 (14:17 +0100)
committerKirill Bobyrev <kbobyrev@google.com>
Thu, 13 Feb 2020 14:05:18 +0000 (15:05 +0100)
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

clang-tools-extra/clangd/CodeComplete.cpp
clang-tools-extra/clangd/Protocol.cpp
clang-tools-extra/clangd/Protocol.h
clang-tools-extra/clangd/test/completion-auto-trigger.test
clang-tools-extra/clangd/test/completion-snippets.test
clang-tools-extra/clangd/test/completion.test
clang-tools-extra/clangd/test/protocol.test
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

index 6fc6c5d..52c1cee 100644 (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;
 }
 
index d607ffb..1e71c2a 100644 (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);
 }
 
index 3275cbb..80a11ee 100644 (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:
   //
index 46871b9..cf7261e 100644 (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",
index 22cd082..7dc9809 100644 (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})",
index 0094d47..d6376b3 100644 (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",
index 3e16c9e..5e852d1 100644 (file)
@@ -39,6 +39,7 @@ Content-Length: 146
 # CHECK-NEXT:        "insertTextFormat": 1,\r
 # CHECK-NEXT:        "kind": 5,\r
 # CHECK-NEXT:        "label": " a",\r
+# CHECK-NEXT:        "score": {{[0-9]+.[0-9]+}},\r
 # CHECK-NEXT:        "sortText": "{{.*}}"\r
 #      CHECK:    ]\r
 # CHECK-NEXT:  }\r
@@ -68,6 +69,7 @@ Content-Length: 146
 # CHECK-NEXT:        "insertTextFormat": 1,\r
 # CHECK-NEXT:        "kind": 5,\r
 # CHECK-NEXT:        "label": " a",\r
+# CHECK-NEXT:        "score": {{[0-9]+.[0-9]+}},\r
 # CHECK-NEXT:        "sortText": "{{.*}}"\r
 #      CHECK:    ]\r
 # CHECK-NEXT:  }\r
@@ -97,6 +99,7 @@ Content-Length: 146
 # CHECK-NEXT:        "insertTextFormat": 1,\r
 # CHECK-NEXT:        "kind": 5,\r
 # CHECK-NEXT:        "label": " a",\r
+# CHECK-NEXT:        "score": {{[0-9]+.[0-9]+}},\r
 # CHECK-NEXT:        "sortText": "{{.*}}"\r
 #      CHECK:    ]\r
 # CHECK-NEXT:  }\r
index 67b8abb..a39c743 100644 (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);