Файли
openidea/src/editor/lsp/syntaxFallback.test.ts
T
l0ng-ai 401d66d850 fix(navigation): keep the keyboard in choosers and filter implementations by class (#3708)
A speed search that matched nothing replaced the focused list with a non-focusable empty state, so focus fell to the page and the chooser stopped taking keys. List now renders its empty state inside the focusable list (all list popups and choosers). Chooser rows whose targets share one name (a method's implementations, same-named declaration candidates) show the containing class as their text, so typing a class name filters them.
2026-11-16 16:03:30 +08:00

1110 рядки
50 KiB
TypeScript

import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { EditorState, StateEffect, Text } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import type { IdeError, Locals, NodeAt, Range, SymbolMatch, SyntaxNode } from "@/bindings";
type R<T> = Promise<{ status: "ok"; data: T } | { status: "error"; error: IdeError }>;
const ok = <T>(data: T) => Promise.resolve({ status: "ok" as const, data });
const unavailable = () =>
Promise.resolve({ status: "error" as const, error: { kind: "unavailable", message: "no server" } as IdeError });
const cmd = {
lspHover: vi.fn((): R<unknown> => ok(null)),
lspDocumentLinks: vi.fn(() => ok([])),
cancelRequest: vi.fn(() => ok(null)),
lspGoto: vi.fn((): R<unknown> => unavailable()),
lspDocumentHighlights: vi.fn((): R<unknown> => unavailable()),
lspPrepareRename: vi.fn((): R<unknown> => unavailable()),
lspRename: vi.fn((): R<unknown> => unavailable()),
syntaxSupport: vi.fn(() => ok({ tree: true, locals: true })),
syntaxLocals: vi.fn((): R<Locals> => ok({ status: "noIdentifier" })),
syntaxNodeAt: vi.fn((): R<NodeAt | null> => ok(null)),
syntaxIdentifiers: vi.fn((): R<unknown> => ok([])),
syntaxErrors: vi.fn((): R<unknown> => ok({ errors: [], truncated: false })),
findSymbols: vi.fn((): R<SymbolMatch[]> => ok([])),
navIndexDefinitions: vi.fn((): R<SymbolMatch[]> => ok([])),
navResolveModule: vi.fn((): R<string | null> => ok(null)),
applyWorkspaceEdit: vi.fn((): R<unknown> => ok(null)),
};
vi.mock("@/platform/ipc", async (orig) => {
const actual = await orig<typeof import("@/platform/ipc")>();
return { ...actual, isTauri: () => true, commands: { ...actual.commands, ...cmd } };
});
const executeAction = vi.hoisted(() => vi.fn());
vi.mock("@/platform/actions", async (orig) => {
const actual = await orig<typeof import("@/platform/actions")>();
executeAction.mockImplementation(actual.executeAction);
return { ...actual, executeAction };
});
const openFile = vi.fn(async () => {});
const { provideEditorService, registerDocumentIndexSync } = await import("@/workbench/contributions");
provideEditorService({ openFile } as never);
const fb = await import("./syntaxFallback");
const { gotoTargets, navigationExtension, highlightUsages } = await import("./navigation");
const { registerNavigationProvider } = await import("@/editor/navigationProviders");
const { startRename, renameNamePart } = await import("./rename");
const { lspDoc } = await import("./lsp");
const { invalidateConfigurationNavigation } = await import("./configurationNavigation");
const { useLspPopup, closePopup } = await import("./popupStore");
const { inplaceRenameExtension, inplaceSession } = await import("./inplaceRename");
const r = (line: number, col: number, endCol: number): Range => ({ start: { line, col }, end: { line, col: endCol } });
const node = (kind: string, range: Range, field: string | null = null): SyntaxNode => ({
kind,
range,
field,
named: true,
error: false,
missing: false,
hasError: false,
});
const sym = (name: string, kind: SymbolMatch["kind"], path: string, container: string | null = null): SymbolMatch => ({
name,
kind,
container,
path,
relPath: path.slice(1),
range: r(3, 4, 4 + name.length),
score: 1,
indices: [],
});
const local = (over: Partial<Extract<Locals, { status: "local" }>["symbol"]> = {}): Locals => ({
status: "local",
symbol: {
name: "x",
kind: "var",
definition: r(0, 6, 7),
scope: { start: { line: 0, col: 0 }, end: { line: 9, col: 0 } },
references: [r(1, 2, 3)],
certain: true,
conflicts: [],
...over,
},
});
const views: EditorView[] = [];
function editor(doc: string, language = "typescript", anchor = 0, path = "/p/a.ts") {
const view = new EditorView({
state: EditorState.create({
doc,
selection: { anchor },
extensions: [lspDoc.of({ doc: 7, path, language, uiMode: null, readOnly: false }), navigationExtension()],
}),
parent: document.body,
});
views.push(view);
return view;
}
beforeEach(() => {
invalidateConfigurationNavigation();
for (const f of Object.values(cmd)) f.mockClear();
openFile.mockClear();
closePopup();
});
afterEach(() => views.splice(0).forEach((v) => v.destroy()));
describe("usageRole / filterSymbols", () => {
it("reads the role from the tree", () => {
const id = r(0, 0, 3);
expect(fb.usageRole({ node: node("type_identifier", id), ancestors: [] })).toBe("type");
expect(fb.usageRole({ node: node("identifier", id, "function"), ancestors: [node("call_expression", id)] })).toBe(
"call",
);
expect(fb.usageRole({ node: node("identifier", id, "name"), ancestors: [node("method_invocation", id)] })).toBe(
"call",
);
expect(fb.usageRole({ node: node("identifier", id, "constructor"), ancestors: [node("new_expression", id)] })).toBe(
"new",
);
expect(fb.usageRole({ node: node("identifier", id), ancestors: [node("binary_expression", id)] })).toBe("other");
expect(fb.usageRole(null)).toBe("other");
});
it("keeps exact names whose kind fits the role", () => {
const all = [
sym("Foo", "class", "/a"),
sym("Foo", "function", "/b"),
sym("Foo", "field", "/c"),
sym("Foobar", "class", "/d"),
sym("Foo", "file", "/e"),
];
expect(fb.filterSymbols(all, "Foo", "type").map((s) => s.path)).toEqual(["/a"]);
expect(fb.filterSymbols(all, "Foo", "call").map((s) => s.path)).toEqual(["/a", "/b"]);
expect(fb.filterSymbols(all, "Foo", "other").map((s) => s.path)).toEqual(["/a", "/b", "/c"]);
});
it("only keeps declarations from the same language family", () => {
expect(fb.compatibleFiles("/a/Main.kt", "/b/Util.java")).toBe(true);
expect(fb.compatibleFiles("/a/app.tsx", "/b/util.js")).toBe(true);
expect(fb.compatibleFiles("/a/pets.rb", "/b/util.lua")).toBe(false);
expect(fb.compatibleFiles("/a/x.lua", "/b/y.lua")).toBe(true);
expect(fb.compatibleFiles("/a/Makefile", "/b/y.lua")).toBe(true);
});
it("ranks the current file and same-extension files first", () => {
const t = (path: string) => ({ path, range: r(0, 0, 1) });
expect(fb.rankTargets([t("/z.kt"), t("/b.ts"), t("/a.py"), t("/me.ts")], "/me.ts").map((x) => x.path)).toEqual([
"/me.ts",
"/b.ts",
"/a.py",
"/z.kt",
]);
});
});
describe("declarationFallback", () => {
const doc = { doc: 7, path: "/p/a.ts", language: "typescript" };
const text = Text.of(["const x = 1;", " x + foo();"]);
it("jumps to a certain local definition without asking the index", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local()));
expect(await fb.declarationFallback(doc, text, { line: 1, col: 2 })).toEqual({
kind: "targets",
targets: [{ path: "/p/a.ts", range: r(0, 6, 7), name: "x" }],
ambiguous: false,
});
expect(cmd.findSymbols).not.toHaveBeenCalled();
});
it("offers every same-scope declaration when the local is ambiguous", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ certain: false, conflicts: [r(4, 4, 5)] })));
const res = await fb.declarationFallback(doc, text, { line: 1, col: 2 });
expect(res).toMatchObject({ kind: "targets", ambiguous: true });
expect(res.kind === "targets" && res.targets.map((t) => t.range.start.line)).toEqual([0, 4]);
});
it("looks non-locals up in the index by exact name and role", async () => {
cmd.syntaxLocals.mockReturnValueOnce(
ok({ status: "notLocal", name: "foo", range: r(1, 6, 9), reason: "unresolved" }),
);
cmd.syntaxNodeAt.mockReturnValueOnce(
ok({ node: node("identifier", r(1, 6, 9), "function"), ancestors: [node("call_expression", r(1, 6, 11))] }),
);
cmd.findSymbols.mockReturnValueOnce(
ok([
sym("foo", "function", "/p/b.ts", "util"),
sym("foo", "field", "/p/c.ts"),
sym("foobar", "function", "/p/d.ts"),
]),
);
const res = await fb.declarationFallback(doc, text, { line: 1, col: 7 });
expect(cmd.findSymbols).toHaveBeenCalledWith("foo", "all", 500);
expect(res).toEqual({
kind: "targets",
targets: [{ path: "/p/b.ts", range: r(3, 4, 7), name: "foo", kind: "function", container: "util" }],
ambiguous: false,
});
});
it("does not filter members by role", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "foo", range: r(1, 6, 9), reason: "member" }));
cmd.syntaxNodeAt.mockReturnValueOnce(ok({ node: node("property_identifier", r(1, 6, 9)), ancestors: [] }));
cmd.findSymbols.mockReturnValueOnce(
ok([sym("foo", "method", "/p/b.ts", "A"), sym("foo", "field", "/p/c.ts", "B")]),
);
const res = await fb.declarationFallback(doc, text, { line: 1, col: 7 });
expect(res).toMatchObject({ kind: "targets", ambiguous: true });
});
it("uses the node text for languages without locals, and gives up without a grammar", async () => {
cmd.syntaxSupport.mockReturnValueOnce(ok({ tree: true, locals: false }));
cmd.syntaxNodeAt.mockReturnValueOnce(ok({ node: node("identifier", r(1, 6, 9)), ancestors: [] }));
await fb.declarationFallback({ ...doc, language: "nolocals" }, text, { line: 1, col: 7 });
expect(cmd.findSymbols).toHaveBeenCalledWith("foo", "all", 500);
cmd.syntaxSupport.mockReturnValueOnce(ok({ tree: false, locals: false }));
expect(await fb.declarationFallback({ ...doc, language: "nogrammar" }, text, { line: 0, col: 0 })).toEqual({
kind: "unsupported",
});
});
it("answers a Vue component from the <script> region's shifted coordinates", async () => {
cmd.syntaxSupport.mockReturnValueOnce(ok({ tree: true, locals: true }));
// The backend answers a script region's local in document coordinates.
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ name: "size", definition: r(9, 6, 10) })));
const res = await fb.declarationFallback({ ...doc, path: "/p/App.vue", language: "vue" }, text, {
line: 9,
col: 6,
});
expect(res).toEqual({
kind: "targets",
targets: [{ path: "/p/App.vue", range: r(9, 6, 10), name: "size" }],
ambiguous: false,
});
});
});
describe("rename helpers", () => {
it("expands shorthands and refuses stale text", () => {
const text = Text.of(["const x = 1;", "f({ x }, x);"]);
const plan = {
symbol: (local() as Extract<Locals, { status: "local" }>).symbol,
occurrences: [
{ range: r(0, 6, 7), shorthand: false },
{ range: r(1, 4, 5), shorthand: true },
{ range: r(1, 9, 10), shorthand: false },
],
};
const changes = fb.renameChanges(text, plan, "y");
expect(EditorState.create({ doc: text }).update({ changes: changes! }).state.doc.toString()).toBe(
"const y = 1;\nf({ x: y }, y);",
);
expect(fb.renameChanges(Text.of(["const z = 1;", "f({ x }, x);"]), plan, "y")).toBeNull();
});
it("detects JS and Rust shorthand nodes", () => {
const id = r(0, 0, 1);
expect(fb.isShorthand({ node: node("shorthand_property_identifier", id), ancestors: [] })).toBe(true);
expect(fb.isShorthand({ node: node("shorthand_property_identifier_pattern", id), ancestors: [] })).toBe(true);
expect(fb.isShorthand({ node: node("identifier", id), ancestors: [node("shorthand_field_initializer", id)] })).toBe(
true,
);
expect(fb.isShorthand({ node: node("identifier", id), ancestors: [node("arguments", id)] })).toBe(false);
});
it("refuses non-locals and plans locals with shorthand detection", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "foo", range: r(0, 0, 3), reason: "member" }));
expect(await fb.planLocalRename({ doc: 7, language: "typescript" }, { line: 0, col: 1 })).toMatchObject({
kind: "refused",
});
cmd.syntaxLocals.mockReturnValueOnce(ok(local()));
cmd.syntaxNodeAt
.mockReturnValueOnce(ok({ node: node("identifier", r(0, 6, 7)), ancestors: [] }))
.mockReturnValueOnce(ok({ node: node("shorthand_property_identifier", r(1, 2, 3)), ancestors: [] }));
const res = await fb.planLocalRename({ doc: 7, language: "typescript" }, { line: 0, col: 6 });
expect(res.kind === "plan" && res.plan.occurrences.map((o) => o.shorthand)).toEqual([false, true]);
// No shorthand syntax in Python: no node queries.
cmd.syntaxNodeAt.mockClear();
cmd.syntaxLocals.mockReturnValueOnce(ok(local()));
await fb.planLocalRename({ doc: 7, language: "python" }, { line: 0, col: 6 });
expect(cmd.syntaxNodeAt).not.toHaveBeenCalled();
});
it("plans a Vue component's local rename with shorthand detection", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local()));
const short = ok({ node: node("shorthand_property_identifier", r(1, 2, 3)), ancestors: [] });
cmd.syntaxNodeAt.mockReturnValueOnce(short);
cmd.syntaxNodeAt.mockReturnValueOnce(short);
const plan = await fb.planLocalRename({ doc: 7, language: "vue" }, { line: 1, col: 2 });
expect(plan.kind === "plan" && plan.plan.occurrences.map((o) => o.shorthand)).toEqual([true, true]);
});
it("flags ambiguity and name clashes inside the scope", async () => {
const plan = {
symbol: (local({ certain: false, conflicts: [r(3, 0, 1)] }) as Extract<Locals, { status: "local" }>).symbol,
occurrences: [],
};
cmd.syntaxIdentifiers.mockReturnValueOnce(
ok([
{ range: r(2, 0, 1), kind: "identifier", qualified: false },
{ range: r(2, 4, 5), kind: "property_identifier", qualified: true },
{ range: r(20, 0, 1), kind: "identifier", qualified: false },
]),
);
const risks = await fb.renameRisks(7, plan, "y");
expect(risks).toHaveLength(2);
expect(risks[0]).toContain("declared 2 times");
expect(risks[1]).toContain("1 occurrence");
});
it("only highlights certain locals", () => {
expect(fb.localHighlights(local())).toEqual([
{ range: r(0, 6, 7), kind: "write" },
{ range: r(1, 2, 3), kind: "text" },
]);
expect(fb.localHighlights(local({ certain: false }))).toBeNull();
expect(fb.localHighlights({ status: "notLocal", name: "x", range: r(0, 0, 1), reason: "topLevel" })).toBeNull();
});
});
describe("navigation wiring", () => {
it.each(["vue", "html", "svelte", "jsp"])("keeps %s server answers ahead of syntax", async (language) => {
const target = { path: `/p/a.${language}`, range: r(0, 6, 7), uri: null };
const view = editor("const x = 1;\nx;", language, 13, target.path);
cmd.lspGoto.mockReturnValueOnce(ok([target]));
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith(target.path, { selection: target.range.start, focus: true });
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(cmd.syntaxNodeAt).not.toHaveBeenCalled();
invalidateConfigurationNavigation();
cmd.lspGoto.mockReturnValueOnce(ok([]));
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledOnce();
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
});
it("respects an empty server answer", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([]));
const view = editor("const x = 1;\nx;", "typescript", 13);
await gotoTargets(view, "definition");
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe("Cannot find declaration to go to");
});
it("answers from the index when the server cannot resolve an import into a dependency project", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([]));
cmd.navIndexDefinitions.mockReturnValueOnce(ok([sym("ParallelDims", "class", "/deps/engine/model.py")]));
const view = editor("from torchtitan.distributed import ParallelDims;\n", "python", 35, "/p/a.py");
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/deps/engine/model.py", {
selection: { line: 3, col: 4 },
focus: true,
});
});
it("opens the file a package segment of an import path names", async () => {
cmd.navResolveModule.mockReturnValueOnce(ok("/deps/torchtitan/__init__.py"));
cmd.navResolveModule.mockReturnValueOnce(ok("/deps/torchtitan/tools/logging.py"));
const view = editor("from torchtitan.tools.logging import logger;\n", "python", 6, "/p/a.py");
await gotoTargets(view, "definition");
expect(cmd.navResolveModule).toHaveBeenCalledWith("torchtitan", "/p/a.py");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/__init__.py", {
selection: { line: 0, col: 0 },
focus: true,
});
openFile.mockClear();
const deeper = editor("from torchtitan.tools.logging import logger;\n", "python", 24, "/p/a.py");
await gotoTargets(deeper, "definition");
expect(cmd.navResolveModule).toHaveBeenLastCalledWith("torchtitan.tools.logging", "/p/a.py");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/tools/logging.py", {
selection: { line: 0, col: 0 },
focus: true,
});
});
it("never looks a module path segment up as a symbol name", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([]));
const view = editor("from torchtitan.tools.logging import logger;\n", "python", 17, "/p/a.py");
await gotoTargets(view, "definition");
expect(cmd.navResolveModule).toHaveBeenCalledWith("torchtitan.tools", "/p/a.py");
expect(cmd.navIndexDefinitions).not.toHaveBeenCalled();
expect(openFile).not.toHaveBeenCalled();
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe("Cannot find declaration to go to");
});
it("still looks the imported name up, not the module path", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([]));
cmd.navIndexDefinitions.mockReturnValueOnce(ok([sym("logger", "variable", "/deps/torchtitan/tools/logging.py")]));
const view = editor("from torchtitan.tools.logging import logger;\n", "python", 39, "/p/a.py");
await gotoTargets(view, "definition");
expect(cmd.navResolveModule).not.toHaveBeenCalled();
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/tools/logging.py", {
selection: { line: 3, col: 4 },
focus: true,
});
});
it("shows usages on a declaration even when a plugin adds another target", async () => {
// A MyBatis mapper method: the server answers the method itself, the plugin its XML statement.
cmd.lspGoto.mockReturnValueOnce(
ok([
{ path: "/p/UserMapper.xml", range: r(3, 16, 24) },
{ path: "/p/UserMapper.java", range: r(3, 11, 19) },
]),
);
const view = editor(
"package demo;\n\npublic interface UserMapper {\n String findName(Long id);\n}\n",
"java",
58,
"/p/UserMapper.java",
);
await gotoTargets(view, "definition");
expect(executeAction).toHaveBeenCalledWith("find.showUsages");
expect(openFile).not.toHaveBeenCalled();
});
it("shows usages on a declaration a plugin's index links elsewhere, and follows the link from a call", async () => {
// MyBatis Duo indexes the mapper method's name as a link to its XML statement.
const xml = { path: "/p/UserMapper.xml", range: r(3, 16, 24) };
const off = registerNavigationProvider(async (view) => {
const at = view.state.doc.toString().indexOf("findName");
return { from: at, to: at + 8, targets: [xml] };
});
try {
// The syntax tree puts the caret on the method's name; the host then answers both.
cmd.syntaxNodeAt.mockReturnValueOnce(
ok({
node: node("identifier", r(3, 11, 19), "name"),
ancestors: [node("method_declaration", { start: { line: 3, col: 4 }, end: { line: 3, col: 29 } })],
}),
);
cmd.lspGoto.mockReturnValueOnce(ok([xml, { path: "/p/UserMapper.java", range: r(3, 11, 19) }]));
const view = editor(
"package demo;\n\npublic interface UserMapper {\n String findName(Long id);\n}\n",
"java",
58,
"/p/UserMapper.java",
);
await gotoTargets(view, "definition");
expect(executeAction).toHaveBeenCalledWith("find.showUsages");
expect(openFile).not.toHaveBeenCalled();
// A call: the plugin's link is followed without asking the language server.
executeAction.mockClear();
cmd.lspGoto.mockClear();
cmd.syntaxNodeAt.mockReturnValueOnce(
ok({ node: node("identifier", r(1, 26, 34), "name"), ancestors: [node("method_invocation", r(1, 24, 38))] }),
);
const call = editor("class S {\n void f(UserMapper m) { m.findName(1L); }\n}\n", "java", 37, "/p/S.java");
await gotoTargets(call, "definition");
expect(openFile).toHaveBeenCalledWith("/p/UserMapper.xml", expect.objectContaining({ focus: true }));
expect(executeAction).not.toHaveBeenCalledWith("find.showUsages");
expect(cmd.lspGoto).not.toHaveBeenCalled();
} finally {
off();
}
});
it("looks past a server answer that points at the import itself", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([{ path: "/p/a.py", range: r(0, 37, 43) }]));
cmd.navIndexDefinitions.mockReturnValueOnce(ok([sym("logger", "variable", "/deps/torchtitan/tools/logging.py")]));
const view = editor("from torchtitan.tools.logging import logger;\n", "python", 39, "/p/a.py");
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/tools/logging.py", {
selection: { line: 3, col: 4 },
focus: true,
});
expect(executeAction).not.toHaveBeenCalledWith("find.showUsages");
});
it("narrows a plain import with a trailing comment the same way", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([{ path: "/p/a.py", range: r(0, 32, 39) }]));
cmd.navResolveModule
.mockImplementationOnce(() => ok(null)) // the segment click did not resolve: the answer came from the server
.mockImplementationOnce(() => ok(null)) // the name is not a submodule of the module
.mockImplementationOnce(() => ok("/deps/torchtitan/models/common/decoder.py"));
cmd.navIndexDefinitions.mockReturnValueOnce(
ok([sym("Decoder", "class", "/deps/torchtitan/models/common/decoder.py")]),
);
const view = editor("import torchtitan.models.common.decoder # Decoder\n", "python", 36, "/p/a.py");
await gotoTargets(view, "definition");
// The plain import's name is the module segment itself: the index has no exact
// "decoder" symbol, so the narrowed answer is the module file.
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/models/common/decoder.py", {
selection: { line: 0, col: 0 },
focus: true,
});
expect(executeAction).not.toHaveBeenCalledWith("find.showUsages");
});
it("narrows a self-pointed import answer to the module the import names", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([{ path: "/p/a.py", range: r(0, 45, 52) }]));
cmd.navResolveModule
.mockImplementationOnce(() => ok(null)) // the name is not a submodule of the module
.mockImplementationOnce(() => ok("/deps/torchtitan/models/common/decoder.py"));
cmd.navIndexDefinitions.mockReturnValueOnce(
ok([
sym("Decoder", "class", "/deps/torchtitan/models/common/decoder.py"),
sym("Decoder", "class", "/deps/torchtitan/models/flux/autoencoder.py"),
]),
);
const view = editor("from torchtitan.models.common.decoder import Decoder\n", "python", 47, "/p/a.py");
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/models/common/decoder.py", {
selection: { line: 3, col: 4 },
focus: true,
});
expect(executeAction).not.toHaveBeenCalledWith("find.showUsages");
});
it("jumps to the file when an imported name is a submodule", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([{ path: "/p/a.py", range: r(0, 30, 37) }]));
cmd.navResolveModule.mockImplementationOnce(() => ok("/deps/torchtitan/models/decoder/__init__.py"));
const view = editor("from torchtitan.models import decoder\n", "python", 35, "/p/a.py");
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/models/decoder/__init__.py", {
selection: { line: 0, col: 0 },
focus: true,
});
expect(cmd.navIndexDefinitions).not.toHaveBeenCalled();
});
it("narrows a listed name of a parenthesised import to its module", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([{ path: "/p/a.py", range: r(1, 2, 9) }]));
cmd.navResolveModule
.mockImplementationOnce(() => ok(null))
.mockImplementationOnce(() => ok("/deps/torchtitan/models/common/decoder.py"));
cmd.navIndexDefinitions.mockReturnValueOnce(
ok([
sym("Decoder", "class", "/deps/torchtitan/models/common/decoder.py"),
sym("Decoder", "class", "/deps/torchtitan/models/flux/autoencoder.py"),
]),
);
const view = editor("from torchtitan.models.common.decoder import (\n Decoder,\n)\n", "python", 52, "/p/a.py");
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/deps/torchtitan/models/common/decoder.py", {
selection: { line: 3, col: 4 },
focus: true,
});
});
it("says why when the server only checks the file's syntax", async () => {
const { setFileDiagnostics } = await import("./diagnostics");
setFileDiagnostics("/p/a.ts", "jdtls", [
{
range: { start: { line: 0, col: 0 }, end: { line: 0, col: 1 } },
severity: "warning",
message: "a.ts is not on the classpath of project app, only syntax errors are reported",
source: "Java",
code: "32",
},
]);
try {
cmd.lspGoto.mockReturnValueOnce(ok([]));
const view = editor("const x = 1;\nx;", "typescript", 13);
await gotoTargets(view, "definition");
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe(
"Cannot find declaration to go to. This file is outside the source folders of its Java module: only syntax errors are reported, and navigation and completion don't work.",
);
} finally {
setFileDiagnostics("/p/a.ts", "jdtls", []);
}
});
it("falls back to the local definition when the server is not ready", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ references: [r(1, 0, 1)] })));
const view = editor("const x = 1;\nx;", "typescript", 13);
await gotoTargets(view, "definition");
expect(openFile).toHaveBeenCalledWith("/p/a.ts", { selection: { line: 0, col: 6 }, focus: true });
});
it("opens a chooser for several index candidates", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "run", range: r(0, 4, 7), reason: "member" }));
cmd.syntaxNodeAt.mockReturnValueOnce(ok({ node: node("property_identifier", r(0, 4, 7)), ancestors: [] }));
cmd.findSymbols.mockReturnValueOnce(
ok([sym("run", "method", "/p/a.ts", "A"), sym("run", "method", "/p/b.ts", "B")]),
);
const view = editor("obj.run();", "typescript", 5);
await gotoTargets(view, "definition");
const popup = useLspPopup.getState().popup;
expect(popup?.kind).toBe("list");
if (popup?.kind !== "list") return;
expect(popup.title).toBe("Choose Declaration of run (2 found, based on syntax)");
expect(popup.pin?.label).toBe("Open in Find Tool Window");
// Same-named candidates read by their containers.
expect(popup.items.map((i) => [i.label, i.symbol, i.detail])).toEqual([
["A", "method", "p/a.ts:4"],
["B", "method", "p/b.ts:4"],
]);
expect(openFile).not.toHaveBeenCalled();
});
it("keeps the server error when the language has no grammar", async () => {
cmd.syntaxSupport.mockReturnValueOnce(ok({ tree: false, locals: false }));
const view = editor("x", "plain-nogrammar", 0);
await gotoTargets(view, "definition");
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe("No language server available");
});
it("highlights a certain local when the server is not ready", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ references: [r(1, 0, 1)] })));
const view = editor("const x = 1;\nx;", "typescript", 13);
highlightUsages(view);
await vi.waitFor(() => expect(view.dom.querySelectorAll(".cm-lsp-occurrence")).toHaveLength(2));
expect(view.dom.querySelectorAll(".cm-lsp-occurrence-write")).toHaveLength(1);
});
it("does not highlight non-locals from syntax", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "x", range: r(0, 0, 1), reason: "topLevel" }));
const view = editor("x;", "typescript", 0);
highlightUsages(view);
await vi.waitFor(() =>
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe("No language server available"),
);
expect(view.dom.querySelectorAll(".cm-lsp-occurrence")).toHaveLength(0);
});
});
describe("rename wiring", () => {
it("preserves the server's missing capability when syntax cannot rename a method", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(
Promise.resolve({
status: "error",
error: { kind: "unavailable", message: "intelephense doesn't support rename" },
}),
);
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "run", range: r(0, 4, 7), reason: "member" }));
const view = editor("obj.run();", "php", 5);
await startRename(view);
const message = document.querySelector(".cm-lsp-hint")?.textContent;
expect(message).toContain("intelephense doesn't support rename");
expect(message).toContain("only local variables");
expect(message).not.toContain("without the language server");
expect(view.state.doc.toString()).toBe("obj.run();");
expect(useLspPopup.getState().popup).toBeNull();
});
it("renames a certain local in place when the server is not ready", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ references: [r(1, 4, 5)] })));
cmd.syntaxNodeAt
.mockReturnValueOnce(ok({ node: node("identifier", r(0, 6, 7)), ancestors: [] }))
.mockReturnValueOnce(ok({ node: node("shorthand_property_identifier", r(1, 4, 5)), ancestors: [] }));
const view = editor("const x = 1;\nf({ x });", "typescript", 6);
await startRename(view);
const popup = useLspPopup.getState().popup;
expect(popup?.kind).toBe("rename");
if (popup?.kind !== "rename") return;
expect(popup.note).toContain("Based on syntax");
popup.onSubmit("y");
await vi.waitFor(() => expect(view.state.doc.toString()).toBe("const y = 1;\nf({ x: y });"));
expect(cmd.lspRename).not.toHaveBeenCalled();
});
it("asks before renaming an ambiguous local", async () => {
cmd.syntaxLocals.mockReturnValueOnce(
ok(local({ references: [r(1, 0, 1)], certain: false, conflicts: [r(2, 4, 5)] })),
);
const view = editor("const x = 1;\nx;\nvar x;", "typescript", 6);
await startRename(view);
const popup = useLspPopup.getState().popup;
if (popup?.kind !== "rename") throw new Error("no rename popup");
popup.onSubmit("y");
await vi.waitFor(() => expect(useLspPopup.getState().popup?.kind).toBe("preview"));
const preview = useLspPopup.getState().popup;
if (preview?.kind !== "preview") return;
expect(preview.occurrences?.map((o) => o.line)).toEqual([0, 1]);
expect(view.state.doc.toString()).toBe("const x = 1;\nx;\nvar x;");
preview.onConfirm();
expect(view.state.doc.toString()).toBe("const y = 1;\ny;\nvar x;");
});
it("never renames non-locals from syntax", async () => {
cmd.syntaxLocals.mockReturnValueOnce(ok({ status: "notLocal", name: "run", range: r(0, 4, 7), reason: "member" }));
const view = editor("obj.run();", "typescript", 5);
await startRename(view);
expect(useLspPopup.getState().popup).toBeNull();
expect(document.querySelector(".cm-lsp-hint")?.textContent).toContain("only local variables");
});
it.each(["typescript", "java"])("previews a %s multi-file rename and retains version guards", async (language) => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 0, 3)));
const edit = {
files: ["/p/Main.java", "/p/Use.java"].map((path) => ({
kind: "edit",
path,
version: 7,
edits: [{ range: r(0, 0, 3), new_text: "new" }],
})),
preconditions: [{ path: "/p/Main.java", hash: "original" }],
};
cmd.lspRename.mockReturnValueOnce(ok({ edit, incomplete: null }));
const view = editor("old", language, 0);
await renameNamePart(view, { from: 0, to: 3 }, "new");
const preview = useLspPopup.getState().popup;
expect(preview?.kind).toBe("preview");
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
if (preview?.kind !== "preview") throw new Error("missing rename preview");
expect(preview.summary.files).toBe(2);
preview.onConfirm();
await vi.waitFor(() => expect(cmd.applyWorkspaceEdit).toHaveBeenCalledExactlyOnceWith(edit));
});
it.each(["outOfSync", "invalidInput"] as const)(
"never falls back to a partial local rename after %s preparation",
async (kind) => {
const failed = () =>
Promise.resolve({
status: "error" as const,
error: { kind, message: "Indexed anchors cannot be used" } as IdeError,
});
cmd.lspPrepareRename.mockReturnValueOnce(failed()).mockReturnValueOnce(failed());
const view = editor("old", "typescript", 0);
await startRename(view);
await renameNamePart(view, { from: 0, to: 3 }, "new");
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
expect(view.state.doc.toString()).toBe("old");
},
);
it("keeps a native-owned rename refusal out of the syntax fallback even over broken code", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 6, 7)));
cmd.lspRename.mockReturnValueOnce(
Promise.resolve({
status: "error",
error: { kind: "invalidInput", message: "Native server declined rename" } as IdeError,
}),
);
cmd.syntaxErrors.mockReturnValueOnce(ok({ errors: [r(0, 0, 1)], truncated: false }));
cmd.syntaxLocals.mockReturnValueOnce(ok(local()));
const view = editor("const x = 1;\nx;", "typescript", 6);
await renameNamePart(view, { from: 6, to: 7 }, "y");
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
expect(useLspPopup.getState().popup).toBeNull();
expect(view.state.doc.toString()).toBe("const x = 1;\nx;");
});
it("does not fall back to a local-only rename when a contributed index is unavailable", async () => {
const off = registerDocumentIndexSync(async () => {
throw new Error("Index not ready");
});
try {
const view = editor("old", "typescript", 0);
await renameNamePart(view, { from: 0, to: 3 }, "new");
expect(cmd.lspPrepareRename).not.toHaveBeenCalled();
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
expect(view.state.doc.toString()).toBe("old");
} finally {
off();
}
});
it("requires confirmation for a single-file indexed rename and cancellation leaves every anchor unchanged", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 0, 3)));
const edit = {
files: [{ kind: "edit", path: "/p/key.idx", edits: [{ range: r(0, 0, 3), new_text: "new" }] }],
preconditions: [{ path: "/p/key.idx", hash: "before" }],
};
cmd.lspRename.mockReturnValueOnce(ok({ edit, incomplete: null, preview: true }));
const view = editor("old", "text", 0);
await renameNamePart(view, { from: 0, to: 3 }, "new");
expect(useLspPopup.getState().popup?.kind).toBe("preview");
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
closePopup();
expect(view.state.doc.toString()).toBe("old");
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
});
describe("Typo: Rename to (issue #42)", () => {
it("renames the whole name through the server, references included", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 0, 12)));
const edits = [r(0, 0, 12), r(1, 6, 18)].map((range) => ({ range, new_text: "message_count" }));
const edit = { files: [{ kind: "edit", path: "/p/a.ts", edits }] };
cmd.lspRename.mockReturnValueOnce(ok({ edit, incomplete: null }));
const view = editor("mesage_count = 1\nprint(mesage_count)", "python", 0);
await renameNamePart(view, { from: 0, to: 6 }, "message");
expect(cmd.lspRename).toHaveBeenCalledWith(expect.anything(), 7, { line: 0, col: 0 }, "message_count");
expect(cmd.applyWorkspaceEdit).toHaveBeenCalledWith(edit);
// Nothing was replaced locally: the server's edit covers every reference.
expect(view.state.doc.toString()).toBe("mesage_count = 1\nprint(mesage_count)");
});
it("shows an edit that may miss usages before applying it, even in one file", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 0, 12)));
const edit = {
files: [{ kind: "edit", path: "/p/a.ts", edits: [{ range: r(0, 0, 12), new_text: "message_count" }] }],
};
cmd.lspRename.mockReturnValueOnce(ok({ edit, incomplete: "tooManyFiles" }));
const view = editor("mesage_count = 1", "python", 0);
await renameNamePart(view, { from: 0, to: 6 }, "message");
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
const popup = useLspPopup.getState().popup;
expect(popup?.kind === "preview" && popup.warnings).toEqual([
"May be incomplete: too many files mention 'mesage_count' to search them all",
]);
if (popup?.kind === "preview") popup.onConfirm();
await vi.waitFor(() => expect(cmd.applyWorkspaceEdit).toHaveBeenCalledWith(edit));
closePopup();
});
it("says why when an incomplete search found nothing to rename", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 0, 12)));
cmd.lspRename.mockReturnValueOnce(ok({ edit: { files: [] }, incomplete: "indexing" }));
const view = editor("mesage_count = 1", "python", 0);
await renameNamePart(view, { from: 0, to: 6 }, "message");
expect(document.querySelector(".cm-lsp-hint")?.textContent).toContain("still indexing");
});
it("cancels a rename still waiting for the server when a newer one starts", async () => {
let answer: (v: unknown) => void = () => {};
cmd.lspPrepareRename.mockReturnValue(ok(r(0, 0, 12)));
cmd.lspRename.mockReturnValueOnce(new Promise((resolve) => (answer = resolve)) as never);
cmd.lspRename.mockReturnValueOnce(ok({ edit: { files: [] }, incomplete: null }));
const view = editor("mesage_count = 1", "python", 0);
const first = renameNamePart(view, { from: 0, to: 6 }, "message");
await vi.waitFor(() => expect(cmd.lspRename).toHaveBeenCalledTimes(1));
const id = (cmd.lspRename.mock.calls[0] as unknown[])[0];
await renameNamePart(view, { from: 0, to: 6 }, "massage");
expect(cmd.cancelRequest).toHaveBeenCalledWith(id);
answer({ status: "error", error: { kind: "cancelled", message: "cancelled" } });
await first;
expect(cmd.applyWorkspaceEdit).not.toHaveBeenCalled();
cmd.lspPrepareRename.mockReset();
cmd.lspPrepareRename.mockImplementation(() => unavailable());
});
it("falls back to the syntax rename of a local, references included", async () => {
cmd.syntaxLocals.mockReturnValueOnce(
ok(local({ name: "mesage", definition: r(0, 0, 6), references: [r(1, 6, 12)] })),
);
const view = editor("mesage = 'hello'\nprint(mesage)", "python", 0);
await renameNamePart(view, { from: 0, to: 6 }, "message");
expect(view.state.doc.toString()).toBe("message = 'hello'\nprint(message)");
});
it("changes nothing when the symbol can't be renamed safely", async () => {
cmd.syntaxLocals.mockReturnValueOnce(
ok({ status: "notLocal", name: "mesage", range: r(0, 0, 6), reason: "topLevel" }),
);
const view = editor("mesage = 'hello'\nprint(mesage)", "python", 0);
await renameNamePart(view, { from: 0, to: 6 }, "message");
expect(view.state.doc.toString()).toBe("mesage = 'hello'\nprint(mesage)");
expect(document.querySelector(".cm-lsp-hint")?.textContent).toContain("Cannot rename safely");
});
});
it.each(["dialog", "cancel"])("does not revive an older preparation after the current %s", async (finish) => {
let release!: (value: Awaited<R<unknown>>) => void;
cmd.lspPrepareRename.mockReturnValueOnce(new Promise((resolve) => (release = resolve)));
cmd.lspPrepareRename.mockReturnValueOnce(ok(r(0, 6, 7)));
const view = editor("const x = 1;", "typescript", 6);
view.dispatch({ effects: StateEffect.appendConfig.of(inplaceRenameExtension()) });
const older = startRename(view);
await vi.waitFor(() => expect(cmd.lspPrepareRename).toHaveBeenCalledTimes(1));
await startRename(view);
expect(inplaceSession(view.state)?.oldName).toBe("x");
await startRename(view);
expect(useLspPopup.getState().popup?.kind).toBe("rename");
if (finish === "cancel") closePopup();
release({ status: "ok", data: r(0, 6, 7) });
await older;
expect(inplaceSession(view.state)).toBeNull();
expect(useLspPopup.getState().popup?.kind ?? null).toBe(finish === "dialog" ? "rename" : null);
});
it.each(["text", "document"])("drops pending preparation when the editor %s changes", async (change) => {
let release!: (value: Awaited<R<unknown>>) => void;
cmd.lspPrepareRename.mockReturnValueOnce(new Promise((resolve) => (release = resolve)));
const view = editor("const x = 1;", "typescript", 6);
view.dispatch({ effects: StateEffect.appendConfig.of(inplaceRenameExtension()) });
const pending = startRename(view);
await vi.waitFor(() => expect(cmd.lspPrepareRename).toHaveBeenCalledTimes(1));
if (change === "text") view.dispatch({ changes: { from: 6, to: 7, insert: "other" } });
else
view.setState(
EditorState.create({
doc: view.state.doc,
extensions: [
lspDoc.of({ doc: 8, path: "/p/other.ts", language: "typescript", uiMode: null, readOnly: false }),
inplaceRenameExtension(),
],
}),
);
const before = view.state.doc.toString();
release({ status: "ok", data: r(0, 6, 7) });
await pending;
expect(inplaceSession(view.state)).toBeNull();
expect(useLspPopup.getState().popup).toBeNull();
expect(view.state.doc.toString()).toBe(before);
});
it("renames the file itself when no server covers it (a plain text file)", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(
Promise.resolve({
status: "error",
error: { kind: "unavailable", message: "no running language server supports rename" } as IdeError,
}),
);
cmd.syntaxSupport.mockReturnValueOnce(ok({ tree: false, locals: false }));
executeAction.mockResolvedValueOnce(true);
const view = editor("# notes", "markdown", 2);
await startRename(view);
expect(executeAction).toHaveBeenCalledWith("project.rename");
expect(document.querySelector(".cm-lsp-hint")).toBeNull();
});
it("keeps the hint when a server runs but lacks rename (no file fallback)", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(
Promise.resolve({
status: "error",
error: { kind: "unavailable", message: "jdtls doesn't support rename" } as IdeError,
}),
);
const view = editor("class A {}", "java", 6);
await startRename(view);
expect(executeAction).not.toHaveBeenCalledWith("project.rename");
expect(document.querySelector(".cm-lsp-hint")?.textContent).toContain("jdtls doesn't support rename");
});
it("respects a server refusal over valid code, but falls back over broken code", async () => {
cmd.lspPrepareRename.mockReturnValueOnce(ok(null));
const view = editor("const x = 1;", "typescript", 6);
await startRename(view);
expect(cmd.syntaxLocals).not.toHaveBeenCalled();
expect(document.querySelector(".cm-lsp-hint")?.textContent).toBe("This element can't be renamed");
cmd.lspPrepareRename.mockReturnValueOnce(ok(null));
cmd.syntaxErrors.mockReturnValueOnce(
ok({
errors: [{ kind: "missing", range: r(0, 12, 12), expected: ";", message: "missing `;`" }],
truncated: false,
}),
);
cmd.syntaxLocals.mockReturnValueOnce(ok(local({ references: [] })));
await startRename(view);
expect(useLspPopup.getState().popup?.kind).toBe("rename");
});
});
describe("modifier-hover navigation recovery", () => {
const target = { path: "/p/target.ts", range: r(0, 0, 1), uri: null };
let mac = false;
beforeAll(async () => {
mac = (await import("@/platform/keymap")).isMacHost();
});
function move(view: EditorView, pos = 0, primary = true) {
vi.spyOn(view, "posAtCoords").mockReturnValue(pos);
view.contentDOM.dispatchEvent(
new MouseEvent("mousemove", {
bubbles: true,
metaKey: primary && mac,
ctrlKey: primary && !mac,
}),
);
}
it.each(["yaml", "properties"])(
"%s hover underlines without a popup and clicking reuses the target",
async (language) => {
cmd.lspGoto.mockReturnValueOnce(ok([target]));
const view = editor("port: 8080", language);
move(view, 1);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("port"));
expect(cmd.lspHover).not.toHaveBeenCalled();
view.contentDOM.dispatchEvent(
new MouseEvent("mousedown", {
bubbles: true,
cancelable: true,
metaKey: mac,
ctrlKey: !mac,
}),
);
await vi.waitFor(() => expect(openFile).toHaveBeenCalledOnce());
expect(cmd.lspGoto).toHaveBeenCalledOnce();
expect(view.dom.querySelector(".cm-lsp-nav-info")).toBeNull();
},
);
it.each(["yaml", "properties"])("%s underlines the entire hyphenated configuration name", async (language) => {
cmd.lspGoto.mockReturnValueOnce(ok([target]));
const view = editor("driver-class-name: example.Driver", language);
for (const pos of [1, 6, 8, 12, 15]) {
move(view, pos);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("driver-class-name"));
}
await gotoTargets(view, "definition", 14);
expect(cmd.lspGoto).toHaveBeenCalledOnce();
expect(openFile).toHaveBeenCalledOnce();
});
it("invalidates the hover marker after dependencies change", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([target]));
const view = editor("port: 8080", "yaml");
move(view, 1);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")).not.toBeNull());
invalidateConfigurationNavigation();
cmd.lspGoto.mockReturnValueOnce(ok([]));
move(view, 1);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")).toBeNull());
expect(cmd.lspGoto).toHaveBeenCalledTimes(2);
});
it("re-probes a stuck configuration lookup through the running request, without retaining its empty answer", async () => {
let finish!: (value: Awaited<R<unknown>>) => void;
cmd.lspGoto.mockReturnValueOnce(
new Promise((resolve) => {
finish = resolve;
}),
);
const view = editor("port: 8080", "yaml");
vi.useFakeTimers();
try {
move(view, 1);
await vi.advanceTimersByTimeAsync(3001);
move(view, 1);
await vi.advanceTimersByTimeAsync(0);
expect(cmd.lspGoto).toHaveBeenCalledOnce();
} finally {
vi.useRealTimers();
}
finish({ status: "ok", data: [] });
await new Promise((resolve) => setTimeout(resolve, 20));
expect(view.dom.querySelector(".cm-lsp-link")).toBeNull();
cmd.lspGoto.mockReturnValueOnce(ok([target]));
await gotoTargets(view, "definition", 1);
expect(cmd.lspGoto).toHaveBeenCalledTimes(2);
expect(openFile).toHaveBeenCalledOnce();
});
it("shows the link without waiting for documentation", async () => {
cmd.lspGoto.mockReturnValueOnce(ok([target]));
cmd.lspHover.mockReturnValueOnce(new Promise(() => {}));
const view = editor("foo");
move(view);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("foo"));
expect(cmd.lspHover).toHaveBeenCalled();
});
it("retries a transient server error instead of caching no target", async () => {
cmd.lspGoto.mockReturnValueOnce(
Promise.resolve({ status: "error", error: { kind: "internal", message: "timeout" } }),
);
const view = editor("foo");
move(view);
await new Promise((resolve) => setTimeout(resolve, 20));
cmd.lspGoto.mockReturnValueOnce(ok([target]));
move(view);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("foo"));
expect(cmd.lspGoto).toHaveBeenCalledTimes(2);
});
it("re-probes a stuck word through the running request", async () => {
let finish!: (value: Awaited<R<unknown>>) => void;
cmd.lspGoto.mockReturnValueOnce(
new Promise((resolve) => {
finish = resolve;
}),
);
const view = editor("foo");
vi.useFakeTimers();
try {
move(view);
await vi.advanceTimersByTimeAsync(3001);
move(view);
await vi.advanceTimersByTimeAsync(0);
expect(cmd.lspGoto).toHaveBeenCalledOnce();
} finally {
vi.useRealTimers();
}
finish(await ok([target]));
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("foo"));
expect(cmd.lspGoto).toHaveBeenCalledOnce();
});
it("ignores a late probe after moving to a different word", async () => {
let finish!: (value: Awaited<R<unknown>>) => void;
cmd.lspGoto.mockReturnValueOnce(
new Promise((resolve) => {
finish = resolve;
}),
);
cmd.lspGoto.mockReturnValueOnce(ok([target]));
const view = editor("foo bar");
move(view);
move(view, 4);
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("bar"));
finish({ status: "ok", data: [target] });
await new Promise((resolve) => setTimeout(resolve, 20));
expect(view.dom.querySelector(".cm-lsp-link")?.textContent).toBe("bar");
});
});
describe("same-document fragment navigation", () => {
it.each(["definition", "declaration"] as const)(
"follows Markdown HTML anchors through %s without a server",
async (kind) => {
const source = '[artist::order](#Artist-order)\n\n<a id="Artist-order"></a>';
const view = editor(source, "markdown", source.indexOf("#Artist"), "/p/notes.md");
await gotoTargets(view, kind);
expect(view.state.selection.main.head).toBe(source.indexOf("id="));
expect(openFile).not.toHaveBeenCalled();
},
);
});