From 336471c060d47bb9d472913f9b4603f820c4c9e0 Mon Sep 17 00:00:00 2001 From: argothiel Date: Mon, 4 May 2026 23:28:01 +0200 Subject: [PATCH 1/2] Fix completion crash when item.range starts using {inserting, replacing} The LSP spec (since 3.16) allows CompletionItem.range to be either a plain Range or an InsertReplaceRange ({inserting, replacing}). The old code cast item.range directly to vscode.Range; once clangd starts sending the newer form, this will crash (llvm/llvm-project#187623). Narrow the type before extracting the start position, and guard against a missing range entirely. --- src/clangd-context.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/clangd-context.ts b/src/clangd-context.ts index 4e51365a..82eb8807 100644 --- a/src/clangd-context.ts +++ b/src/clangd-context.ts @@ -138,8 +138,15 @@ export class ClangdContext implements vscode.Disposable { let items = (!list ? [] : Array.isArray(list) ? list : list.items); items = items.map(item => { // Gets the prefix used by VSCode when doing fuzzymatch. - let prefix = document.getText( - new vscode.Range((item.range as vscode.Range).start, position)) + // item.range is either a Range or {inserting, replacing} (see + // CompletionItem in the VS Code API); narrow before using. + let prefix = ''; + if (item.range) { + const start = item.range instanceof vscode.Range + ? item.range.start + : item.range.inserting.start; + prefix = document.getText(new vscode.Range(start, position)); + } if (prefix) item.filterText = prefix + '_' + item.filterText; // Workaround for https://github.com/clangd/vscode-clangd/issues/357 From f56d5f399fe6058d3083024256efb1fd6327d392 Mon Sep 17 00:00:00 2001 From: argothiel Date: Mon, 4 May 2026 23:58:52 +0200 Subject: [PATCH 2/2] fixup! Fix completion crash when item.range starts using {inserting, replacing} Fix old formatting issue. --- src/clangd-context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clangd-context.ts b/src/clangd-context.ts index 82eb8807..252850dc 100644 --- a/src/clangd-context.ts +++ b/src/clangd-context.ts @@ -148,7 +148,7 @@ export class ClangdContext implements vscode.Disposable { prefix = document.getText(new vscode.Range(start, position)); } if (prefix) - item.filterText = prefix + '_' + item.filterText; + item.filterText = prefix + '_' + item.filterText; // Workaround for https://github.com/clangd/vscode-clangd/issues/357 // clangd's used of commit-characters was well-intentioned, but // overall UX is poor. Due to vscode-languageclient bugs, we didn't