дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
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.
Цей коміт міститься в:
1 джерело
23d81a2b3d
коміт
ddc483fd2a
2 files changed
+73
No files matched your search
@@ -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({
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Заблокувати користувача