From ddc483fd2a2ed8fa133e0e66ae8b414e0af7338e Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 17:36:08 +0800 Subject: [PATCH] feat(java): end a completed break or continue with a semicolon (#7493) Fixes #7358. Completing break or continue in Java inserted the bare keyword. As in IDEA's keyword completion, it now ends with a semicolon, or with a space inside a labeled statement where the keyword can name the label; nothing is added when the line already continues. return is left alone. --- src/editor/lsp/completion.test.ts | 39 +++++++++++++++++++++++++++++++ src/editor/lsp/completion.ts | 34 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts index 6c644ede8..82fae9df5 100644 --- a/src/editor/lsp/completion.test.ts +++ b/src/editor/lsp/completion.test.ts @@ -308,6 +308,45 @@ describe("applying completions", () => { expect(view.state.selection.main.head).toBe(expected.indexOf("|")); }); + it.each([ + { + name: "break in a switch case", + doc: "switch (c) {\n case A:\n bre|\n default:\n}", + label: "break", + expected: "switch (c) {\n case A:\n break;|\n default:\n}", + }, + { + name: "continue in a loop", + doc: "for (var e : list) {\n if (e == null) cont|\n}", + label: "continue", + expected: "for (var e : list) {\n if (e == null) continue;|\n}", + }, + { + name: "break inside a labeled loop, which can name the label", + doc: "outer:\nfor (int i = 0; i < n; i++) {\n while (true) {\n bre|\n }\n}", + label: "break", + expected: "outer:\nfor (int i = 0; i < n; i++) {\n while (true) {\n break |\n }\n}", + }, + { name: "break before its semicolon", doc: "bre|;", label: "break", expected: "break|;" }, + { name: "break before a label", doc: "bre| outer;", label: "break", expected: "break| outer;" }, + ])("completes a Java $name as IDEA does (#7358)", ({ doc, label, expected }) => { + const { view, req, pos } = setup(doc); + const typed = doc.slice(0, doc.indexOf("|")).match(/\w+$/)![0]; + apply( + view, + makeCompletion(item({ label, kind: "keyword", insertText: label }), { ...req, language: "java" }), + pos - typed.length, + ); + expect(view.state.doc.toString()).toBe(expected.replace("|", "")); + expect(view.state.selection.main.head).toBe(expected.indexOf("|")); + }); + + it("leaves other languages' break alone", () => { + const { view, req, pos } = setup("bre|"); + apply(view, makeCompletion(item({ label: "break", kind: "keyword", insertText: "break" }), req), pos - 3); + expect(view.state.doc.toString()).toBe("break"); + }); + it("replaces a Java annotation name before its existing arguments with Tab", () => { const { view, req, pos } = setup('@Sup|pressWarnings("unused")'); const suggestion = item({ diff --git a/src/editor/lsp/completion.ts b/src/editor/lsp/completion.ts index 0bc3dfc26..3a8790dc6 100644 --- a/src/editor/lsp/completion.ts +++ b/src/editor/lsp/completion.ts @@ -253,6 +253,8 @@ function completionInsertText( to: number, ): string { const { insertText } = item; + if (language === "java" && item.kind === "keyword" && JUMP_KEYWORDS.has(insertText)) + return insertText + javaJumpTail(doc, from, to); // JDT LS can append a space to a bare annotation name. It is useful only as a separator. if (language !== "java" || item.kind !== "interface" || insertText !== `${item.label} `) return insertText; const line = doc.lineAt(from); @@ -262,6 +264,38 @@ function completionInsertText( return item.label; } +const JUMP_KEYWORDS = new Set(["break", "continue"]); +const LABEL = + /[\n;{}]\s*(?!case\b|default\b)[\p{L}_$][\p{L}\p{N}_$]*\s*:\s*(?:for|while|do|switch)?\s*(?:\([^]*\))?\s*$/u; +/** How far back the enclosing statements are looked for. */ +const LABEL_SCAN = 20_000; + +/** + * What follows a completed `break` / `continue` in Java (#7358), as in IDEA: `;`, or a space for + * the label inside a labeled statement; nothing when the line already goes on (`break;`, `break outer`). + */ +function javaJumpTail(doc: Text, from: number, to: number): string { + const after = doc.sliceString(to, doc.lineAt(to).to); + if (/^\s*[;\p{L}\p{N}_$]/u.test(after)) return ""; + return insideLabeledStatement(doc, from) ? " " : ";"; +} + +/** Whether a block enclosing `pos` is a labeled statement's (`outer: for (…) {`); comments and strings are not skipped. */ +function insideLabeledStatement(doc: Text, pos: number): boolean { + const start = Math.max(0, pos - LABEL_SCAN); + const text = doc.sliceString(start, pos); + let depth = 0; + for (let i = text.length - 1; i >= 0; i--) { + const ch = text[i]; + if (ch === "}") depth++; + else if (ch === "{") { + if (depth > 0) depth--; + else if (LABEL.test("\n" + text.slice(Math.max(0, i - 400), i))) return true; + } + } + return false; +} + /** ↵ (`insert`) or ⇥ (`replace`: also overwrites the rest of the identifier). */ export function applyLspCompletion( view: EditorView,