дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
Gutter and hover action providers ran with an empty invocation, so they could not tell which project the editor belongs to, and markers kept running actions computed for text that had since changed. The host now passes the editor window's project into gutter-actions and hover-actions calls, and the new API 1.18 function workspace.invoking-project returns the project of the window a call serves (the invoking window for commands and events, the editor's window for gutter and hover actions). project-of keeps answering the innermost open project. Actions record the document they were computed for; after an edit, gutter markers are dimmed and gutter and hover actions ignore clicks until the plugin answers for the new text. Fixes #4762
182 рядки
8.2 KiB
TypeScript
182 рядки
8.2 KiB
TypeScript
import { afterEach, expect, it, vi } from "vitest";
|
|
import { EditorState } from "@codemirror/state";
|
|
import { EditorView } from "@codemirror/view";
|
|
import { syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language";
|
|
import { iconAreaExtension } from "@/editor/core/cm/iconArea";
|
|
import { invalidateNavigationProviders } from "@/editor/api";
|
|
const ipc = vi.hoisted(() => ({
|
|
pluginLanguageVisuals: vi.fn(),
|
|
lspExecuteCommand: vi.fn(async () => ({ status: "ok", data: null })),
|
|
}));
|
|
vi.mock("@/platform/ipc", async (original) => ({
|
|
...(await original<typeof import("@/platform/ipc")>()),
|
|
commands: ipc,
|
|
}));
|
|
const { injectedHighlights, pluginLanguageVisuals } = await import("./languageVisuals");
|
|
const ctx = { doc: 7, path: "/p/a.txt", language: "text", uiMode: null, readOnly: false };
|
|
const views: EditorView[] = [];
|
|
afterEach(() => {
|
|
views.splice(0).forEach((v) => v.destroy());
|
|
vi.useRealTimers();
|
|
ipc.pluginLanguageVisuals.mockReset();
|
|
vi.clearAllMocks();
|
|
});
|
|
it("highlights only the embedded slice with its own parser and the editor theme", async () => {
|
|
const state = EditorState.create({
|
|
doc: "host SELECT a FROM t host",
|
|
extensions: [syntaxHighlighting(defaultHighlightStyle)],
|
|
});
|
|
const deco = await injectedHighlights(state, [
|
|
{ range: { start: { line: 0, col: 5 }, end: { line: 0, col: 20 } }, language: "sql" },
|
|
]);
|
|
expect(deco.size).toBeGreaterThan(0);
|
|
const cursor = deco.iter();
|
|
while (cursor.value) {
|
|
expect(cursor.from).toBeGreaterThanOrEqual(5);
|
|
expect(cursor.to).toBeLessThanOrEqual(20);
|
|
expect(cursor.value.spec.class).toMatch(/\S/);
|
|
cursor.next();
|
|
}
|
|
});
|
|
it("renders owned gutter commands and drops answers to superseded buffers", async () => {
|
|
vi.useFakeTimers();
|
|
let answer!: (v: unknown) => void;
|
|
ipc.pluginLanguageVisuals.mockImplementationOnce(
|
|
() =>
|
|
new Promise((r) => {
|
|
answer = r;
|
|
}),
|
|
);
|
|
const visual = {
|
|
version: 2,
|
|
injections: [],
|
|
colors: [],
|
|
gutterActions: [["test", { line: 0, icon: "run", tooltip: "Run test", command: "run", argument: "value" }]],
|
|
};
|
|
ipc.pluginLanguageVisuals.mockResolvedValue({ status: "ok", data: visual });
|
|
const view = new EditorView({
|
|
state: EditorState.create({ doc: "before", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }),
|
|
parent: document.body,
|
|
});
|
|
views.push(view);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
view.dispatch({ changes: { from: 0, to: 6, insert: "after" } });
|
|
answer({ status: "ok", data: visual });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBeNull();
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
const button = view.dom.querySelector<HTMLButtonElement>(".cm-plugin-gutter-action")!;
|
|
expect(button.getAttribute("aria-label")).toBe("Run test");
|
|
button.click();
|
|
expect(ipc.lspExecuteCommand).toHaveBeenCalledWith(
|
|
7,
|
|
expect.objectContaining({ command: "openidea.plugin-language:test:run", arguments: ['"value"'] }),
|
|
);
|
|
});
|
|
|
|
it("keeps Run buttons mounted while edits and delayed refreshes move their lines", async () => {
|
|
vi.useFakeTimers();
|
|
const action = { line: 0, icon: "run", tooltip: "Run sum", command: "run", argument: "sum" };
|
|
const visual = { version: 1, injections: [], colors: [], gutterActions: [["riscv", action]] };
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual });
|
|
const view = new EditorView({
|
|
state: EditorState.create({ doc: "sum: ret", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }),
|
|
parent: document.body,
|
|
});
|
|
views.push(view);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
const button = view.dom.querySelector<HTMLButtonElement>(".cm-plugin-gutter-action")!;
|
|
expect(button).not.toBeNull();
|
|
const area = view.dom.querySelector(".cm-openidea-iconArea");
|
|
let answer!: (value: unknown) => void;
|
|
ipc.pluginLanguageVisuals.mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)));
|
|
view.dispatch({ changes: { from: 8, insert: " " } });
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
answer({ status: "ok", data: visual });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
expect(view.dom.querySelector(".cm-openidea-iconArea")).toBe(area);
|
|
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual });
|
|
invalidateNavigationProviders();
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({
|
|
status: "ok",
|
|
data: { ...visual, gutterActions: [["riscv", { ...action, line: 1 }]] },
|
|
});
|
|
view.dispatch({ changes: { from: 0, insert: "\n" } });
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
button.click();
|
|
expect(ipc.lspExecuteCommand).toHaveBeenLastCalledWith(
|
|
7,
|
|
expect.objectContaining({ command: "openidea.plugin-language:riscv:run", arguments: ['"sum"'] }),
|
|
);
|
|
});
|
|
|
|
it("retains buttons on refresh failures and removes them when the provider confirms removal", async () => {
|
|
vi.useFakeTimers();
|
|
const visual = {
|
|
version: 1,
|
|
injections: [],
|
|
colors: [],
|
|
gutterActions: [["test", { line: 0, icon: "run", tooltip: "Run test", command: "run", argument: null }]],
|
|
};
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual });
|
|
const view = new EditorView({
|
|
state: EditorState.create({ doc: "main: ret", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }),
|
|
parent: document.body,
|
|
});
|
|
views.push(view);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
const button = view.dom.querySelector(".cm-plugin-gutter-action");
|
|
expect(button).not.toBeNull();
|
|
ipc.pluginLanguageVisuals.mockRejectedValueOnce(new Error("refresh interrupted"));
|
|
view.dispatch({ changes: { from: 9, insert: " " } });
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: { ...visual, gutterActions: [] } });
|
|
view.dispatch({ changes: { from: 0, to: view.state.doc.length } });
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBeNull();
|
|
});
|
|
|
|
it("leaves actions inert from an edit until the plugin answers for the new text", async () => {
|
|
vi.useFakeTimers();
|
|
const action = { line: 0, icon: "run", tooltip: "Run build", command: "run", argument: "build" };
|
|
const visual = { version: 1, injections: [], colors: [], gutterActions: [["task", action]] };
|
|
ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual });
|
|
const view = new EditorView({
|
|
state: EditorState.create({ doc: "build:", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }),
|
|
parent: document.body,
|
|
});
|
|
views.push(view);
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
const button = view.dom.querySelector<HTMLButtonElement>(".cm-plugin-gutter-action")!;
|
|
expect(view.dom.classList.contains("cm-plugin-actions-stale")).toBe(false);
|
|
|
|
let answer!: (value: unknown) => void;
|
|
ipc.pluginLanguageVisuals.mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)));
|
|
view.dispatch({ changes: { from: 0, to: 5, insert: "test" } });
|
|
expect(view.dom.classList.contains("cm-plugin-actions-stale")).toBe(true);
|
|
button.click();
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
button.click();
|
|
expect(ipc.lspExecuteCommand).not.toHaveBeenCalled();
|
|
|
|
answer({ status: "ok", data: { ...visual, gutterActions: [["task", { ...action, argument: "test" }]] } });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(view.dom.classList.contains("cm-plugin-actions-stale")).toBe(false);
|
|
view.dom.querySelector<HTMLButtonElement>(".cm-plugin-gutter-action")!.click();
|
|
expect(ipc.lspExecuteCommand).toHaveBeenCalledWith(7, expect.objectContaining({ arguments: ['"test"'] }));
|
|
});
|