fix(java): remove extra spaces from annotation completions (#6989)

JDT LS appends a space to annotation completions; it is dropped when nothing follows that needs a separator, so no trailing whitespace after @Override or space before the parenthesis.

Co-authored-by: Chris-young42 <[email protected]>
Цей коміт міститься в:
Chris-young42таChris-young42 authored and GitHub committed 2026-11-30 17:42:42 +08:00
1 джерело c4f7fec86c
коміт a441b2a906
2 files changed
+81 -4

No files matched your search

+57
Переглянути файл
@@ -283,6 +283,63 @@ describe("applying completions", () => {
expect(view.state.selection.main.head).toBe(14);
});
it.each([
{ doc: " @Over|\n", prefix: "Over", label: "Override", expected: " @Override|\n" },
{ doc: '@Supp|("unused")', prefix: "Supp", label: "SuppressWarnings", expected: '@SuppressWarnings|("unused")' },
{ doc: "@java.lang.Over|", prefix: "Over", label: "Override", expected: "@java.lang.Override|" },
{
doc: "@Over| public void run() {}",
prefix: "Over",
label: "Override",
expected: "@Override| public void run() {}",
},
{
doc: "@Over|public void run() {}",
prefix: "Over",
label: "Override",
expected: "@Override |public void run() {}",
},
{ doc: "implements Run|\n", prefix: "Run", label: "Runnable", expected: "implements Runnable |\n" },
])("applies a Java interface completion in $doc", ({ doc, prefix, label, expected }) => {
const { view, req, pos } = setup(doc);
const suggestion = item({ label, kind: "interface", insertText: `${label} `, isSnippet: true });
apply(view, makeCompletion(suggestion, { ...req, language: "java" }), pos - prefix.length);
expect(view.state.doc.toString()).toBe(expected.replace("|", ""));
expect(view.state.selection.main.head).toBe(expected.indexOf("|"));
});
it("replaces a Java annotation name before its existing arguments with Tab", () => {
const { view, req, pos } = setup('@Sup|pressWarnings("unused")');
const suggestion = item({
label: "SuppressWarnings",
kind: "interface",
insertText: "SuppressWarnings ",
isSnippet: true,
replaceRange: { start: { line: 0, col: 1 }, end: { line: 0, col: pos } },
replaceRangeTab: { start: { line: 0, col: 1 }, end: { line: 0, col: 17 } },
});
applyLspCompletion(view, makeCompletion(suggestion, { ...req, language: "java" }), 1, pos, "replace");
expect(view.state.doc.toString()).toBe('@SuppressWarnings("unused")');
expect(view.state.selection.main.head).toBe(17);
});
it("keeps spacing in a Java annotation attribute completion", () => {
const { view, req } = setup("@Tag(va|)");
const suggestion = item({ label: "value", kind: "field", insertText: "value = ", isSnippet: true });
apply(view, makeCompletion(suggestion, { ...req, language: "java" }), 5);
expect(view.state.doc.toString()).toBe("@Tag(value = )");
expect(view.state.selection.main.head).toBe(13);
});
it("accounts for what follows each Java annotation when completing at several carets", () => {
const { view, req } = setup("@Ov|\n@Ovpublic void run() {}", "", [EditorState.allowMultipleSelections.of(true)]);
view.dispatch({ selection: EditorSelection.create([EditorSelection.cursor(3), EditorSelection.cursor(7)]) });
const suggestion = item({ label: "Override", kind: "interface", insertText: "Override ", isSnippet: true });
applyLspCompletion(view, makeCompletion(suggestion, { ...req, language: "java" }), 1, 3, "insert");
expect(view.state.doc.toString()).toBe("@Override\n@Override public void run() {}");
expect(view.state.selection.ranges.map((r) => r.head)).toEqual([9, 20]);
});
it("lets Tab step out of the parentheses a completion inserted", () => {
const { view, req } = setup("service.cr|", "", [tabOut]);
applyLspCompletion(view, makeCompletion(item(), req), 8, 10, "insert");
+24 -4
Переглянути файл
@@ -245,6 +245,23 @@ function applyLsp(view: EditorView, completion: Completion, from: number, to: nu
applyLspCompletion(view, completion, from, to, replaceMode ? "replace" : "insert");
}
function completionInsertText(
item: CompletionItem,
language: string | null,
doc: Text,
from: number,
to: number,
): string {
const { insertText } = item;
// 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);
const before = line.text.slice(0, from - line.from);
const after = doc.sliceString(to, to + 2);
if (!/@(?:[\p{L}\p{N}\p{M}_$]+\.)*$/u.test(before) || identifierEnd(after, 0) > 0) return insertText;
return item.label;
}
/** ↵ (`insert`) or ⇥ (`replace`: also overwrites the rest of the identifier). */
export function applyLspCompletion(
view: EditorView,
@@ -291,6 +308,7 @@ export function applyLspCompletion(
return;
}
const insertText = completionInsertText(item, req.language, state.doc, start, end);
let insertedParens: ReturnType<typeof callParens> = null;
let bareCall: number | null = null;
if (item.isSnippet) {
@@ -300,7 +318,7 @@ export function applyLspCompletion(
start = tr.changes.mapPos(start, 1);
end = tr.changes.mapPos(end, 1);
}
const source = lspSnippetToCm(item.insertText, snippetVariables(req.path));
const source = lspSnippetToCm(insertText, snippetVariables(req.path));
applySnippet(view, [{ from: start, to: end, snippet: source }], completion);
// A call snippet without a stop (`ArrayList<>()`): the caret goes inside until
// signature help says there is nothing to pass.
@@ -320,7 +338,7 @@ export function applyLspCompletion(
? callParens(item, state.doc.lineAt(start), start, end)
: null;
insertedParens = parens;
const changes = state.changes([...extra, { from: start, to: end, insert: item.insertText + (parens ? "()" : "") }]);
const changes = state.changes([...extra, { from: start, to: end, insert: insertText + (parens ? "()" : "") }]);
const caret = changes.mapPos(end, 1) - (parens === "inside" ? 1 : 0);
view.dispatch({
changes,
@@ -391,11 +409,13 @@ function applyAtCarets(
// Parentheses depend on what follows each site: `a.cr|` gets them, `b.cr|()` does not.
const textAt = (site: { from: number; to: number }): { text: string; caret: number } => {
if (item.isSnippet) return lspSnippetToPlainWithCaret(item.insertText, snippetVariables(completion.req.path));
const { language, path } = completion.req;
const insertText = completionInsertText(item, language, state.doc, site.from, site.to);
if (item.isSnippet) return lspSnippetToPlainWithCaret(insertText, snippetVariables(path));
const parens = editorPrefs().completion.insertParentheses
? callParens(item, state.doc.lineAt(site.from), site.from, site.to)
: null;
const text = item.insertText + (parens ? "()" : "");
const text = insertText + (parens ? "()" : "");
return { text, caret: text.length - (parens === "inside" ? 1 : 0) };
};
const texts = sites.map(textAt);