diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts --- a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts @@ -65,6 +65,14 @@ } } +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 @@ -112,12 +120,27 @@ // 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 => { + // ^ is the position. + // Example 1: std::u_p^ + // Example 2: a.f^ + // + // When run code completion in VSCode, what happens: + // - clangd returns a completion item with range [[u_p]] and filterText "unique_ptr" + // - vscode extracts the word (prefix we called) from the range , which is "u_p" + // - vscode fuzzy-matches the filterText "unique_ptr" against the prefix "u_p" + // + // For example 2, the completion item range is [[.f]], filterText + // is "foo", insertText is "->foo". ".f" is the prefix used to do + // fuzzymatch, and the filterText "foo" is not matched, therefore + // the item is filtered out and not shown in the UI. + // + // Adding the prefix to the filterText will prevent VSCode filtering + // it out. + + // 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 +160,7 @@ 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(