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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
*/
|
||||
import { ChangeSet, Text } from "@codemirror/state";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { diffDocs, EngineText, separatorOf } from "../../src/components/editor/engineText";
|
||||
import { diffDocs, EngineText, positionOf, separatorOf } from "../../src/components/editor/engineText";
|
||||
|
||||
describe("EngineText", () => {
|
||||
it("maps positions past CRLF breaks and keeps mixed endings", () => {
|
||||
|
|
@ -56,3 +56,26 @@ describe("EngineText", () => {
|
|||
expect(separatorOf("?")).toBe("\n");
|
||||
});
|
||||
});
|
||||
|
||||
describe("positionOf", () => {
|
||||
it("maps engine line and UTF-16 column to the same text in CRLF and LF documents", () => {
|
||||
for (const engine of ["ab\r\ncd😀e\r\n\r\nf", "ab\ncd😀e\n\nf", "ab\rcd😀e\r\rf"]) {
|
||||
const text = EngineText.fromEngine(engine);
|
||||
const pos = positionOf(text.doc, 1, 4);
|
||||
expect(text.doc.sliceString(pos, pos + 1)).toBe("e");
|
||||
expect(engine.charAt(text.toEngine(pos))).toBe("e");
|
||||
const f = positionOf(text.doc, 3, 0);
|
||||
expect(engine.charAt(text.toEngine(f))).toBe("f");
|
||||
}
|
||||
});
|
||||
|
||||
it("clamps lines and columns into the document", () => {
|
||||
const doc = EngineText.fromEngine("ab\r\ncd").doc;
|
||||
expect(positionOf(doc, 0, 99)).toBe(2);
|
||||
expect(positionOf(doc, 99, 1)).toBe(4);
|
||||
expect(positionOf(doc, -3, -1)).toBe(0);
|
||||
expect(positionOf(doc, 1.8, 1.9)).toBe(4);
|
||||
expect(positionOf(doc, Number.NaN, Number.POSITIVE_INFINITY)).toBe(0);
|
||||
expect(positionOf(Text.empty, 4, 4)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
111
ui/tests/editor/highlightPerf.test.ts
Normal file
111
ui/tests/editor/highlightPerf.test.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* Keystroke-to-highlight on a 1 MB PHP file (budget: < 16 ms, C3 contract).
|
||||
*
|
||||
* Opt-in, since it measures rather than checks behaviour: run it with
|
||||
* `LUTRA_PERF=1 pnpm vitest run tests/editor/highlightPerf.test.ts` on an
|
||||
* idle machine. It builds a 1 MB PHP file, parses it completely once (as the
|
||||
* editor does in the background after opening), then types characters into
|
||||
* a view at the top and in the middle of the file. Each keystroke is one
|
||||
* `view.dispatch`, which runs the incremental Lezer reparse up to the
|
||||
* visible range and redraws the highlighted lines; it also times the
|
||||
* incremental reparse of the whole document after an edit (the background
|
||||
* work the editor spreads over idle time). jsdom has no layout or paint, so
|
||||
* the numbers are the JavaScript work of a keystroke, not the frame.
|
||||
*/
|
||||
import { ensureSyntaxTree, forceParsing, syntaxTree } from "@codemirror/language";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { editorExtensions } from "../../src/components/editor/extensions";
|
||||
|
||||
/** One class of the generated file, about 1 KB. */
|
||||
function chunk(n: number): string {
|
||||
const i = String(n);
|
||||
return [
|
||||
`/** Shop item ${i}. */`,
|
||||
`final class Item${i} extends Base implements \\JsonSerializable`,
|
||||
"{",
|
||||
` public const LIMIT = ${i};`,
|
||||
" private array $tags = ['otter', \"stone\", 'river'];",
|
||||
"",
|
||||
" public function __construct(private int $id, protected ?string $name = null) {}",
|
||||
"",
|
||||
" // Sums the price with tax.",
|
||||
" public function total(float $price, int $count = 1): float",
|
||||
" {",
|
||||
" $sum = $price * $count * 1.19;",
|
||||
" if ($sum > self::LIMIT && $this->name !== null) {",
|
||||
' return round($sum, 2) + strlen("{$this->name}");',
|
||||
" }",
|
||||
" foreach ($this->tags as $k => $tag) { $sum += \\count([$k, $tag]); }",
|
||||
" return $sum;",
|
||||
" }",
|
||||
"",
|
||||
" public function jsonSerialize(): mixed { return ['id' => $this->id, 'name' => $this->name]; }",
|
||||
"}",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/** Median and 95th percentile of samples, in ms. */
|
||||
function stats(samples: number[]): { median: number; p95: number; max: number } {
|
||||
const sorted = [...samples].sort((a, b) => a - b);
|
||||
const at = (q: number): number => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))] ?? 0;
|
||||
return { median: at(0.5), p95: at(0.95), max: sorted.at(-1) ?? 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Types into a view of the text with the extensions of a path.
|
||||
*
|
||||
* @param path - picks the language (`big.php`, or `big.txt` for the plain
|
||||
* text baseline).
|
||||
* @param text - the document.
|
||||
* @returns timings in ms.
|
||||
*/
|
||||
function measure(path: string, text: string): Record<string, unknown> {
|
||||
const view = new EditorView({ state: EditorState.create({ doc: text, extensions: editorExtensions(path) }), parent: document.body });
|
||||
let started = performance.now();
|
||||
forceParsing(view, text.length, 60_000);
|
||||
const fullParseMs = performance.now() - started;
|
||||
const parsed = syntaxTree(view.state).length;
|
||||
const typing = (pos: number, count: number): number[] => {
|
||||
const samples: number[] = [];
|
||||
view.dispatch({ selection: { anchor: pos }, effects: EditorView.scrollIntoView(pos, { y: "center" }) });
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const at = view.state.selection.main.head;
|
||||
const t0 = performance.now();
|
||||
view.dispatch({ changes: { from: at, insert: i % 10 === 9 ? "\n" : "x" }, selection: { anchor: at + 1 }, userEvent: "input.type" });
|
||||
samples.push(performance.now() - t0);
|
||||
}
|
||||
return samples;
|
||||
};
|
||||
const lineStart = (n: number): number => view.state.doc.line(n).from + 8;
|
||||
typing(lineStart(20), 50);
|
||||
const top = stats(typing(lineStart(30), 300));
|
||||
const middleLine = Math.floor(view.state.doc.lines / 2);
|
||||
typing(lineStart(middleLine - 40), 50);
|
||||
const middle = stats(typing(lineStart(middleLine), 300));
|
||||
const edited = view.state.update({ changes: { from: lineStart(middleLine), insert: "$" } }).state;
|
||||
started = performance.now();
|
||||
ensureSyntaxTree(edited, edited.doc.length, 60_000);
|
||||
const reparseMs = performance.now() - started;
|
||||
view.destroy();
|
||||
return { fullParseMs, keystrokeTop: top, keystrokeMiddle: middle, fullIncrementalReparseMs: reparseMs, parsed };
|
||||
}
|
||||
|
||||
describe.runIf(process.env.LUTRA_PERF)("keystroke to highlight, 1 MB PHP", () => {
|
||||
it("stays within the frame budget", () => {
|
||||
Range.prototype.getClientRects = () => Object.assign([] as DOMRect[], { item: () => null });
|
||||
Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0);
|
||||
let text = "<?php\n\nnamespace App\\Shop;\n\n";
|
||||
for (let i = 0; text.length < 1024 * 1024; i += 1) {
|
||||
text += chunk(i);
|
||||
}
|
||||
const php = measure("big.php", text);
|
||||
const plain = measure("big.txt", text);
|
||||
const round = (_k: string, v: unknown): unknown => (typeof v === "number" ? Math.round(v * 100) / 100 : v);
|
||||
console.log(JSON.stringify({ bytes: text.length, php, plainTextBaseline: plain }, round, 2));
|
||||
expect(php.parsed).toBe(text.length);
|
||||
expect((php.keystrokeMiddle as { median: number }).median).toBeLessThan(16);
|
||||
}, 120_000);
|
||||
});
|
||||
149
ui/tests/editor/language.test.ts
Normal file
149
ui/tests/editor/language.test.ts
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/**
|
||||
* Tests of the editor's language support (`language.ts`): the language
|
||||
* picked per file extension, and the highlight classes (`lt-syntax-*`) on
|
||||
* the spans CodeMirror draws for PHP, embedded HTML and the other
|
||||
* languages, each coloured by its `--lt-syntax-*` token.
|
||||
*/
|
||||
import { ensureSyntaxTree } from "@codemirror/language";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { editorExtensions, lutraEditorTheme } from "../../src/components/editor/extensions";
|
||||
import { languageOf, SYNTAX_TOKENS } from "../../src/components/editor/language";
|
||||
|
||||
/** Views to destroy after each test. */
|
||||
let views: EditorView[] = [];
|
||||
|
||||
/** jsdom has no layout; give text ranges empty rectangles. */
|
||||
function stubRangeGeometry(): void {
|
||||
const empty = (): DOMRectList => Object.assign([] as DOMRect[], { item: () => null });
|
||||
Range.prototype.getClientRects = empty;
|
||||
Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a document in a view with the extensions of a file path, parsed
|
||||
* completely.
|
||||
*
|
||||
* @param path - the file path that picks the language.
|
||||
* @param doc - the text.
|
||||
*/
|
||||
function show(path: string, doc: string): EditorView {
|
||||
const state = EditorState.create({ doc, extensions: editorExtensions(path) });
|
||||
ensureSyntaxTree(state, state.doc.length, 5000);
|
||||
const view = new EditorView({ state, parent: document.body });
|
||||
views.push(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* The text of every span with a highlight class, by token.
|
||||
*
|
||||
* @param view - the view.
|
||||
*/
|
||||
function highlighted(view: EditorView): Record<string, string[]> {
|
||||
const out: Record<string, string[]> = {};
|
||||
for (const token of SYNTAX_TOKENS) {
|
||||
const spans = [...view.contentDOM.querySelectorAll(`.lt-syntax-${token}`)];
|
||||
if (spans.length > 0) {
|
||||
out[token] = spans.map((span) => span.textContent);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
stubRangeGeometry();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const view of views) {
|
||||
view.destroy();
|
||||
}
|
||||
views = [];
|
||||
});
|
||||
|
||||
describe("languageOf", () => {
|
||||
it.each([
|
||||
["src/Controller/Home.php", "php"],
|
||||
["templates/view.PHTML", "php"],
|
||||
["legacy\\lib.inc", "php"],
|
||||
["public/index.html", "html"],
|
||||
["assets/app.css", "css"],
|
||||
["assets/app.js", "javascript"],
|
||||
["assets/app.tsx", "typescript"],
|
||||
["composer.json", "json"],
|
||||
["config/services.yaml", "yaml"],
|
||||
[".github/ci.yml", "yaml"],
|
||||
["README.md", "markdown"],
|
||||
])("%s is %s", (path, id) => {
|
||||
expect(languageOf(path)).toBe(id);
|
||||
});
|
||||
|
||||
it.each(["Makefile", ".env", "notes.txt", "archive.tar.gz", "dir.php/file", ".php", ""])(
|
||||
"%s is plain text",
|
||||
(path) => {
|
||||
expect(languageOf(path)).toBeNull();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("highlighting", () => {
|
||||
it("marks PHP tokens with lt-syntax classes", () => {
|
||||
const view = show(
|
||||
"a.php",
|
||||
[
|
||||
"<?php",
|
||||
"// a note",
|
||||
"namespace App;",
|
||||
"final class Shop extends Base {",
|
||||
" const LIMIT = 10;",
|
||||
" public function total(int $count): float {",
|
||||
' return $count * 4.5 + strlen("otter") + self::LIMIT;',
|
||||
" }",
|
||||
"}",
|
||||
"$ok = true;",
|
||||
].join("\n"),
|
||||
);
|
||||
const marks = highlighted(view);
|
||||
expect(marks.comment).toContain("// a note");
|
||||
expect(marks.keyword).toEqual(expect.arrayContaining(["namespace", "final", "class", "extends", "public", "function", "return"]));
|
||||
expect(marks.type).toEqual(expect.arrayContaining(["Shop", "int", "float"]));
|
||||
expect(marks.variable).toEqual(expect.arrayContaining(["$count", "$ok"]));
|
||||
expect(marks.number).toEqual(expect.arrayContaining(["10", "4.5"]));
|
||||
expect(marks.string).toContain('"otter"');
|
||||
expect(marks.function).toEqual(expect.arrayContaining(["total", "strlen"]));
|
||||
expect(marks.constant).toContain("true");
|
||||
expect(marks.operator).toEqual(expect.arrayContaining(["*", "+", "="]));
|
||||
});
|
||||
|
||||
it("marks the HTML around PHP blocks", () => {
|
||||
const marks = highlighted(show("view.php", '<div class="box"><?= $name ?></div>\n'));
|
||||
expect(marks.tag).toEqual(expect.arrayContaining(["div"]));
|
||||
expect(marks.attribute).toContain("class");
|
||||
expect(marks.variable).toContain("$name");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["composer.json", '{"name": "lutra/demo", "n": 1}', "string", '"lutra/demo"'],
|
||||
["ci.yml", "on: push # build\n", "comment", "# build"],
|
||||
["app.ts", "const x: number = 1;\n", "keyword", "const"],
|
||||
["app.css", "a { color: red; }\n", "tag", "a"],
|
||||
["README.md", "# Lutra\n", "keyword", " Lutra"],
|
||||
])("highlights %s", (path, doc, token, text) => {
|
||||
expect(highlighted(show(path, doc))[token]).toContain(text);
|
||||
});
|
||||
|
||||
it("leaves plain text unmarked", () => {
|
||||
expect(highlighted(show("notes.txt", "class Foo { $x = 1; }"))).toEqual({});
|
||||
});
|
||||
|
||||
it("colours every highlight class with its theme token", () => {
|
||||
const view = new EditorView({ state: EditorState.create({ extensions: lutraEditorTheme }), parent: document.body });
|
||||
views.push(view);
|
||||
const css = [...document.querySelectorAll("style")].map((style) => style.textContent).join("\n");
|
||||
for (const token of SYNTAX_TOKENS) {
|
||||
expect(css).toMatch(new RegExp(`\\.lt-syntax-${token}\\s*\\{\\s*color:\\s*var\\(--lt-syntax-${token}\\)`));
|
||||
}
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue