дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
fix(completion): put the caret inside a Kotlin call that takes arguments
kotlin-lsp inserts a picked item itself, with its parentheses, and the caret was moved after the whole edit: `read()|` for `read(name: String)`, so typing an argument went after the call. When the item's signature lists parameters and the edit ends with `()`, the caret now goes between them.
Цей коміт міститься в:
1 джерело
51bab9a25b
коміт
3322ecc527
2 files changed
+36
-3
No files matched your search
@@ -323,6 +323,22 @@ describe("applying completions", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("puts the caret inside the parentheses of a server-inserted call that takes arguments", async () => {
|
||||
setInvokeRoute(() => Promise.resolve(null));
|
||||
try {
|
||||
const { view, req } = setup("x.rea|\n", "", [completionExtension()]);
|
||||
view.dispatch({ effects: StateEffect.appendConfig.of(lspDoc.of({ doc: 1 } as EditorDocContext)) });
|
||||
const command = { title: "Apply Completion", command: "jetbrains.kotlin.completion.apply", arguments: ["1"] };
|
||||
const kotlin = item({ label: "read", kind: "method", labelDetail: "(name: String)", insertText: "", command });
|
||||
applyLspCompletion(view, makeCompletion(kotlin, req), 5, 5, "insert");
|
||||
view.dispatch({ changes: { from: 5, insert: "d()" }, annotations: remoteChange.of(true) });
|
||||
expect(view.state.doc.toString()).toBe("x.read()\n");
|
||||
expect(view.state.selection.main.head).toBe(7);
|
||||
} finally {
|
||||
setInvokeRoute(null);
|
||||
}
|
||||
});
|
||||
|
||||
it("completes at every caret whose typed prefix matches the main caret's", () => {
|
||||
const { view, req } = setup("a.cr|\nb.cr\nc.xy", "", [EditorState.allowMultipleSelections.of(true)]);
|
||||
// Carets after both `cr`s (the main one first) and after `xy`.
|
||||
|
||||
@@ -462,6 +462,8 @@ async function applyLateEdits(view: EditorView, c: LspCompletion, insertedAt: nu
|
||||
interface ServerInsert {
|
||||
at: number;
|
||||
others: readonly number[];
|
||||
/** The item is a call that takes arguments: the caret goes inside an inserted `()`. */
|
||||
inside?: boolean;
|
||||
}
|
||||
const awaitServerInsert = StateEffect.define<ServerInsert | null>();
|
||||
const serverInsertAt = StateField.define<ServerInsert | null>({
|
||||
@@ -470,7 +472,11 @@ const serverInsertAt = StateField.define<ServerInsert | null>({
|
||||
for (const e of tr.effects) if (e.is(awaitServerInsert)) return e.value;
|
||||
// The edit landed, or the caret moved on before it did.
|
||||
if (value === null || tr.annotation(remoteChange) || tr.selection) return null;
|
||||
return { at: tr.changes.mapPos(value.at, -1), others: value.others.map((p) => tr.changes.mapPos(p, -1)) };
|
||||
return {
|
||||
at: tr.changes.mapPos(value.at, -1),
|
||||
others: value.others.map((p) => tr.changes.mapPos(p, -1)),
|
||||
inside: value.inside,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -541,7 +547,9 @@ const caretAfterServerInsert = EditorState.transactionFilter.of((tr) => {
|
||||
if (tr.changes.touchesRange(from, to)) continue;
|
||||
others.push({ from: tr.changes.mapPos(from, 1), to: tr.changes.mapPos(to, -1) });
|
||||
}
|
||||
const selection = EditorSelection.cursor(mainAfter);
|
||||
const inside =
|
||||
pending.inside && /\(\)$/.test(landed.insert) && tr.newDoc.sliceString(mainAfter - 2, mainAfter) === "()";
|
||||
const selection = EditorSelection.cursor(inside ? mainAfter - 1 : mainAfter);
|
||||
if (!others.length) return [tr, { selection, sequential: true }];
|
||||
// Not part of this transaction: a remote one is not sent back to the document model.
|
||||
const changes = others
|
||||
@@ -553,6 +561,12 @@ const caretAfterServerInsert = EditorState.transactionFilter.of((tr) => {
|
||||
/** Client-side commands handled by the editor itself (VS Code ids servers commonly send). */
|
||||
const CLIENT_COMMANDS = new Set(["editor.action.triggerParameterHints", "editor.action.triggerSuggest"]);
|
||||
|
||||
/** A function or method whose signature lists parameters (`(name: String)`), as far as the item tells. */
|
||||
function takesArguments(item: Pick<CompletionItem, "kind" | "labelDetail" | "detail">): boolean {
|
||||
if (item.kind && !CALLABLE.has(item.kind)) return false;
|
||||
return /^\s*\(\s*[^)\s]/.test(item.labelDetail ?? item.detail ?? "");
|
||||
}
|
||||
|
||||
/** kotlin-lsp inserts the item itself (`jetbrains.kotlin.completion.apply`): nothing goes in here. */
|
||||
function serverInserts(item: CompletionItem): boolean {
|
||||
const cmd = item.command?.command;
|
||||
@@ -564,7 +578,10 @@ function runCommand(view: EditorView, item: CompletionItem, others: readonly num
|
||||
if (!command?.command) return;
|
||||
const cmd = command.command;
|
||||
const serverInserted = serverInserts(item);
|
||||
if (serverInserted) view.dispatch({ effects: awaitServerInsert.of({ at: view.state.selection.main.head, others }) });
|
||||
if (serverInserted)
|
||||
view.dispatch({
|
||||
effects: awaitServerInsert.of({ at: view.state.selection.main.head, others, inside: takesArguments(item) }),
|
||||
});
|
||||
if (cmd === "editor.action.triggerParameterHints") setTimeout(() => triggerSignatureHelp(view), 0);
|
||||
else if (cmd === "editor.action.triggerSuggest") setTimeout(() => startCompletion(view), 0);
|
||||
else if (!CLIENT_COMMANDS.has(cmd) && !cmd.startsWith("editor.")) {
|
||||
|
||||
Reference in new issue
Заблокувати користувача