From a9714d98ca8bfab6e25c39e8b51e09edf685ea1c Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:38:44 +0800 Subject: [PATCH] fix(completion): complete a keyword rather than a declaration snippet of the same name jdtls lists both the keyword `interface` and a snippet `interface` that inserts a whole declaration named after the file. Picking the snippet while changing `class` to `interface` duplicated the declaration. A server snippet whose label matches a keyword the same list offers is dropped, as IDEA completes only the keyword. Fixes #7151 --- src/editor/lsp/completion.test.ts | 35 +++++++++++++++++++++++++++++++ src/editor/lsp/completion.ts | 4 ++++ 2 files changed, 39 insertions(+) diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts index 4e5ae58b59..6c644ede84 100644 --- a/src/editor/lsp/completion.test.ts +++ b/src/editor/lsp/completion.test.ts @@ -540,6 +540,41 @@ describe("applying completions", () => { expect(a!.apply).not.toBe(b!.apply); }); + it("completes a keyword rather than a declaration snippet with the same name", async () => { + const { view } = setup("public interf| Demo {}"); + setInvokeRoute((cmd) => { + if (cmd === "cancel_request") return Promise.resolve(null); + if (cmd === "lsp_completion") { + return Promise.resolve({ + isIncomplete: false, + items: [ + item({ label: "interface", kind: "keyword", insertText: "interface" }), + item({ + label: "interface", + kind: "snippet", + insertText: "public interface ${1:Demo} {\n\t$0\n}", + isSnippet: true, + }), + ], + }); + } + return Promise.resolve(null); + }); + try { + view.dispatch({ + effects: StateEffect.appendConfig.of([ + lspDoc.of({ doc: 7151, path: "/p/Demo.java", language: "java", uiMode: "Java", readOnly: false }), + completionExtension(), + ]), + }); + startCompletion(view); + await vi.waitFor(() => expect(currentCompletions(view.state)).toHaveLength(1)); + expect((currentCompletions(view.state)[0] as unknown as { lsp: CompletionItem }).lsp.kind).toBe("keyword"); + } finally { + setInvokeRoute(null); + } + }); + it("lists same-name Java imports from different packages", async () => { const { view } = setup("@RequestBod|"); const packages = ["org.springframework.web.bind.annotation", "io.swagger.v3.oas.annotations.parameters"]; diff --git a/src/editor/lsp/completion.ts b/src/editor/lsp/completion.ts index 7201cb89ea..0bc3dfc266 100644 --- a/src/editor/lsp/completion.ts +++ b/src/editor/lsp/completion.ts @@ -875,6 +875,10 @@ async function lspCompletionSource(ctx: CompletionContext): Promise !it.label.startsWith("--")); const pruned = pruneItems(items, typed); items = [...pruned.items]; + // A server snippet named like a keyword it also lists (jdtls `interface`, `class`, `enum` declaration + // skeletons) would replace one word with a whole declaration; IDEA completes the keyword. + const keywords = new Set(items.filter((it) => it.kind === "keyword").map((it) => it.label)); + if (keywords.size) items = items.filter((it) => it.kind !== "snippet" || !keywords.has(it.label)); // A server snippet named like a live template (jdtls `sout`, `fori`) would be listed twice; the template wins. if (items.some((it) => it.kind === "snippet")) { const own = await contributedSource(ctx);