M2.6: go-to popup for files, classes and symbols
One popup with Files / Classes / Symbols tabs (Tab switches), opened by
goto.fileOpen (Mod+P), goto.classOpen (Mod+Alt+C) and goto.symbolOpen
(Mod+T), each with an optional {query} for agents. Live debounced query
against goto.* with stale answers dropped, matched characters marked,
directory / namespace / container column, library rows labelled, an
indexing hint with progress and a re-query on indexReady. Enter or a
click opens through editor.reveal; Escape closes. An exclusive modal and
a launcher: any other command closes it.
goto.file items now carry `library` (PM decision). indexProgress and
indexReady join the ipc event union. Notes in plan/phpstorm/08-goto.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ef198f5f8f
commit
943ff889df
21 changed files with 1650 additions and 23 deletions
471
ui/tests/goto/GotoPopup.test.ts
Normal file
471
ui/tests/goto/GotoPopup.test.ts
Normal file
|
|
@ -0,0 +1,471 @@
|
|||
/**
|
||||
* Tests of the go-to popup (M2.6): the three open commands and their
|
||||
* shortcuts, the debounced live query against `goto.*` with stale answers
|
||||
* dropped, match marks, the path / namespace / container column, library
|
||||
* rows, the indexing hint and the re-query on `indexReady`, opening through
|
||||
* `editor.reveal`, Escape, mode switching, the one-modal rule against the
|
||||
* palette, and the parsing helpers of the store. The engine is a fake
|
||||
* behind the mocked Tauri `invoke` and `listen`.
|
||||
*/
|
||||
import { flushPromises, mount, type VueWrapper } from "@vue/test-utils";
|
||||
import { createPinia, setActivePinia, type Pinia } from "pinia";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { defineComponent, h } from "vue";
|
||||
|
||||
const invoke = vi.hoisted(() => vi.fn());
|
||||
const listen = vi.hoisted(() => vi.fn());
|
||||
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
|
||||
vi.mock("@tauri-apps/api/event", () => ({ listen }));
|
||||
|
||||
import { useModalsStore } from "../../src/commands/modals";
|
||||
import { useCommandsStore } from "../../src/commands/store";
|
||||
import { UI_COMMANDS } from "../../src/commands/uiCommands";
|
||||
import GotoPopup from "../../src/components/goto/GotoPopup.vue";
|
||||
import { segments } from "../../src/components/goto/marks";
|
||||
import CommandPalette from "../../src/components/palette/CommandPalette.vue";
|
||||
import {
|
||||
DEBOUNCE_MS,
|
||||
fileItem,
|
||||
markSubsequence,
|
||||
readRanges,
|
||||
symbolItem,
|
||||
useGotoStore,
|
||||
} from "../../src/stores/goto";
|
||||
import { useProjectStore } from "../../src/stores/project";
|
||||
|
||||
/** A pending answer of the fake engine. */
|
||||
interface Deferred {
|
||||
/** Answers with a value. */
|
||||
resolve: (value: unknown) => void;
|
||||
}
|
||||
|
||||
/** Every `engine_execute` call, in order. */
|
||||
let calls: { command: string; params: Record<string, unknown> }[];
|
||||
/** Answers of the go-to commands by command id; a function answers per query. */
|
||||
let answers: Map<string, (params: Record<string, unknown>) => unknown>;
|
||||
/** Go-to calls held back until the test resolves them, when set. */
|
||||
let held: Deferred[] | null;
|
||||
/** The engine event callbacks, one per subscription. */
|
||||
let listeners: ((payload: unknown) => void)[];
|
||||
/** Pinia of the current test. */
|
||||
let pinia: Pinia;
|
||||
/** Mounted components, unmounted after each test. */
|
||||
let mounted: VueWrapper[];
|
||||
/** Params of every `editor.reveal` the stand-in editor ran. */
|
||||
let revealed: unknown[];
|
||||
|
||||
/** The fake engine's listing: the registered UI commands. */
|
||||
function listing(): unknown[] {
|
||||
const registered = calls.find((c) => c.command === "ui.registerCommands")?.params as
|
||||
| { commands: { id: string; labelKey: string; defaultShortcut?: string }[] }
|
||||
| undefined;
|
||||
return (registered?.commands ?? []).map((c) => ({
|
||||
id: c.id,
|
||||
label: c.labelKey,
|
||||
labelKey: c.labelKey,
|
||||
menuPath: null,
|
||||
defaultShortcut: c.defaultShortcut ?? null,
|
||||
params: "",
|
||||
enabled: true,
|
||||
ui: true,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an engine event to every subscriber.
|
||||
*
|
||||
* @param payload - the event.
|
||||
*/
|
||||
async function emit(payload: unknown): Promise<void> {
|
||||
for (const listener of listeners) {
|
||||
listener(payload);
|
||||
}
|
||||
await flushPromises();
|
||||
}
|
||||
|
||||
/** Waits out the debounce and lets the answers arrive. */
|
||||
async function settle(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, DEBOUNCE_MS + 20));
|
||||
await flushPromises();
|
||||
}
|
||||
|
||||
/**
|
||||
* Presses a key on the focused element (or the body).
|
||||
*
|
||||
* @param init - key event properties.
|
||||
*/
|
||||
async function press(init: KeyboardEventInit): Promise<void> {
|
||||
const target = document.activeElement ?? document.body;
|
||||
target.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init }));
|
||||
await flushPromises();
|
||||
}
|
||||
|
||||
/** A focusable button standing in for the editor. */
|
||||
const Host = defineComponent({
|
||||
setup: () => () => h("button", { id: "host-button" }, "host"),
|
||||
});
|
||||
|
||||
/**
|
||||
* Mounts the popup (and the palette) next to a focusable button, opens a
|
||||
* project, provides a stand-in `editor.reveal` and starts the commands.
|
||||
*
|
||||
* @param withProject - whether a project is open.
|
||||
*/
|
||||
async function mountPopup(withProject = true): Promise<VueWrapper> {
|
||||
const host = mount(Host, { attachTo: document.body });
|
||||
const palette = mount(CommandPalette, { attachTo: document.body, global: { plugins: [pinia] } });
|
||||
const wrapper = mount(GotoPopup, { attachTo: document.body, global: { plugins: [pinia] } });
|
||||
mounted.push(host, palette, wrapper);
|
||||
if (withProject) {
|
||||
useProjectStore(pinia).root = { path: "/p", name: "p" };
|
||||
}
|
||||
const commands = useCommandsStore(pinia);
|
||||
commands.provide("editor.reveal", {
|
||||
run: (params) => {
|
||||
revealed.push(params);
|
||||
},
|
||||
});
|
||||
await commands.start();
|
||||
await flushPromises();
|
||||
(host.element as HTMLElement).focus();
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Types into the popup's field.
|
||||
*
|
||||
* @param wrapper - the popup.
|
||||
* @param text - the query.
|
||||
*/
|
||||
async function type(wrapper: VueWrapper, text: string): Promise<void> {
|
||||
await wrapper.find("[data-test=goto-input]").setValue(text);
|
||||
await settle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The visible rows as `label | detail`.
|
||||
*
|
||||
* @param wrapper - the popup.
|
||||
*/
|
||||
function rows(wrapper: VueWrapper): string[] {
|
||||
return wrapper
|
||||
.findAll("[data-test=goto-row]")
|
||||
.map((row) => `${row.find("[data-test=goto-label]").text()} | ${row.find("[data-test=goto-detail]").text()}`);
|
||||
}
|
||||
|
||||
/** Calls of one engine command. */
|
||||
function callsOf(command: string): Record<string, unknown>[] {
|
||||
return calls.filter((c) => c.command === command).map((c) => c.params);
|
||||
}
|
||||
|
||||
/** Two files, one of them in vendor. */
|
||||
const FILES = {
|
||||
items: [
|
||||
{ path: "src/Models/User.php", score: 90, matches: [[11, 15]], library: false },
|
||||
{ path: "vendor/acme/User.php", score: 80, matches: [[11, 15]], library: true },
|
||||
],
|
||||
indexing: false,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
pinia = createPinia();
|
||||
setActivePinia(pinia);
|
||||
calls = [];
|
||||
answers = new Map([["goto.file", () => FILES]]);
|
||||
held = null;
|
||||
listeners = [];
|
||||
mounted = [];
|
||||
revealed = [];
|
||||
Element.prototype.scrollIntoView = () => undefined;
|
||||
invoke.mockReset();
|
||||
listen.mockReset();
|
||||
listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => {
|
||||
listeners.push((payload) => {
|
||||
callback({ payload });
|
||||
});
|
||||
return Promise.resolve(() => undefined);
|
||||
});
|
||||
invoke.mockImplementation((cmd: string, args?: { command: string; params: Record<string, unknown> }) => {
|
||||
if (cmd === "engine_list_commands") {
|
||||
return Promise.resolve(listing());
|
||||
}
|
||||
if (cmd === "engine_execute" && args) {
|
||||
calls.push({ command: args.command, params: args.params });
|
||||
const answer = answers.get(args.command);
|
||||
if (answer) {
|
||||
const value = answer(args.params);
|
||||
if (held) {
|
||||
const queue = held;
|
||||
return new Promise((resolve) => {
|
||||
queue.push({
|
||||
resolve: () => {
|
||||
resolve(value);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
}
|
||||
return Promise.resolve(null);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
useCommandsStore(pinia).stop();
|
||||
for (const wrapper of mounted) {
|
||||
wrapper.unmount();
|
||||
}
|
||||
});
|
||||
|
||||
describe("go-to commands", () => {
|
||||
it("registers three open commands with Lutra's shortcuts", () => {
|
||||
const shortcuts = Object.fromEntries(
|
||||
UI_COMMANDS.filter((c) => c.id.startsWith("goto.")).map((c) => [c.id, c.defaultShortcut]),
|
||||
);
|
||||
expect(shortcuts).toEqual({ "goto.classOpen": "Mod+Alt+C", "goto.fileOpen": "Mod+P", "goto.symbolOpen": "Mod+T" });
|
||||
});
|
||||
|
||||
it("opens in file mode with Ctrl+P and focuses the field", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await press({ key: "p", code: "KeyP", ctrlKey: true });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(true);
|
||||
expect(document.activeElement).toBe(wrapper.find("[data-test=goto-input]").element);
|
||||
expect(wrapper.find(".goto__mode--active").attributes("data-mode")).toBe("file");
|
||||
expect(wrapper.find("[data-test=goto-empty]").text()).toBe("Type a name");
|
||||
expect(useModalsStore(pinia).current).toBe("goto");
|
||||
});
|
||||
|
||||
it("is disabled without a project", async () => {
|
||||
const wrapper = await mountPopup(false);
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
});
|
||||
|
||||
it("opens from a uiCommand with a query and searches at once", async () => {
|
||||
answers.set("goto.class", () => ({
|
||||
items: [
|
||||
{ name: "User", fqn: "App\\Models\\User", kind: "class", path: "src/Models/User.php", line: 3, column: 12, library: false },
|
||||
],
|
||||
indexing: false,
|
||||
}));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.classOpen", params: { query: "usr" } });
|
||||
expect(callsOf("goto.class")).toEqual([{ query: "usr", limit: 100 }]);
|
||||
expect(rows(wrapper)).toEqual(["User | App\\Models"]);
|
||||
expect(wrapper.findAll("mark").map((m) => m.text())).toEqual(["Us", "r"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("live query", () => {
|
||||
it("debounces typing, marks the matched characters and dims library rows", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
const input = wrapper.find("[data-test=goto-input]");
|
||||
await input.setValue("u");
|
||||
await input.setValue("us");
|
||||
await input.setValue("user");
|
||||
await settle();
|
||||
expect(callsOf("goto.file")).toEqual([{ query: "user", limit: 100 }]);
|
||||
expect(rows(wrapper)).toEqual(["User.php | src/Models/", "User.php | vendor/acme/"]);
|
||||
expect(wrapper.findAll("[data-test=goto-row]")[0]?.find("mark").text()).toBe("User");
|
||||
const library = wrapper.findAll("[data-test=goto-row]")[1];
|
||||
expect(library?.classes()).toContain("goto__row--library");
|
||||
expect(library?.find("[data-test=goto-library]").text()).toBe("library");
|
||||
expect(wrapper.findAll("[data-test=goto-library]")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("drops an answer that arrives after a newer one", async () => {
|
||||
answers.set("goto.file", (params) => ({
|
||||
items: [{ path: `src/${String(params.query)}.php`, score: 1, matches: [], library: false }],
|
||||
indexing: false,
|
||||
}));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
held = [];
|
||||
const queue = held;
|
||||
await type(wrapper, "old");
|
||||
await type(wrapper, "new");
|
||||
expect(queue).toHaveLength(2);
|
||||
queue[1]?.resolve(null);
|
||||
await flushPromises();
|
||||
queue[0]?.resolve(null);
|
||||
await flushPromises();
|
||||
expect(rows(wrapper)).toEqual(["new.php | src/"]);
|
||||
});
|
||||
|
||||
it("shows no matches, and an error from the engine", async () => {
|
||||
answers.set("goto.file", () => ({ items: [], indexing: false }));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
await type(wrapper, "zzz");
|
||||
expect(wrapper.find("[data-test=goto-empty]").text()).toBe("Nothing matches");
|
||||
invoke.mockImplementationOnce(() => Promise.reject(Object.assign(new Error("x"), { code: "disabled", message: "no project" })));
|
||||
await type(wrapper, "zzzz");
|
||||
expect(wrapper.find("[data-test=goto-hint]").text()).toBe("no project");
|
||||
});
|
||||
|
||||
it("shows the indexing hint with progress and re-queries on indexReady", async () => {
|
||||
let indexing = true;
|
||||
answers.set("goto.file", () => ({ ...FILES, items: indexing ? FILES.items.slice(0, 1) : FILES.items, indexing }));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: { query: "user" } });
|
||||
expect(wrapper.find("[data-test=goto-hint]").text()).toBe("Indexing… results may be incomplete");
|
||||
await emit({ kind: "indexProgress", done: 120, total: 400 });
|
||||
expect(wrapper.find("[data-test=goto-hint]").text()).toBe("Indexing 120 of 400 files… results may be incomplete");
|
||||
expect(rows(wrapper)).toHaveLength(1);
|
||||
indexing = false;
|
||||
await emit({ kind: "indexReady", files: 400, symbols: 900, elapsedMs: 50 });
|
||||
expect(callsOf("goto.file")).toHaveLength(2);
|
||||
expect(rows(wrapper)).toHaveLength(2);
|
||||
expect(wrapper.find("[data-test=goto-hint]").exists()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("keyboard and opening", () => {
|
||||
it("opens a file with Enter through editor.reveal and gives the focus back", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: { query: "user" } });
|
||||
await press({ key: "ArrowDown" });
|
||||
await press({ key: "Enter" });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
expect(revealed).toEqual([{ path: "vendor/acme/User.php" }]);
|
||||
expect(document.activeElement?.id).toBe("host-button");
|
||||
expect(useModalsStore(pinia).current).toBe(null);
|
||||
});
|
||||
|
||||
it("reveals a symbol at its line and column on click", async () => {
|
||||
answers.set("goto.symbol", () => ({
|
||||
items: [
|
||||
{ name: "find", fqn: "App\\User::find", kind: "method", container: "App\\User", path: "src/User.php", line: 7, column: 20, library: false },
|
||||
],
|
||||
indexing: false,
|
||||
}));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.symbolOpen", params: { query: "User::fi" } });
|
||||
expect(rows(wrapper)).toEqual(["find | App\\User"]);
|
||||
expect(wrapper.findAll("mark").map((m) => m.text())).toEqual(["fi"]);
|
||||
await wrapper.find("[data-test=goto-row]").trigger("click");
|
||||
expect(revealed).toEqual([{ path: "src/User.php", line: 7, column: 20 }]);
|
||||
});
|
||||
|
||||
it("switches modes with Tab and keeps the text", async () => {
|
||||
answers.set("goto.class", () => ({ items: [], indexing: false }));
|
||||
answers.set("goto.symbol", () => ({ items: [], indexing: false }));
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: { query: "user" } });
|
||||
await press({ key: "Tab" });
|
||||
expect(wrapper.find(".goto__mode--active").attributes("data-mode")).toBe("class");
|
||||
await press({ key: "Tab" });
|
||||
await press({ key: "Tab", shiftKey: true });
|
||||
expect(wrapper.find(".goto__mode--active").attributes("data-mode")).toBe("class");
|
||||
expect(callsOf("goto.class").map((p) => p.query)).toEqual(["user", "user"]);
|
||||
expect(callsOf("goto.symbol").map((p) => p.query)).toEqual(["user"]);
|
||||
await wrapper.find("[data-mode=file]").trigger("click");
|
||||
await flushPromises();
|
||||
expect(wrapper.find(".goto__mode--active").attributes("data-mode")).toBe("file");
|
||||
expect((wrapper.find("[data-test=goto-input]").element as HTMLInputElement).value).toBe("user");
|
||||
});
|
||||
|
||||
it("closes with Escape and on a click outside", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
await press({ key: "Escape" });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
await wrapper.find("[data-test=goto]").trigger("mousedown");
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
});
|
||||
|
||||
it("never stacks with the palette", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "palette.open", params: {} });
|
||||
expect(document.querySelector("[data-test=palette]")).not.toBeNull();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: {} });
|
||||
expect(document.querySelector("[data-test=palette]")).toBeNull();
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(true);
|
||||
await emit({ kind: "uiCommand", id: "palette.open", params: {} });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
expect(useModalsStore(pinia).current).toBe("palette");
|
||||
});
|
||||
});
|
||||
|
||||
describe("launcher", () => {
|
||||
it("closes when another command runs, also from the control channel", async () => {
|
||||
const wrapper = await mountPopup();
|
||||
await emit({ kind: "uiCommand", id: "goto.fileOpen", params: { query: "user" } });
|
||||
await emit({ kind: "uiCommand", id: "goto.classOpen", params: {} });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(true);
|
||||
await emit({ kind: "uiCommand", id: "editor.reveal", params: { path: "src/Models/User.php" } });
|
||||
expect(wrapper.find("[data-test=goto]").exists()).toBe(false);
|
||||
expect(revealed).toEqual([{ path: "src/Models/User.php" }]);
|
||||
expect(useModalsStore(pinia).current).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parsing", () => {
|
||||
it("clamps and drops malformed ranges", () => {
|
||||
expect(readRanges([[2, 4], [9, 99], [5, 5], ["a", 1], [1], null], 10)).toEqual([
|
||||
[2, 4],
|
||||
[9, 10],
|
||||
]);
|
||||
expect(readRanges("x", 10)).toEqual([]);
|
||||
});
|
||||
|
||||
it("splits path marks at the file name and marks vendor as library", () => {
|
||||
expect(fileItem({ path: "vendor/a/Bc.php", matches: [[7, 12]] }, 0)).toMatchObject({
|
||||
label: "Bc.php",
|
||||
labelMatches: [[0, 3]],
|
||||
detail: "vendor/a/",
|
||||
detailMatches: [[7, 9]],
|
||||
library: true,
|
||||
line: null,
|
||||
});
|
||||
expect(fileItem({ path: "" }, 0)).toBeNull();
|
||||
expect(fileItem("nope", 0)).toBeNull();
|
||||
});
|
||||
|
||||
it("reads symbol items and drops bad ones", () => {
|
||||
expect(symbolItem({ name: "f", path: "a.php", line: -1, column: 1.5 }, 0, "f")).toMatchObject({
|
||||
label: "f",
|
||||
detail: "",
|
||||
line: null,
|
||||
column: null,
|
||||
kind: null,
|
||||
});
|
||||
expect(symbolItem({ name: 3, path: "a.php" }, 0, "f")).toBeNull();
|
||||
});
|
||||
|
||||
it("marks query characters in order, ignoring case", () => {
|
||||
expect(markSubsequence("UserRepository", "urep")).toEqual([
|
||||
[0, 1],
|
||||
[3, 4],
|
||||
[5, 7],
|
||||
]);
|
||||
expect(markSubsequence("$email", "$em")).toEqual([[1, 3]]);
|
||||
expect(markSubsequence("abc", "x")).toEqual([]);
|
||||
});
|
||||
|
||||
it("cuts text into marked segments", () => {
|
||||
expect(segments("abcdef", [[1, 3], [2, 4], [5, 9]])).toEqual([
|
||||
{ text: "a", mark: false },
|
||||
{ text: "bc", mark: true },
|
||||
{ text: "d", mark: true },
|
||||
{ text: "e", mark: false },
|
||||
{ text: "f", mark: true },
|
||||
]);
|
||||
expect(segments("", [])).toEqual([]);
|
||||
});
|
||||
|
||||
it("ignores a stale answer after the popup closed", async () => {
|
||||
const goto = useGotoStore(pinia);
|
||||
held = [];
|
||||
const queue = held;
|
||||
const pending = goto.show("file", "user");
|
||||
goto.hide();
|
||||
queue[0]?.resolve(null);
|
||||
await pending;
|
||||
expect(goto.items).toEqual([]);
|
||||
expect(goto.open).toBe(false);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue