дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-18 07:35:39 +02:00
Порівняти коміти
1
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
cd0ec7d107 |
No files matched your search
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { EditorState, StateEffect, type Extension } from "@codemirror/state";
|
||||
import { EditorView, runScopeHandlers } from "@codemirror/view";
|
||||
import type { CompletionItem } from "@/bindings";
|
||||
import type { EditorDocContext } from "@/editor/api";
|
||||
import { remoteChange, type EditorDocContext } from "@/editor/api";
|
||||
import { lspDoc } from "./lsp";
|
||||
import { insertTab, tabOut, tabOutOfScope } from "@/editor/core/cm/smartKeys";
|
||||
import {
|
||||
@@ -294,6 +294,32 @@ describe("applying completions", () => {
|
||||
await vi.waitFor(() => expect(view.state.selection.main.head).toBe(17));
|
||||
});
|
||||
|
||||
it("leaves an item its server inserts to the server, and puts the caret after what it inserted", async () => {
|
||||
const executed: string[] = [];
|
||||
setInvokeRoute((cmd) => {
|
||||
executed.push(cmd);
|
||||
return Promise.resolve(null);
|
||||
});
|
||||
try {
|
||||
const { view, req } = setup("p.ref|\n", "", [completionExtension()]);
|
||||
view.dispatch({ effects: StateEffect.appendConfig.of(lspDoc.of({ doc: 1 } as EditorDocContext)) });
|
||||
// kotlin-lsp: an empty text, inserted by `jetbrains.kotlin.completion.apply` as a workspace edit.
|
||||
const command = { title: "Apply Completion", command: "jetbrains.kotlin.completion.apply", arguments: ["1"] };
|
||||
const kotlin = item({ label: "refreshName", labelDetail: "()", insertText: "", command });
|
||||
applyLspCompletion(view, makeCompletion(kotlin, req), 5, 5, "insert");
|
||||
expect(view.state.doc.toString()).toBe("p.ref\n");
|
||||
await vi.waitFor(() => expect(executed).toContain("lsp_execute_command"));
|
||||
view.dispatch({ changes: { from: 5, insert: "reshName()" }, annotations: remoteChange.of(true) });
|
||||
expect(view.state.doc.toString()).toBe("p.refreshName()\n");
|
||||
expect(view.state.selection.main.head).toBe(15);
|
||||
// Later edits from elsewhere leave the caret alone.
|
||||
view.dispatch({ changes: { from: 15, insert: " " }, annotations: remoteChange.of(true) });
|
||||
expect(view.state.selection.main.head).toBe(15);
|
||||
} finally {
|
||||
setInvokeRoute(null);
|
||||
}
|
||||
});
|
||||
|
||||
it("does not register a scope when jumping outside closing brackets is off", () => {
|
||||
const { view, req } = setup("service.cr|");
|
||||
applyLspCompletion(view, makeCompletion(item(), req), 8, 10, "insert");
|
||||
@@ -532,6 +558,7 @@ describe("insert parentheses", () => {
|
||||
expect(callParens(item({ kind: "field" }), line("a.cr"), 2, 4)).toBeNull();
|
||||
expect(callParens(item({ isSnippet: true }), line("a.cr"), 2, 4)).toBeNull();
|
||||
expect(callParens(item({ insertText: "create()" }), line("a.cr"), 2, 4)).toBeNull();
|
||||
expect(callParens(item({ insertText: "" }), line("a.cr"), 4, 4)).toBeNull();
|
||||
});
|
||||
|
||||
it("skips calls that already have parentheses and imports", () => {
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
type CompletionResult,
|
||||
type CompletionSource,
|
||||
} from "@codemirror/autocomplete";
|
||||
import { EditorSelection, Prec, type Text } from "@codemirror/state";
|
||||
import { EditorSelection, EditorState, Prec, StateEffect, StateField, type Text } from "@codemirror/state";
|
||||
import { EditorView, ViewPlugin, keymap, type ViewUpdate } from "@codemirror/view";
|
||||
import {
|
||||
call,
|
||||
@@ -58,6 +58,7 @@ import { tx } from "@/platform/i18n";
|
||||
import { showHint } from "./hint";
|
||||
import { inplaceRenameActive } from "./inplaceRename";
|
||||
import { languageSemantics } from "./semantic";
|
||||
import { remoteChange } from "@/editor/api";
|
||||
|
||||
interface RequestContext {
|
||||
doc: number;
|
||||
@@ -297,7 +298,9 @@ export function callParens(
|
||||
start: number,
|
||||
end: number,
|
||||
): "inside" | "after" | null {
|
||||
if (item.isSnippet || !item.kind || !CALLABLE.has(item.kind) || /[(<[{]/.test(item.insertText)) return null;
|
||||
// An empty text is inserted by the item's server command (kotlin-lsp), parentheses included.
|
||||
if (item.isSnippet || !item.kind || !CALLABLE.has(item.kind) || !item.insertText || /[(<[{]/.test(item.insertText))
|
||||
return null;
|
||||
const next = line.text.charAt(end - line.from);
|
||||
if (next && /[(\p{L}\p{N}_$'"`]/u.test(next)) return null;
|
||||
if (IMPORT_LINE.test(line.text.slice(0, start - line.from))) return null;
|
||||
@@ -322,6 +325,33 @@ async function applyLateEdits(view: EditorView, c: LspCompletion, insertedAt: nu
|
||||
view.dispatch({ changes: edits, userEvent: "input.complete" });
|
||||
}
|
||||
|
||||
/** Where the caret was when an item its server inserts was picked, until that edit lands (`null`: none). */
|
||||
const awaitServerInsert = StateEffect.define<number | null>();
|
||||
const serverInsertAt = StateField.define<number | null>({
|
||||
create: () => null,
|
||||
update(at, tr) {
|
||||
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 (at === null || tr.annotation(remoteChange) || tr.selection) return null;
|
||||
return tr.changes.mapPos(at, -1);
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* The server's edit inserts the item at the caret: the caret goes after it, as after any other
|
||||
* completion (a remote insertion at the caret would otherwise leave it in front).
|
||||
*/
|
||||
const caretAfterServerInsert = EditorState.transactionFilter.of((tr) => {
|
||||
const at = tr.startState.field(serverInsertAt, false);
|
||||
const caret = tr.startState.selection.main;
|
||||
if (at == null || !tr.docChanged || !tr.annotation(remoteChange) || !caret.empty || caret.head !== at) return tr;
|
||||
let inserted = false;
|
||||
tr.changes.iterChangedRanges((fromA, toA) => {
|
||||
if (fromA <= at && toA >= at) inserted = true;
|
||||
});
|
||||
return inserted ? [tr, { selection: EditorSelection.cursor(tr.changes.mapPos(at, 1)), sequential: true }] : 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"]);
|
||||
|
||||
@@ -329,18 +359,30 @@ function runCommand(view: EditorView, item: CompletionItem) {
|
||||
const command = item.command;
|
||||
if (!command?.command) return;
|
||||
const cmd = command.command;
|
||||
// kotlin-lsp inserts the item itself (`jetbrains.kotlin.completion.apply`): nothing went in here.
|
||||
const serverInserted = !item.insertText && !CLIENT_COMMANDS.has(cmd) && !cmd.startsWith("editor.");
|
||||
if (serverInserted) view.dispatch({ effects: awaitServerInsert.of(view.state.selection.main.head) });
|
||||
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.")) {
|
||||
const doc = docOf(view);
|
||||
// Server commands (auto-import, postfix…); resulting edits come back as workspace/applyEdit.
|
||||
if (doc)
|
||||
void lspRequest(() => commands.lspExecuteCommand(doc.doc, command)).catch((e: unknown) =>
|
||||
reportLspError(view, e, false),
|
||||
);
|
||||
void lspRequest(() => commands.lspExecuteCommand(doc.doc, command))
|
||||
.catch((e: unknown) => reportLspError(view, e, false))
|
||||
.finally(() => {
|
||||
// The edit arrives before the command's answer; allow for the document update.
|
||||
if (serverInserted)
|
||||
setTimeout(() => {
|
||||
if (view.dom.isConnected && view.state.field(serverInsertAt, false) != null)
|
||||
view.dispatch({ effects: awaitServerInsert.of(null) });
|
||||
}, SERVER_INSERT_WAIT_MS);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const SERVER_INSERT_WAIT_MS = 1000;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Source
|
||||
|
||||
@@ -828,6 +870,8 @@ export function completionExtension() {
|
||||
}),
|
||||
completionFooter,
|
||||
lookupTracker,
|
||||
serverInsertAt,
|
||||
caretAfterServerInsert,
|
||||
Prec.highest(
|
||||
keymap.of([
|
||||
// macOS: ⌃N / ⌃P are line down / up in text, so they move through the lookup as ↓ / ↑ do.
|
||||
|
||||
Reference in new issue
Заблокувати користувача