M2.2: PHP highlighting, syntax colour tokens and editor.reveal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f13a49064d
commit
6c75c08d70
25 changed files with 1044 additions and 33 deletions
|
|
@ -4,7 +4,8 @@
|
|||
* `buffer.edit`, the dirty marker, closing (middle click, shortcut, the
|
||||
* Save / Don't Save / Cancel question), saving through the global keymap
|
||||
* (`commands/store.ts`) with shortcuts loaded from the engine's command
|
||||
* list, and the notice for files changed on disk.
|
||||
* list, the notice for files changed on disk, and the UI command
|
||||
* `editor.reveal` (open and closed files, CRLF text, bad params).
|
||||
* The engine is a fake behind the mocked Tauri `invoke`.
|
||||
*/
|
||||
import { flushPromises, mount, type VueWrapper } from "@vue/test-utils";
|
||||
|
|
@ -21,6 +22,7 @@ import { emitEditorEvent } from "../../src/components/editor/engineEvents";
|
|||
import { emitEngineEvent } from "../../src/components/project/engineEvents";
|
||||
import ProjectToolWindow from "../../src/components/project/ProjectToolWindow.vue";
|
||||
import { useEditorStore } from "../../src/stores/editor";
|
||||
import { useNotificationsStore } from "../../src/stores/notifications";
|
||||
|
||||
/** A buffer of the fake engine. */
|
||||
interface FakeBuffer {
|
||||
|
|
@ -47,7 +49,14 @@ let calls: { command: string; params: Record<string, unknown> }[];
|
|||
function describeBuffer(id: number): unknown {
|
||||
const b = buffers.get(id);
|
||||
return b
|
||||
? { id, path: b.path, text: b.text, version: b.version, dirty: b.version !== b.saved, lineEnding: "lf" }
|
||||
? {
|
||||
id,
|
||||
path: b.path,
|
||||
text: b.text,
|
||||
version: b.version,
|
||||
dirty: b.version !== b.saved,
|
||||
lineEnding: b.text.includes("\r\n") ? "crlf" : "lf",
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
|
|
@ -473,3 +482,96 @@ describe("EditorArea", () => {
|
|||
expect(timing?.stateMs).toBeLessThan(2000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("editor.reveal", () => {
|
||||
beforeEach(() => {
|
||||
disk["c.php"] = "<?php\nfunction a() {}\n\n $x = 'ü😀z';\n";
|
||||
disk["w.php"] = "<?php\r\n$a = 1;\r\n$b = 2;\r\n";
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("opens a closed file, activates it, places the cursor and centres it", async () => {
|
||||
const scroll = vi.spyOn(EditorView, "scrollIntoView");
|
||||
const area = await mountArea();
|
||||
const editor = useEditorStore(pinia);
|
||||
await editor.openFile("a.php");
|
||||
await flushPromises();
|
||||
await useCommandsStore(pinia).dispatch("editor.reveal", { path: "c.php", line: 3, column: 9 });
|
||||
await flushPromises();
|
||||
expect(calls.filter((c) => c.command === "file.open").map((c) => c.params.path)).toEqual(["a.php", "c.php"]);
|
||||
expect(editor.activeTab?.path).toBe("c.php");
|
||||
const view = viewOf(area);
|
||||
const head = view.state.selection.main.head;
|
||||
expect(view.state.doc.lineAt(head).number).toBe(4);
|
||||
expect(view.state.sliceDoc(head, head + 3)).toBe("😀z");
|
||||
expect(scroll).toHaveBeenLastCalledWith(head, { y: "center" });
|
||||
});
|
||||
|
||||
it("activates an open tab without opening the file again", async () => {
|
||||
const area = await mountArea();
|
||||
const editor = useEditorStore(pinia);
|
||||
await editor.openFile("c.php");
|
||||
await editor.openFile("b.txt");
|
||||
await flushPromises();
|
||||
await useCommandsStore(pinia).execute("editor.reveal", { path: "c.php", line: 1, column: 9 });
|
||||
await flushPromises();
|
||||
expect(calls.filter((c) => c.command === "file.open")).toHaveLength(2);
|
||||
expect(editor.activeTab?.path).toBe("c.php");
|
||||
expect(viewOf(area).state.selection.main.head).toBe(6 + 9);
|
||||
});
|
||||
|
||||
it("maps positions in CRLF text through the engine line breaks", async () => {
|
||||
const area = await mountArea();
|
||||
const editor = useEditorStore(pinia);
|
||||
await useCommandsStore(pinia).dispatch("editor.reveal", { path: "w.php", line: 2, column: 1 });
|
||||
await flushPromises();
|
||||
const view = viewOf(area);
|
||||
const head = view.state.selection.main.head;
|
||||
expect(head).toBe(15);
|
||||
expect(view.state.sliceDoc(head, head + 1)).toBe("b");
|
||||
const engineText = disk["w.php"] ?? "";
|
||||
const crlfBefore = engineText.slice(0, 16).split("\r\n").length - 1;
|
||||
expect(engineText.charAt(head + crlfBefore)).toBe("b");
|
||||
expect(editor.activeTab?.path).toBe("w.php");
|
||||
});
|
||||
|
||||
it("clamps positions past the end", async () => {
|
||||
const area = await mountArea();
|
||||
await useCommandsStore(pinia).dispatch("editor.reveal", { path: "w.php", line: 0, column: 99 });
|
||||
await flushPromises();
|
||||
expect(viewOf(area).state.selection.main.head).toBe(5);
|
||||
await useCommandsStore(pinia).dispatch("editor.reveal", { path: "w.php", line: 99, column: 0 });
|
||||
await flushPromises();
|
||||
expect(viewOf(area).state.selection.main.head).toBe(viewOf(area).state.doc.length);
|
||||
});
|
||||
|
||||
it("centres the cursor of the active tab without a path (palette)", async () => {
|
||||
const scroll = vi.spyOn(EditorView, "scrollIntoView");
|
||||
const area = await mountArea();
|
||||
await useEditorStore(pinia).openFile("c.php");
|
||||
await flushPromises();
|
||||
viewOf(area).dispatch({ selection: { anchor: 8 } });
|
||||
await useCommandsStore(pinia).execute("editor.reveal");
|
||||
await flushPromises();
|
||||
expect(viewOf(area).state.selection.main.head).toBe(8);
|
||||
expect(scroll).toHaveBeenLastCalledWith(8, { y: "center" });
|
||||
});
|
||||
|
||||
it("reports bad params and files that cannot be opened", async () => {
|
||||
await mountArea();
|
||||
const commands = useCommandsStore(pinia);
|
||||
await commands.dispatch("editor.reveal", { path: 3, line: 0, column: 0 });
|
||||
await commands.dispatch("editor.reveal", { path: "c.php", line: "1" });
|
||||
await commands.dispatch("editor.reveal", { path: "missing.php", line: 0, column: 0 });
|
||||
await flushPromises();
|
||||
const texts = useNotificationsStore(pinia).toasts.map((t) => t.text);
|
||||
expect(texts).toHaveLength(3);
|
||||
expect(texts[0]).toContain("needs a path");
|
||||
expect(texts[1]).toContain("needs a path");
|
||||
expect(texts[2]).toContain("not_found");
|
||||
expect(useEditorStore(pinia).tabs).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue