[clangd][vscode] Enable dot-to-arrow fixes in clangd completion.
authorHaojian Wu <hokein.wu@gmail.com>
Fri, 6 Mar 2020 14:08:23 +0000 (15:08 +0100)
committerHaojian Wu <hokein.wu@gmail.com>
Mon, 9 Mar 2020 13:02:08 +0000 (14:02 +0100)
Summary:
The previous issue is that the item was filtered out by vscode, because
the prefix (which contains ".") are not matched against the filterText.

This patch works around it by adjusting the item filterText, inspired by
https://reviews.llvm.org/D75623.

Reviewers: sammccall

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

Tags: #clang

Differential Revision: https://reviews.llvm.org/D75739

clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts

index 3d8ed2d..4749cd1 100644 (file)
@@ -65,6 +65,15 @@ class ClangdLanguageClient extends vscodelc.LanguageClient {
   }
 }
 
+class EnableEditsNearCursorFeature implements vscodelc.StaticFeature {
+  initialize() {}
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities): void {
+    const extendedCompletionCapabilities: any =
+        capabilities.textDocument.completion;
+    extendedCompletionCapabilities.editsNearCursor = true;
+  }
+}
+
 /**
  *  this method is called when your extension is activate
  *  your extension is activated the very first time the command is executed
@@ -107,17 +116,18 @@ export function activate(context: vscode.ExtensionContext) {
         // By adding the prefix to the beginning of the filterText, we get a perfect
         // fuzzymatch score for every item.
         // The sortText (which reflects clangd ranking) breaks the tie.
+        // This also prevents VSCode from filtering out any results due to the
+        // differences in how fuzzy filtering is applies, e.g. enable dot-to-arrow
+        // fixes in completion.
         //
         // We also have to mark the list as incomplete to force retrieving new rankings.
         // See https://github.com/microsoft/language-server-protocol/issues/898
         middleware: {
           provideCompletionItem: async (document, position, context, token, next) => {
-            // Get the incomplete identifier before the cursor.
-            let word = document.getWordRangeAtPosition(position);
-            let prefix = word && document.getText(new vscode.Range(word.start, position));
-            
             let list = await next(document, position, context, token);
             let items = (Array.isArray(list) ? list : list.items).map(item => {
+              // Gets the prefix used by vscode when doing fuzzymatch.
+              let prefix = document.getText(new vscode.Range(item.range.start, position))
               if (prefix)
                 item.filterText = prefix + "_" + item.filterText;
               return item;
@@ -137,6 +147,7 @@ export function activate(context: vscode.ExtensionContext) {
         vscode.Disposable.from(semanticHighlightingFeature));
     clangdClient.registerFeature(semanticHighlightingFeature);
   }
+  clangdClient.registerFeature(new EnableEditsNearCursorFeature);
   console.log('Clang Language Server is now active!');
   context.subscriptions.push(clangdClient.start());
   context.subscriptions.push(vscode.commands.registerCommand(