diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts index c2d5fd3b92..51f6ddd2a0 100644 --- a/src/editor/lsp/completion.test.ts +++ b/src/editor/lsp/completion.test.ts @@ -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`. diff --git a/src/editor/lsp/completion.ts b/src/editor/lsp/completion.ts index edcfb56bab..71e4bd2593 100644 --- a/src/editor/lsp/completion.ts +++ b/src/editor/lsp/completion.ts @@ -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(); const serverInsertAt = StateField.define({ @@ -470,7 +472,11 @@ const serverInsertAt = StateField.define({ 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): 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.")) {