From 6c75c08d70151b0b171881ff1038e1f0cc8906bb Mon Sep 17 00:00:00 2001 From: danny Date: Sat, 17 Oct 2026 12:06:42 +0200 Subject: [PATCH] M2.2: PHP highlighting, syntax colour tokens and editor.reveal Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- ROADMAP.md | 8 +- crates/engine/src/theme.rs | 54 ++++++++ docs/architecture.md | 21 ++- docs/gaps.md | 5 +- docs/roadmap.md | 5 +- docs/target-app-parity.md | 5 +- docs/ui-parity.md | 5 +- ui/package.json | 9 ++ ui/pnpm-lock.yaml | 173 ++++++++++++++++++++++++ ui/src/commands/uiCommands.ts | 1 + ui/src/components/editor/EditorArea.vue | 5 +- ui/src/components/editor/engineText.ts | 22 +++ ui/src/components/editor/extensions.ts | 36 ++++- ui/src/components/editor/language.ts | 167 +++++++++++++++++++++++ ui/src/components/editor/reveal.ts | 88 ++++++++++++ ui/src/i18n/en.json | 3 + ui/src/stores/editor.ts | 19 +-- ui/tests/editor/EditorArea.test.ts | 106 ++++++++++++++- ui/tests/editor/engineText.test.ts | 25 +++- ui/tests/editor/highlightPerf.test.ts | 111 +++++++++++++++ ui/tests/editor/language.test.ts | 149 ++++++++++++++++++++ ui/tests/palette/CommandPalette.test.ts | 1 + ui/tests/theme/contrast.test.ts | 35 ++++- ui/tests/theme/themes.json | 22 +++ 25 files changed, 1044 insertions(+), 33 deletions(-) create mode 100644 ui/src/components/editor/language.ts create mode 100644 ui/src/components/editor/reveal.ts create mode 100644 ui/tests/editor/highlightPerf.test.ts create mode 100644 ui/tests/editor/language.test.ts diff --git a/README.md b/README.md index 28ee92b..0595253 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ claude mcp add lutra -- lutra mcp ## Status -**Pre-alpha (2026-10-16): M1 done.** You can open a folder, browse it in the project tree with live updates from disk, and edit and save files in tabs (plain text for now), with every command in a palette (`Mod+Shift+P`) and settings and the light or dark theme in a settings dialog (`Mod+,`); unsaved text survives a crash and is offered back on the next open; syntax highlighting and PHP features come next. With M1 done, about 13 agent hours remain to the MVP (M2 and M3, estimated after calibrating on the first cycle). [**ROADMAP.md**](ROADMAP.md) has the stage and what's next, +**Pre-alpha (2026-10-16): M1 done.** You can open a folder, browse it in the project tree with live updates from disk, and edit and save files in tabs with syntax highlighting for PHP (and HTML, CSS, JS/TS, JSON, YAML, Markdown), with every command in a palette (`Mod+Shift+P`) and settings and the light or dark theme in a settings dialog (`Mod+,`); unsaved text survives a crash and is offered back on the next open; find in files, go to file/class/symbol and PHP language features come next. With M1 done, about 13 agent hours remain to the MVP (M2 and M3, estimated after calibrating on the first cycle). [**ROADMAP.md**](ROADMAP.md) has the stage and what's next, [`docs/target-app-parity.md`](docs/target-app-parity.md) the parity assessment and [`docs/gaps.md`](docs/gaps.md) the ranked work list. **First target (MVP, M1–M3):** open a project, project tree, tabs, editor with highlighting, find in files, diff --git a/ROADMAP.md b/ROADMAP.md index 674b4ce..5a7a1af 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: pre-alpha (M1 done)** · next: M2 syntax and search; alpha when the six core workflows work end to end (see [docs/roadmap.md › Alpha gate](docs/roadmap.md#alpha-gate)) -> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-16 · **Change:** minor (M1.9b: settings dialog; M1 done) · **Target:** JetBrains PhpStorm 2026.x, described from public documentation only +> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-16 · **Change:** minor (M2.2: syntax highlighting, `editor.reveal`) · **Target:** JetBrains PhpStorm 2026.x, described from public documentation only Lutra is a clean-room, open-source PHP IDE with a Rust core and a Tauri + Vue interface, targeting the PhpStorm workflow. Agents can drive all of it over MCP, the `lutra` CLI and a JSON control channel. This page is the summary; the evidence is in [`docs/target-app-parity.md`](docs/target-app-parity.md) and the work list in [`docs/gaps.md`](docs/gaps.md). @@ -13,7 +13,7 @@ Lutra is a clean-room, open-source PHP IDE with a Rust core and a Tauri + Vue in | | Value | Kind | |---|---|---| | **Feature breadth** (PhpStorm's actions, tool windows, languages, integrations exist) | **<1%** (project tool window, editor tabs; no inventory to count against yet) | estimated | -| **Feature depth** (weighted by use, scored by behaviour) | **~1.6%** (Editor core 9% × weight 12, Project view 8% × weight 6, Settings 7% × weight 1, of 101) | estimated | +| **Feature depth** (weighted by use, scored by behaviour) | **~1.7%** (Editor core 10% × weight 12, Project view 8% × weight 6, Settings 7% × weight 1, of 101) | estimated | | **To MVP (M3)** | **~13 h left** (M1 done in ~5.6 h measured: C1 2.5 h, C2 3.1 h; M2 ~5 h, M3 ~8 h); was ~190 h before calibration | estimated (×0.1; C1 factor 0.07, C2 factor 0.10) | | **To full parity** | **~330 h** if the speed of cycles C1 and C2 holds (range 300–3,300 h; deep areas such as type inference and inspections have no data yet) | estimated | @@ -41,7 +41,7 @@ Stability 8%: no-panic lints, the engine guard, a fuzzed control protocol and a | Area | % | One agent (h) | |---|---:|---:| -| Editor core (buffers, undo, multi-caret, folding, soft wrap) | 9% (est.) | ~106 | +| Editor core (buffers, undo, multi-caret, folding, soft wrap) | 10% (est.) | ~104 | | Project view, VFS, file watching | 8% (est.) | ~40 | | Navigation (go to class/file/symbol, recent files, structure) | 0% | ~120 | | Search and replace (in file, in project, structural) | 0% | ~80 | @@ -76,6 +76,7 @@ All hours estimated. Weights, evidence and the missing items per area: [target-a | Date | What landed | |---|---| +| 2026-10-16 | M2.2: PHP files are highlighted (with the HTML around ``) every theme defines. The +/// editor's highlighter reads only these; each reaches 4.5:1 on the editor +/// background (`ui/tests/theme/contrast.test.ts`). The colours are Lutra's +/// own, derived from the theme's copper accent and warm neutrals. +pub const SYNTAX_TOKENS: &[&str] = &[ + "keyword", + "string", + "number", + "comment", + "variable", + "function", + "type", + "constant", + "tag", + "attribute", + "operator", +]; + /// The default dark theme. pub const DARK: Theme = Theme { id: "lutra-dark", @@ -68,6 +86,17 @@ pub const DARK: Theme = Theme { ("tab-active-bg", "#191817"), ("tab-modified", "#d39a5c"), ("notice-bg", "#3a3025"), + ("syntax-keyword", "#d39a5c"), + ("syntax-string", "#a9bd7e"), + ("syntax-number", "#e3a1a8"), + ("syntax-comment", "#958d84"), + ("syntax-variable", "#d8bba6"), + ("syntax-function", "#e6c46f"), + ("syntax-type", "#7fbac6"), + ("syntax-constant", "#c4a0d6"), + ("syntax-tag", "#e08b70"), + ("syntax-attribute", "#cdb87e"), + ("syntax-operator", "#b8afa6"), ], }; @@ -95,6 +124,17 @@ pub const LIGHT: Theme = Theme { ("tab-active-bg", "#fbfaf8"), ("tab-modified", "#965a22"), ("notice-bg", "#e9dfd4"), + ("syntax-keyword", "#965a22"), + ("syntax-string", "#4e6a24"), + ("syntax-number", "#a03a4e"), + ("syntax-comment", "#6e665e"), + ("syntax-variable", "#7f4a34"), + ("syntax-function", "#7a5808"), + ("syntax-type", "#24697a"), + ("syntax-constant", "#73479a"), + ("syntax-tag", "#a5432a"), + ("syntax-attribute", "#695d1a"), + ("syntax-operator", "#5a5149"), ], }; @@ -226,6 +266,20 @@ mod tests { } } + #[test] + fn themes_define_the_syntax_tokens() { + for theme in BUILTIN { + let tokens = token_map(theme); + for name in SYNTAX_TOKENS { + assert!( + tokens.contains_key(&format!("--lt-syntax-{name}")), + "{} lacks syntax-{name}", + theme.id + ); + } + } + } + #[test] fn resolves_settings() { assert_eq!(resolve("lutra-light", Some(true)), LIGHT); diff --git a/docs/architecture.md b/docs/architecture.md index ed739c7..b118149 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Status:** M0 decisions recorded, M1 workspace, desktop bridge, tabs and editor, message catalog, engine construction, settings, themes, the recovery journal, notifications, UI commands, the palette, the settings dialog, the app icon and the syntax layer documented, plus the search backend · **Last updated:** 2026-10-16 +> **Status:** M0 decisions recorded, M1 workspace, desktop bridge, tabs and editor, message catalog, engine construction, settings, themes, the recovery journal, notifications, UI commands, the palette, the settings dialog, the app icon and the syntax layer documented; M2 highlighting and `editor.reveal`, plus the search backend · **Last updated:** 2026-10-16 ## Overview @@ -102,6 +102,20 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat - Closing a dirty tab asks Save / Don't Save / Cancel; `buffer.close` without `force` failing with `unsaved_changes` also leads to the question. Lutra saves only on request for now (no autosave). - Theme: the editor reads colours only from `--lt-*` tokens, including the editor tokens from M1.10 (selection, active line, cursor, gutter, match, editor font size); no fallbacks remain. +### Highlighting and editor.reveal (M2.2) + +- **Languages:** `ui/src/components/editor/language.ts` picks a language once per tab from the file extension (`languageOf`): PHP (`php`, `phtml`, `inc`, `php3`–`php8`, `phps`) through `@codemirror/lang-php`, whose Lezer grammar also parses the HTML, CSS and JavaScript around `` blocks; HTML, CSS, JavaScript/TypeScript (with JSX), JSON, YAML and Markdown through their `@codemirror/lang-*` packages (all MIT). Other files stay plain text. The editor store passes the tab's path to `editorExtensions(path)` when it creates the tab's state. +- **Colours:** the highlighter is a `tagHighlighter` that only sets classes: a span gets `lt-syntax-` for one of the eleven tokens `keyword`, `string`, `number`, `comment`, `variable`, `function`, `type`, `constant`, `tag`, `attribute`, `operator`. The editor theme (`extensions.ts`) colours each class with its `--lt-syntax-` variable. Lezer tags that are not mapped fall back to their parent tag (`controlKeyword` → `keyword`); tags without a mapped ancestor (punctuation, plain names such as the class after `extends`) keep `--lt-fg`. CodeMirror's default highlight style is never active, so no colour bypasses the tokens. +- **Tokens:** `lutra_engine::theme::SYNTAX_TOKENS` lists the names; both themes define `syntax-*` values. It is Lutra's own palette, derived from the copper accent and the warm neutrals (keyword = accent, comment near `fg-muted`, operator = `statusbar-fg`). The contrast test checks each one at 4.5:1 on `editor-bg` and on `editor-active-line-bg` in both themes, and that the themes define no syntax token it does not check. Lowest ratios: dark comment 5.42 on the editor background and 4.86 on the active line; light keyword 5.32 and 4.84. Text on the selection colour is not held to 4.5:1 (syntax colours reach 3.06–5.96 there). +- **Two highlighters:** Lezer in the UI highlights on every keystroke without IPC; tree-sitter in `crates/syntax` (M2.1) drives outline, folding and the index. They can disagree on broken code; that is accepted (see Decisions). +- **Performance:** keystroke to highlight on a 1 MB PHP file (30k lines), measured with `LUTRA_PERF=1 pnpm vitest run tests/editor/highlightPerf.test.ts` in jsdom (the JavaScript work of one `view.dispatch`, no layout or paint), Windows 11, three runs while other builds ran: median 3.0–3.8 ms near the top of the file and 6.3–11.4 ms in the middle; p95 10–16 ms in two runs (one disturbed run: 82 ms). The plain-text baseline is 0.4–0.6 ms. The initial full parse takes 0.7–1.5 s, which CodeMirror spreads over idle slices after the visible part. Not measured in the WebView itself yet (follow-up for `lutra perf`). +- **`editor.reveal {path, line, column}`** (`reveal.ts`; a UI command registered through `ui.registerCommands`, label `command.editor.reveal`, provided by `EditorArea.vue`): + - **Steps:** it opens the file with `file.open` unless a tab has that exact path, activates the tab and waits one render tick so the view shows it. Then it places the cursor and scrolls it to the centre (`EditorView.scrollIntoView(pos, {y: "center"})`). + - **Positions:** `line` is 0-based and `column` counts UTF-16 code units in the line (C3 contract). `positionOf` in `engineText.ts` converts them to a CodeMirror position. Engine and CodeMirror split lines at the same breaks and a `\r\n` is one break on both sides, so the line carries over, and `EngineText.toEngine` maps the result back to the engine offset. Values are clamped into the document: past the end → end of line or last line; negative, fractional or non-finite → truncated or 0. + - **Without `path`** it centres the active tab's cursor, which is what running it from the palette does. + - **Errors:** wrong types fail with `editor.reveal.invalidParams`, an open failure with the engine's message; both reach the caller as an error toast. + - **Agents:** `engine.execute editor.reveal {...}` over the control channel becomes a `uiCommand`. No default shortcut. + ### Message catalog (M1.11) - Every interface string comes from `ui/src/i18n/en.json`: flat dotted keys grouped by area (`app.*`, `project.*`, `editor.*`, `common.*`), kept sorted. Components and stores call `t(key, params?)` from `ui/src/i18n`; `{name}` placeholders are filled from `params`, a placeholder without a value stays as it is. A missing key returns the key itself and warns on the console in development builds; it never throws. `hasMessage(key)` tells whether a key exists, for keys that arrive at runtime (settings `labelKey`, notification `messageKey`). @@ -162,14 +176,14 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat - **Listing:** `listCommands` returns one list in id order: engine commands and UI commands. `CommandInfo` has `ui: bool`; UI commands also carry `labelKey`, use it as `label` (the engine has no English for them) and are always `enabled` as far as the engine knows. - **Running from outside:** `Engine::execute` on a UI command id queues `uiCommand {id, params}` and returns `null` (params are passed on unchanged). The desktop publishes the event to the window and to control subscribers like any other. The frontend runs the command, so `engine.execute palette.open` over the control channel opens the palette. A `uiCommand` the frontend cannot run (no handler, or disabled) becomes a warning toast. - **Frontend:** `ui/src/commands/`: - - `uiCommands.ts` lists the UI commands: `palette.open` `Mod+Shift+P`, `settings.open` `Mod+,`, `view.toggleProjectTree` `Mod+1`. + - `uiCommands.ts` lists the UI commands: `editor.reveal` (no shortcut, M2.2), `palette.open` `Mod+Shift+P`, `settings.open` `Mod+,`, `view.toggleProjectTree` `Mod+1`. - `keymap.ts` parses Lutra notation and matches by key code (letters, digits and US punctuation by `code`, so Alt and Shift layout effects don't matter). It also formats shortcuts for display with catalog modifier names. - `rank.ts` ranks for the palette. - `store.ts` (Pinia `commands`) holds the merged list, the keymap, the handlers and the recent commands. - **Handlers:** components supply behaviour with `provide(id, {run, enabled?})`: - the palette provides `palette.open`; - the main layout provides `view.toggleProjectTree`; - - the editor area provides `file.save`, `file.saveAll` and `buffer.close` with the active buffer as context (they replace the editor-only keymap of M1.6). + - the editor area provides `file.save`, `file.saveAll` and `buffer.close` with the active buffer as context (they replace the editor-only keymap of M1.6), and `editor.reveal` (M2.2). - A UI command without a handler is disabled. The settings dialog provides `settings.open` (M1.9b). - An engine command without a handler runs through `run(id, {})`. - **Modal rule (M1.15, `ui/src/commands/modals.ts`):** at most one modal is open. The palette, the settings dialog and the close question are exclusive: each reports `opened(id, close)` / `closed(id)` to the Pinia `modals` store, and opening one closes the open one (the close question closes as Cancel; the store follows `editor.closePrompt`, so the editor has no code for it). Running any command but `palette.open` through `execute` (keyboard, palette, `uiCommand`) closes the palette first. The restore dialog is queued instead: hidden while an exclusive modal is open, shown again with its list intact when it closes. Reason: its offer must survive a palette or settings dialog opened by an agent, and closing it would only hide the journal until the next open. @@ -235,6 +249,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat | Date | Change | Summary | |---|---|---| +| 2026-10-16 | minor | M2.2: highlighting by file extension (Lezer), `--lt-syntax-*` tokens and classes, `editor.reveal`, keystroke-to-highlight measurement | | 2026-10-16 | minor | M2.3: find in files backend (`crates/search`, `search.find`/`search.cancel`, `searchResults`/`searchDone` via the pump) | | 2026-10-16 | minor | M2.1: syntax layer (tree-sitter PHP parser, incremental reparse, outline, folding, size cap, timing) | | 2026-10-16 | minor | M1.15: modal rule (one modal at a time, restore dialog queued), `recoveryResolved` event | diff --git a/docs/gaps.md b/docs/gaps.md index b1a0682..e5c0cbc 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Status:** M0 closed G1; G2 under way (text buffer, engine buffers, tabs and editor in the app, recovery journal); G3 M1 part done (project tree, palette, settings dialog, themes); G4 under way (tree-sitter outline and folding library); G14 English UI catalog in place · **Last updated:** 2026-10-16 +> **Status:** M0 closed G1; G2 under way (text buffer, engine buffers, tabs and editor in the app, recovery journal); G3 M1 part done (project tree, palette, settings dialog, themes); G4 under way (tree-sitter outline and folding library, Lezer highlighting); G14 English UI catalog in place · **Last updated:** 2026-10-16 Every known shortfall, ranked. This is the work list: unless the owner gives a task, take the highest open gap. Hours are agent wall-clock hours for one agent, all estimated. @@ -9,7 +9,7 @@ Every known shortfall, ranked. This is the work list: unless the owner gives a t | G1 | ~~Foundation: workspace, quality gate, command registry, guard, control channel~~ closed 2026-10-14 | M0 | done (~2 h measured) | | G2 | Editor core: ~~engine buffers, save~~ (M1.3), ~~editor UI with tabs, undo, multi-caret~~ (M1.6, CodeMirror basics), ~~recovery journal~~ (M1.7), tab management (preview, pin, split, close others, reopen), conflict UI for files changed on disk, autosave option | M1 | ~16 h | | G3 | Project model: ~~VFS~~ (M1.2: listing, ignore rules, native file watching with polling fallback, safe file IO), ~~project tree wiring~~ (M1.5 UI, M1.3 engine, M1.4 bridge: folder picker, live tree), tree toolbar and options, ~~showing truncated listings~~ (M1.13), ~~error notifications for the folder picker~~ (M1.12), ~~tabs~~ (M1.6), ~~command palette~~ (M1.8: commands only, global keymap, UI commands over the control channel); ~~settings~~ (backend: M1.9a; dialog with search: M1.9b), ~~theme switching~~ (M1.10: light/dark, live, system mode; picker in the dialog, M1.9b) | M1 | ~24 h | -| G4 | Syntax and structure: tree-sitter PHP, Lezer highlighting, file structure | M2 | ~15 h | +| G4 | Syntax and structure: tree-sitter PHP, ~~Lezer highlighting~~ (M2.2: PHP with embedded HTML plus HTML, CSS, JS/TS, JSON, YAML, Markdown; `--lt-syntax-*` tokens; `editor.reveal`), file structure, semantic highlighting | M2 | ~12 h | | G5 | Search and navigation: find in files, go to file/class/symbol, first index | M2 | ~35 h | | G6 | Language server: completion, diagnostics, definition, hover, rename via Phpactor | M3 | ~45 h | | G7 | Terminal, `lutra` CLI and MCP server | M3 | ~35 h | @@ -43,6 +43,7 @@ Known smaller items from M0: | Date | Change | Summary | |---|---|---| +| 2026-10-16 | minor | G4: Lezer highlighting and `editor.reveal` (M2.2) | | 2026-10-16 | minor | G5: find-in-files backend (M2.3) | | 2026-10-16 | minor | G4 note: tree-sitter syntax library, outline and folding (M2.1) | | 2026-10-16 | minor | G3: settings dialog, theme picker (M1.9b) | diff --git a/docs/roadmap.md b/docs/roadmap.md index 90f2fc3..1d70400 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,6 +1,6 @@ # Milestones -> **Status:** M0 done, M1 done · **Last updated:** 2026-10-16 +> **Status:** M0 done, M1 done, M2 in progress · **Last updated:** 2026-10-16 Hours are agent wall-clock hours for one agent; M0 and M1 so far are measured, the rest estimated. Since cycle C1 (factor 0.07) M1–M3 are scaled ×0.1; M4–M6 keep their first raw estimates (multiply by ~0.1 for the calibrated value). @@ -8,7 +8,7 @@ Hours are agent wall-clock hours for one agent; M0 and M1 so far are measured, t |---|---|---|---:| | M0 | Foundation: workspace, lints, quality gate, command registry, control channel, empty window | done 2026-10-14 (CI workflow written, not yet run: hosted on Forgejo, runners unknown) | ~2 (measured) | | M1 | Editor and project: open folder, tree, tabs, editor, save, recovery, palette, settings, themes | done 2026-10-16 (M1.1 text, M1.2 VFS, M1.5 tree UI, M1.3 engine workspace, M1.4 desktop bridge, M1.6 tabs and editor, M1.11 message catalog, M1.9a settings backend, M1.10 themes, M1.7 recovery journal, M1.12 notifications, M1.13 C1 follow-ups, M1.8 command palette and keymap, M1.14 app icon, M1.9b settings dialog; C2 live checks pending, gaming mode) | ~5.6 (measured: C1 2.5, C2 3.1) | -| M2 | Syntax and search: highlighting, structure, find in files, go to file/class/symbol | not started | ~5 | +| M2 | Syntax and search: highlighting, structure, find in files, go to file/class/symbol | in progress (C3: M2.2 highlighting and `editor.reveal`) | ~5 | | M3 | LSP, terminal, CLI: completion, diagnostics, go to definition, hover, rename; terminal; `lutra` CLI and MCP — **MVP** | not started | ~8 | | M4 | Git: status, diff, commit, branches, log, blame, conflicts | not started | ~150 | | M5 | Debugger (Xdebug via DAP), PHPUnit/Pest runner, Composer, run configurations | not started | ~300 | @@ -31,6 +31,7 @@ By the raw estimates the alpha gate needs M1–M4 plus cross-platform verificati | Date | Change | Summary | |---|---|---| +| 2026-10-16 | minor | M2 in progress: M2.2 highlighting and `editor.reveal` | | 2026-10-16 | minor | Cycle C2 review: M1 hours measured | | 2026-10-16 | minor | M1.9b settings dialog; M1 done | | 2026-10-16 | minor | M1.8 command palette, global keymap, UI commands | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 703824f..8398ca7 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # Target-app parity -> **Status:** M1 done (text buffer, VFS, engine workspace, project tree, tabs and a plain-text editor, recovery, palette, settings and themes in the app), no PHP feature ships · **Last updated:** 2026-10-16 · **Target:** PhpStorm 2026.x +> **Status:** M1 done (text buffer, VFS, engine workspace, project tree, tabs and a plain-text editor, recovery, palette, settings and themes in the app); M2 started: PHP syntax highlighting · **Last updated:** 2026-10-16 · **Target:** PhpStorm 2026.x ## How this is measured @@ -12,7 +12,7 @@ Hours are the estimated agent wall-clock hours for one agent to reach behaviour | Area | Weight | Score | Missing | Hours (one agent) | Evidence | |---|---:|---:|---|---:|---| -| Editor core | 12 | 9% (est.) | syntax highlighting (M2), code folding, soft wrap and other editor settings, preview tab, pinned/split/reordered tabs, close others/all, reopen closed tab, tab limit, conflict UI for files changed on disk, autosave, local history, encodings other than UTF-8, large-file mode, column selection and multi-caret checked against the docs; recovery journal not yet checked in the running app (gaming mode) | ~106 (est.) | M2.1: `crates/syntax` parses PHP with tree-sitter (incremental reparse from `lutra_text` edits) and computes folding ranges (brace blocks, arrays, comments, heredocs, `use` runs); library only, nothing visible in the editor yet, so the score is unchanged. Tests: outline/folding snapshots for five PHP 8.3 fixtures, proptests (random bytes and token soups never panic, reparse equals a fresh parse, undo round trip). 1 MB parse 364–553 ms, reparse after one keystroke 7–11 ms (release, measured, machine busy). M1.13: opening a file that is already open switches to its tab (`bufferFocused`; engine test, Vitest, control-channel test; live check pending (gaming mode)); files in folders the tree never expanded still reload or report changes on disk, because the parent directory of every open buffer stays watched (engine tests). M1.7: crash-recovery journal (`crates/engine/src/journal.rs`, `cmd/recovery.rs`): dirty buffers are written atomically to `/recovery//` about 1 s after the last edit (at least every 2 s while typing), never per keystroke; removed on save, close and discard; `recoveryAvailable` after `project.open`, `recovery.list/restore/discard`; restored buffers are dirty and keep line ending and BOM; corrupt entries are moved aside with a warning. Restore dialog (`ui/src/components/recovery/`: Restore all / selected / Discard, time, changed-on-disk hint). Tests: engine dropped mid-edit and a new engine restores the exact CRLF + BOM text, corrupt entries, removal paths, a property test of random edit sequences → crash → restore equals the buffer (`crates/engine/tests/recovery.rs`, 10 tests), the same flow headless over the control channel (`crates/control/tests/recovery.rs`), 10 Vitest tests for the dialog. 1 MiB entry write: 8.5 ms median (release, measured). Graded against `plan/phpstorm/04-recovery.md`. M1.6: tabs and a CodeMirror 6 editor in the app (`ui/src/components/editor/`, `ui/src/stores/editor.ts`): open from the project tree or engine events, activate, close by button, middle click or `Mod+W`, dirty dot, Save / Don't Save / Cancel on closing a dirty tab, `Mod+S` / `Mod+Alt+S` from engine command specs, non-blocking notice for a dirty file changed or deleted on disk; local edits go to `buffer.edit` with version-based resync, CRLF files keep `\r\n`; undo/redo, multiple selections, search and bracket matching from CodeMirror's basic setup. Vitest: a burst of 400 keystrokes with stale versions and remote edits loses none, a CRLF burst with deletions converges, store and component tests against a fake engine (`ui/tests/editor/`, 22 tests). Checked in the app on Windows 11 with `--control` and `ui.screenshot`: typing via the UI and via `buffer.edit`, save to disk, close question, middle click, disk-change notice. 1 MB file (16,385 lines): engine `file.open` 80 ms over the control channel (debug build, measured), editor state 1–6 ms and first rendered view 4–11 ms in Chromium (measured), ≈ 90 ms in total (estimated, budget 150 ms). Graded against `plan/phpstorm/03-editor-tabs.md`. `crates/text`: rope buffer, positions, edits (M1.1, unit tests + proptests). M1.3: engine buffers with versions and dirty state, `file.open`, `buffer.get`, `buffer.edit` (UTF-16 change sets, `stale_version`), `file.save`, `file.saveAll`, `buffer.close` with unsaved-changes guard, line ending and BOM kept on save, clean buffers reload on external change; 14 integration tests on temp dirs (`crates/engine/tests/workspace.rs`) | +| Editor core | 12 | 10% (est.) | semantic highlighting and a colour scheme editor, code folding, soft wrap and other editor settings, preview tab, pinned/split/reordered tabs, close others/all, reopen closed tab, tab limit, conflict UI for files changed on disk, autosave, local history, encodings other than UTF-8, large-file mode, column selection and multi-caret checked against the docs; recovery journal not yet checked in the running app (gaming mode) | ~104 (est.) | M2.2: syntax highlighting by file extension (`ui/src/components/editor/language.ts`): PHP with embedded HTML/CSS/JS through `@codemirror/lang-php` (Lezer), plus HTML, CSS, JS/TS, JSON, YAML, Markdown; spans carry `lt-syntax-*` classes coloured only by the eleven `--lt-syntax-*` tokens of both themes (Lutra's own palette, ≥ 4.5:1 on the editor background and active line, contrast test). `editor.reveal {path, line, column}` opens, activates, places the cursor (UTF-16 column, CRLF-safe) and centres it; agents reach it as a UI command. Checked in the app on Windows 11 (bundled UI, control channel): PHP with embedded HTML in dark and light, `editor.reveal` into a closed CRLF file (last line, then line 302 centred). Vitest: language per extension, classes on spans for PHP/HTML and the other languages, reveal on open and closed files, CRLF mapping, clamping, bad params. keystroke to highlight on a 1 MB PHP file 3.0–11.4 ms median (jsdom, JavaScript work only, measured, not yet in the WebView). M2.1: `crates/syntax` parses PHP with tree-sitter (incremental reparse from `lutra_text` edits) and computes folding ranges (brace blocks, arrays, comments, heredocs, `use` runs); library only, nothing visible in the editor yet, so the score is unchanged. Tests: outline/folding snapshots for five PHP 8.3 fixtures, proptests (random bytes and token soups never panic, reparse equals a fresh parse, undo round trip). 1 MB parse 364–553 ms, reparse after one keystroke 7–11 ms (release, measured, machine busy). M1.13: opening a file that is already open switches to its tab (`bufferFocused`; engine test, Vitest, control-channel test; live check pending (gaming mode)); files in folders the tree never expanded still reload or report changes on disk, because the parent directory of every open buffer stays watched (engine tests). M1.7: crash-recovery journal (`crates/engine/src/journal.rs`, `cmd/recovery.rs`): dirty buffers are written atomically to `/recovery//` about 1 s after the last edit (at least every 2 s while typing), never per keystroke; removed on save, close and discard; `recoveryAvailable` after `project.open`, `recovery.list/restore/discard`; restored buffers are dirty and keep line ending and BOM; corrupt entries are moved aside with a warning. Restore dialog (`ui/src/components/recovery/`: Restore all / selected / Discard, time, changed-on-disk hint). Tests: engine dropped mid-edit and a new engine restores the exact CRLF + BOM text, corrupt entries, removal paths, a property test of random edit sequences → crash → restore equals the buffer (`crates/engine/tests/recovery.rs`, 10 tests), the same flow headless over the control channel (`crates/control/tests/recovery.rs`), 10 Vitest tests for the dialog. 1 MiB entry write: 8.5 ms median (release, measured). Graded against `plan/phpstorm/04-recovery.md`. M1.6: tabs and a CodeMirror 6 editor in the app (`ui/src/components/editor/`, `ui/src/stores/editor.ts`): open from the project tree or engine events, activate, close by button, middle click or `Mod+W`, dirty dot, Save / Don't Save / Cancel on closing a dirty tab, `Mod+S` / `Mod+Alt+S` from engine command specs, non-blocking notice for a dirty file changed or deleted on disk; local edits go to `buffer.edit` with version-based resync, CRLF files keep `\r\n`; undo/redo, multiple selections, search and bracket matching from CodeMirror's basic setup. Vitest: a burst of 400 keystrokes with stale versions and remote edits loses none, a CRLF burst with deletions converges, store and component tests against a fake engine (`ui/tests/editor/`, 22 tests). Checked in the app on Windows 11 with `--control` and `ui.screenshot`: typing via the UI and via `buffer.edit`, save to disk, close question, middle click, disk-change notice. 1 MB file (16,385 lines): engine `file.open` 80 ms over the control channel (debug build, measured), editor state 1–6 ms and first rendered view 4–11 ms in Chromium (measured), ≈ 90 ms in total (estimated, budget 150 ms). Graded against `plan/phpstorm/03-editor-tabs.md`. `crates/text`: rope buffer, positions, edits (M1.1, unit tests + proptests). M1.3: engine buffers with versions and dirty state, `file.open`, `buffer.get`, `buffer.edit` (UTF-16 change sets, `stale_version`), `file.save`, `file.saveAll`, `buffer.close` with unsaved-changes guard, line ending and BOM kept on save, clean buffers reload on external change; 14 integration tests on temp dirs (`crates/engine/tests/workspace.rs`) | | Project view, VFS | 6 | 8% (est.) | views list (project / scopes), toolbar (Select Opened File, Expand/Collapse All), sort and display options, show/hide excluded, distinct icons for library and excluded folders, file colours, speed search, preview, file operations | ~37 (est.) | `lutra-vfs` (M1.2): lazy sorted listing, ignore rules (.gitignore, .ignore, info/exclude, global excludes, Lutra defaults), native file watching via `notify` with a polling fallback, capped UTF-8 reads, atomic writes; 34 tests on temp dirs, watcher tests run on both backends (`crates/vfs/tests`). M1.5: project tree UI (lazy listing, keyboard navigation, dimmed ignored entries, event refresh) in `ui/src/components/project/`, Vitest against a mocked engine. M1.3: `project.open`, `project.close`, `project.children` (watches listed directories, capped at 256 native / 32 polling, least recently listed dropped), `treeChanged` events from disk, `events.subscribe` on the control channel with a TCP test (`crates/control/tests/events.rs`). M1.4: the tree runs against the engine in the app: folder picker (`project.openDialog`) and `lutra-desktop --control PORT PATH` open a project, the tree lists it lazily with ignored (`var/` from `.gitignore`) and excluded (`node_modules`) entries dimmed, a file created on disk appears in an expanded folder within ~100 ms; verified with `ui.screenshot` on Windows 11 and Vitest (`ui/tests/project/`, `ui/tests/ipcEvents.test.ts`). M1.13: a directory listed only partly (more than 100,000 entries) ends with a "Listing truncated" note row (Vitest). M1.12: a folder that cannot be opened from the picker or the command line (including `unsaved_changes` when switching projects) and stopped file watching show as toasts instead of stderr (`apps/lutra-desktop/src/host.rs`, `ui/src/stores/notifications.ts`, `ui/tests/notifications/`, 19 Vitest tests; live check pending, gaming mode); score unchanged. Graded against `plan/phpstorm/02-project-view.md`: tool window, folder-first tree, dimmed ignored/excluded, keyboard navigation and live refresh exist; most toolbar, view and display options do not | | Navigation | 10 | 0% | everything (M1.8: a command palette over commands only, no go-to-file/class/symbol yet; score unchanged) | ~120 (est.) | M2.1: file outline in `crates/syntax` (namespaces braced and unbraced, classes, interfaces, traits, enums with cases, functions, methods, properties incl. promoted ones, constants; UTF-16 ranges and short signatures), the base for the Structure window (M2.7) and the symbol index (M2.5); not reachable from the UI yet, score unchanged. M1.8: `ui/src/components/palette/`, `ui/src/commands/` (fuzzy ranking, recent commands, global keymap), Vitest `ui/tests/palette/`, `ui/tests/commands/` | | Search and replace | 6 | 0% (est.) | everything a user sees: Find in Files tool window (M2.4), replace in files, scopes, file masks UI, search history, find in path from the tree, structural search; the backend below has no UI yet, so the score stays 0% | ~76 (est.) | M2.3 backend: `crates/search` (parallel `ignore` walk + `grep-searcher`: literal/regex, case, whole word, include/exclude globs, project ignore rules with `vendor/` searched and `node_modules` skipped, binary and > 8 MiB files skipped, match cap, cancel) and engine `search.find` / `search.cancel` with `searchResults` / `searchDone` events through the pump; dirty buffers are searched in their buffer text. Tests: 9 library tests incl. a property test of random queries and file bytes (`crates/search/tests/search.rs`), 3 preview/column unit tests, 4 engine tests (`crates/engine/tests/search.rs`), a TCP control-channel test (find → results → done, invalid regex, cancel; `crates/control/tests/search.rs`). 20k-file generated tree, release: first result 10–43 ms, full walk 0.66–1.43 s (measured); live over the control channel against the running app: first result 55 ms, done 386 ms on a 5,900-file project (debug build, measured) | @@ -63,6 +63,7 @@ The project tree is the first user-visible feature (Project view 8%, estimated); | Date | Change | Summary | |---|---|---| +| 2026-10-16 | minor | M2.2: Editor core 9% → 10% (est.), syntax highlighting and `editor.reveal` | | 2026-10-16 | minor | M2.3: Search and replace evidence (find-in-files backend, no UI), score unchanged at 0%, hours 80 → 76 | | 2026-10-16 | minor | M2.1: Editor core and Navigation evidence (tree-sitter parse, outline, folding library), scores unchanged | | 2026-10-16 | minor | M1.9b: Settings row 4% → 7% (est.), settings dialog; M1 done | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index aebaa3d..1d2bee1 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity checklist -> **Status:** main window layout (M1.5), project tree running against the engine (M1.4), editor tabs with a CodeMirror editor (M1.6), light and dark themes (M1.10), restore dialog for unsaved changes (M1.7), notification toasts (M1.12), command palette and global keymap (M1.8), settings dialog with search (M1.9b) · **Last updated:** 2026-10-16 +> **Status:** main window layout (M1.5), project tree running against the engine (M1.4), editor tabs with a CodeMirror editor (M1.6), light and dark themes (M1.10), restore dialog for unsaved changes (M1.7), notification toasts (M1.12), command palette and global keymap (M1.8), settings dialog with search (M1.9b), syntax highlighting and `editor.reveal` (M2.2) · **Last updated:** 2026-10-16 - [ ] Main window: tool window stripes, editor area, status bar, navigation bar - M1.5 (2026-10-14): left tool window with header, hide button, collapsed stripe with a show button, resizable by pointer and keyboard; editor area placeholder; status bar. Missing: stripes with several tool windows, bottom/right docking, navigation bar, layout persistence. Not checked in the running app yet (needs M1.4). @@ -9,6 +9,8 @@ - M1.4 (2026-10-15): runs against the real engine in the app: projects open from the folder picker or the command line, the tree updates live when files change on disk; checked with `ui.screenshot` on Windows 11. M1.13 (2026-10-16): a partly listed folder ends with a "Listing truncated" note; opening a file that is already open (tree, control channel) switches to its tab (Vitest; live check pending (gaming mode)). Missing: scroll from source (Select Opened File), expand/collapse all, views list, sort options, speed search, file colours, file operations. Behaviour notes: `plan/phpstorm/02-project-view.md`. - [ ] Editor tabs (pin, split, reorder, close others) - M1.6 (2026-10-15): tab bar above a CodeMirror 6 editor; files open from the project tree (Enter, double click) next to the active tab, or from engine events (control channel, page reload); click and Left/Right activate, the close button, a middle click and `Mod+W` close; a dot marks unsaved changes; closing a dirty tab asks Save / Don't Save / Cancel; `Mod+S` saves, `Mod+Alt+S` saves all (shortcuts read from the engine's command specs); a non-blocking notice when a dirty file changes or disappears on disk. Checked with `ui.screenshot` on Windows 11 and Vitest. Missing: preview tab, pin, split, reorder by drag, close others/all, reopen closed tab, tab limit, tab context menu, recent-files switcher, duplicate-name disambiguation. Behaviour notes: `plan/phpstorm/03-editor-tabs.md`. +- [ ] Editor syntax highlighting (Lutra's own colours, no copied scheme) + - M2.2 (2026-10-16): the language is picked from the file extension when a tab opens: PHP (with the HTML, CSS and JavaScript around ` { commands.provide("file.save", { run: async () => { await editor.save(); }, enabled: hasActive }), commands.provide("file.saveAll", { run: () => editor.saveAll(), enabled: () => editor.tabs.length > 0 }), commands.provide("buffer.close", { run: () => editor.requestClose(), enabled: hasActive }), + commands.provide(REVEAL_COMMAND, { run: (params) => reveal(editor, params) }), ]; }); diff --git a/ui/src/components/editor/engineText.ts b/ui/src/components/editor/engineText.ts index f97627f..108f5b2 100644 --- a/ui/src/components/editor/engineText.ts +++ b/ui/src/components/editor/engineText.ts @@ -212,3 +212,25 @@ export function diffDocs(from: Text, to: Text): ChangeSet { a.length, ); } + +/** + * The CodeMirror position of an engine `{line, column}` position. + * + * Engine lines are split at the same breaks CodeMirror recognises, and a + * `\r\n` counts as one break on both sides, so line numbers carry over + * unchanged; the column counts UTF-16 code units inside the line, which is + * the same in both. A line past the end selects the last line, a column past + * the line's end (into a `\r\n`, say) its end, and negative or fractional + * values are clamped and truncated (non-finite ones count as 0). Converting the result back with + * {@link EngineText.toEngine} yields the engine offset. + * + * @param doc - the CodeMirror document. + * @param line - 0-based line. + * @param column - 0-based column in UTF-16 code units. + * @returns the position in `doc`. + */ +export function positionOf(doc: Text, line: number, column: number): number { + const whole = (n: number): number => (Number.isFinite(n) ? Math.max(Math.trunc(n), 0) : 0); + const target = doc.line(Math.min(whole(line) + 1, doc.lines)); + return target.from + Math.min(whole(column), target.length); +} diff --git a/ui/src/components/editor/extensions.ts b/ui/src/components/editor/extensions.ts index 5d1374f..bdd15b5 100644 --- a/ui/src/components/editor/extensions.ts +++ b/ui/src/components/editor/extensions.ts @@ -1,7 +1,8 @@ /** * CodeMirror extensions of the Lutra editor: the basic setup (line numbers, - * history, selection, brackets, search) without a language (PHP comes in - * M2), and a theme that takes every colour from `--lt-*` tokens, including + * history, selection, brackets, search), the file's language and syntax + * highlighting (`language.ts`), and a theme that takes every colour from + * `--lt-*` tokens, including the syntax tokens (`--lt-syntax-*`) and * the editor tokens (`--lt-editor-selection-bg`, `--lt-editor-active-line-bg`, * `--lt-editor-cursor`, `--lt-editor-gutter-bg`, `--lt-editor-gutter-fg`, * `--lt-editor-match-bg`, `--lt-font-size-editor`). @@ -9,6 +10,25 @@ import { EditorView } from "@codemirror/view"; import type { Extension } from "@codemirror/state"; import { basicSetup } from "codemirror"; +import { languageExtensions, type SYNTAX_CLASS, type SyntaxToken } from "./language"; + +/** + * Colours of the highlight classes (`language.ts`), one `--lt-syntax-*` + * token each, spelled out so the theme tests can check every token name. + */ +const syntaxColours: Record<`.${typeof SYNTAX_CLASS}${SyntaxToken}`, { color: string }> = { + ".lt-syntax-keyword": { color: "var(--lt-syntax-keyword)" }, + ".lt-syntax-string": { color: "var(--lt-syntax-string)" }, + ".lt-syntax-number": { color: "var(--lt-syntax-number)" }, + ".lt-syntax-comment": { color: "var(--lt-syntax-comment)" }, + ".lt-syntax-variable": { color: "var(--lt-syntax-variable)" }, + ".lt-syntax-function": { color: "var(--lt-syntax-function)" }, + ".lt-syntax-type": { color: "var(--lt-syntax-type)" }, + ".lt-syntax-constant": { color: "var(--lt-syntax-constant)" }, + ".lt-syntax-tag": { color: "var(--lt-syntax-tag)" }, + ".lt-syntax-attribute": { color: "var(--lt-syntax-attribute)" }, + ".lt-syntax-operator": { color: "var(--lt-syntax-operator)" }, +}; /** Editor theme built from theme tokens only. */ export const lutraEditorTheme: Extension = EditorView.theme({ @@ -80,9 +100,15 @@ export const lutraEditorTheme: Extension = EditorView.theme({ color: "var(--lt-fg)", border: "1px solid var(--lt-border)", }, + ...syntaxColours, }); -/** Every extension of an editor state. */ -export function editorExtensions(): Extension { - return [basicSetup, lutraEditorTheme]; +/** + * Every extension of an editor state. + * + * @param path - the file's path, which picks the language; plain text when + * omitted or unknown. + */ +export function editorExtensions(path = ""): Extension { + return [basicSetup, lutraEditorTheme, languageExtensions(path)]; } diff --git a/ui/src/components/editor/language.ts b/ui/src/components/editor/language.ts new file mode 100644 index 0000000..028a68f --- /dev/null +++ b/ui/src/components/editor/language.ts @@ -0,0 +1,167 @@ +/** + * Language support of an editor tab, picked once from the file extension + * when the tab opens. + * + * Every parser is a Lezer grammar from the `@codemirror/lang-*` packages + * (MIT). PHP files use `@codemirror/lang-php`, which also parses the HTML + * (with its CSS and JavaScript) around `` blocks. Unknown + * extensions stay plain text. The colours come from {@link lutraHighlighting}. + */ +import type { Extension } from "@codemirror/state"; +import { syntaxHighlighting } from "@codemirror/language"; +import { tagHighlighter, tags } from "@lezer/highlight"; +import { css } from "@codemirror/lang-css"; +import { html } from "@codemirror/lang-html"; +import { javascript } from "@codemirror/lang-javascript"; +import { json } from "@codemirror/lang-json"; +import { markdown } from "@codemirror/lang-markdown"; +import { php } from "@codemirror/lang-php"; +import { yaml } from "@codemirror/lang-yaml"; + +/** Languages the editor knows. */ +export type LanguageId = "php" | "html" | "css" | "javascript" | "typescript" | "json" | "yaml" | "markdown"; + +/** Language by lower-case file extension (without the dot). */ +const BY_EXTENSION: Readonly> = { + php: "php", + phtml: "php", + php3: "php", + php4: "php", + php5: "php", + php7: "php", + php8: "php", + phps: "php", + inc: "php", + html: "html", + htm: "html", + css: "css", + js: "javascript", + mjs: "javascript", + cjs: "javascript", + jsx: "javascript", + ts: "typescript", + mts: "typescript", + cts: "typescript", + tsx: "typescript", + json: "json", + yaml: "yaml", + yml: "yaml", + md: "markdown", + markdown: "markdown", +}; + +/** + * The language of a file. + * + * @param path - the file's path; only the extension after the last `.` of + * the file name counts, case-insensitively. + * @returns the language, or null for plain text. + */ +export function languageOf(path: string): LanguageId | null { + const name = path.slice(Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1); + const dot = name.lastIndexOf("."); + if (dot <= 0) { + return null; + } + return BY_EXTENSION[name.slice(dot + 1).toLowerCase()] ?? null; +} + +/** + * The CodeMirror support of a language. + * + * @param id - the language. + */ +function supportOf(id: LanguageId): Extension { + switch (id) { + case "php": + return php(); + case "html": + return html(); + case "css": + return css(); + case "javascript": + return javascript({ jsx: true }); + case "typescript": + return javascript({ jsx: true, typescript: true }); + case "json": + return json(); + case "yaml": + return yaml(); + case "markdown": + return markdown(); + } +} + +/** + * Highlight class prefix: a highlighted span carries `lt-syntax-`, + * and the editor theme colours it with the token `--lt-syntax-`. + */ +export const SYNTAX_CLASS = "lt-syntax-"; + +/** Syntax token names, as `SYNTAX_TOKENS` in `crates/engine/src/theme.rs`. */ +export const SYNTAX_TOKENS = [ + "keyword", + "string", + "number", + "comment", + "variable", + "function", + "type", + "constant", + "tag", + "attribute", + "operator", +] as const; + +/** A syntax token name. */ +export type SyntaxToken = (typeof SYNTAX_TOKENS)[number]; + +/** + * Lezer highlight tags and the token each one is drawn with. A tag that is + * not listed falls back to its parent (`tags.controlKeyword` to + * `tags.keyword`, `tags.function(tags.variableName)` to + * `tags.variableName`); one without a listed ancestor keeps the text colour. + */ +const TAG_TOKENS: readonly [SyntaxToken, Parameters[0][number]["tag"]][] = [ + ["keyword", [tags.keyword, tags.modifier, tags.self, tags.heading]], + ["string", [tags.string, tags.regexp, tags.character, tags.link, tags.url]], + ["number", [tags.number]], + ["comment", [tags.comment, tags.meta]], + ["variable", [tags.variableName, tags.propertyName, tags.labelName]], + [ + "function", + [ + tags.function(tags.variableName), + tags.function(tags.propertyName), + tags.function(tags.definition(tags.variableName)), + tags.macroName, + ], + ], + ["type", [tags.typeName, tags.className, tags.namespace]], + [ + "constant", + [tags.bool, tags.null, tags.atom, tags.constant(tags.variableName), tags.constant(tags.propertyName), tags.escape], + ], + ["tag", [tags.tagName, tags.angleBracket, tags.processingInstruction]], + ["attribute", [tags.attributeName]], + ["operator", [tags.operator, tags.derefOperator]], +]; + +/** + * Highlighting that marks spans with `lt-syntax-` classes only, so + * every colour comes from a theme token. + */ +export const lutraHighlighting: Extension = syntaxHighlighting( + tagHighlighter(TAG_TOKENS.map(([token, tag]) => ({ tag, class: `${SYNTAX_CLASS}${token}` }))), +); + +/** + * Editor extensions for a file: its language support (if any) and the + * Lutra highlighting. + * + * @param path - the file's path. + */ +export function languageExtensions(path: string): Extension { + const id = languageOf(path); + return id === null ? [] : [supportOf(id), lutraHighlighting]; +} diff --git a/ui/src/components/editor/reveal.ts b/ui/src/components/editor/reveal.ts new file mode 100644 index 0000000..b6ef182 --- /dev/null +++ b/ui/src/components/editor/reveal.ts @@ -0,0 +1,88 @@ +/** + * The UI command `editor.reveal {path, line, column}`: shows a position in + * the editor. It opens the file (`file.open`) unless a tab has it, + * activates the tab, places the cursor and scrolls it to the centre. + * + * Positions follow the C3 contract: `line` is 0-based and `column` counts + * UTF-16 code units inside the line, converted to a CodeMirror position by + * {@link positionOf} (CRLF-safe). Out-of-range values are clamped. Without + * a `path` it centres the cursor of the active tab, which is what running + * it from the palette does. The Find, Go-to and Structure windows and + * agents (`uiCommand`) call it. + */ +import { EditorView } from "@codemirror/view"; +import { nextTick } from "vue"; +import { t } from "../../i18n"; +import type { useEditorStore } from "../../stores/editor"; +import { positionOf } from "./engineText"; + +/** Command id of the reveal command. */ +export const REVEAL_COMMAND = "editor.reveal"; + +/** A position to reveal. */ +export interface RevealTarget { + /** File path as `file.open` takes it; null for the active tab. */ + path: string | null; + /** 0-based line; null keeps the cursor. */ + line: number | null; + /** 0-based column in UTF-16 code units. */ + column: number; +} + +/** The editor store as reveal uses it. */ +type EditorStore = ReturnType; + +/** + * Reads the params of `editor.reveal`. + * + * @param params - untrusted params from the engine, the palette or a caller. + * @returns the target, or null if a field has the wrong type. + */ +export function parseReveal(params: unknown): RevealTarget | null { + const p = typeof params === "object" && params !== null && !Array.isArray(params) ? (params as Record) : {}; + const number = (value: unknown): number | null | undefined => + value === undefined || value === null ? null : typeof value === "number" && Number.isFinite(value) ? value : undefined; + const path = p.path === undefined || p.path === null ? null : typeof p.path === "string" && p.path !== "" ? p.path : undefined; + const line = number(p.line); + const column = number(p.column); + if (path === undefined || line === undefined || column === undefined) { + return null; + } + return { path, line, column: column ?? 0 }; +} + +/** + * Reveals a position. + * + * @param editor - the editor store. + * @param params - the command params, see {@link parseReveal}. + * @throws Error with a catalog message when the params are invalid or the + * file cannot be opened; the command store shows it as an error toast. + */ +export async function reveal(editor: EditorStore, params: unknown): Promise { + const target = parseReveal(params); + if (!target) { + throw new Error(t("editor.reveal.invalidParams")); + } + let id = editor.activeId; + if (target.path !== null) { + const path = target.path; + id = editor.tabs.find((tab) => tab.path === path)?.id ?? (await editor.openFile(path)); + if (id === null) { + throw new Error(editor.lastError?.message ?? t("editor.reveal.openFailed", { path })); + } + editor.activate(id); + await nextTick(); + } + const sync = id === null ? undefined : editor.syncOf(id); + if (!sync) { + return; + } + const state = sync.state; + const pos = target.line === null ? state.selection.main.head : positionOf(state.doc, target.line, target.column); + sync.dispatch({ + selection: { anchor: pos }, + effects: EditorView.scrollIntoView(pos, { y: "center" }), + userEvent: "select.reveal", + }); +} diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json index 019dcbe..1ed6c98 100644 --- a/ui/src/i18n/en.json +++ b/ui/src/i18n/en.json @@ -1,5 +1,6 @@ { "app.statusBar.version": "{name} {version}", + "command.editor.reveal": "Reveal Cursor in Editor", "command.key.alt": "Alt", "command.key.cmd": "Cmd", "command.key.ctrl": "Ctrl", @@ -21,6 +22,8 @@ "editor.closePrompt.question": "Save changes to {name} before closing?", "editor.notice.changedOnDisk": "{name} changed on disk. Your unsaved changes are kept; saving overwrites the file.", "editor.notice.removedOnDisk": "{name} was deleted on disk. Your text is kept; saving writes it again.", + "editor.reveal.invalidParams": "editor.reveal needs a path (string), line and column (numbers)", + "editor.reveal.openFailed": "could not open {path}", "editor.saveFailed": "save failed", "editor.tabs.close": "Close {name}", "editor.tabs.label": "Editor tabs", diff --git a/ui/src/stores/editor.ts b/ui/src/stores/editor.ts index 5f028bb..331b369 100644 --- a/ui/src/stores/editor.ts +++ b/ui/src/stores/editor.ts @@ -107,8 +107,8 @@ export const useEditorStore = defineStore("editor", () => { let timedStart = 0; /** Engine transport for syncs, replaceable in tests. */ let transport: SyncTransport = engineTransport; - /** Extensions of new editor states, replaceable in tests. */ - let extensions: () => Extension = editorExtensions; + /** Extensions of a new editor state for a file path, replaceable in tests. */ + let extensions: (path: string) => Extension = editorExtensions; /** Counter of tab adoptions, so only the newest open activates its tab. */ let openSequence = 0; /** Unsubscribe function of the event subscription. */ @@ -139,9 +139,9 @@ export const useEditorStore = defineStore("editor", () => { * Replaces the engine transport and editor extensions (tests). * * @param next - transport for new syncs. - * @param nextExtensions - extensions for new editor states. + * @param nextExtensions - extensions for new editor states, by file path. */ - function configure(next: SyncTransport, nextExtensions?: () => Extension): void { + function configure(next: SyncTransport, nextExtensions?: (path: string) => Extension): void { transport = next; if (nextExtensions) { extensions = nextExtensions; @@ -160,7 +160,7 @@ export const useEditorStore = defineStore("editor", () => { const started = openStarted.get(info.path); syncs.set( info.id, - new BufferSync(info, extensions(), transport, (error) => { + new BufferSync(info, extensions(info.path), transport, (error) => { const tab = tabOf(info.id); if (tab) { tab.notice = { kind: "error", message: error.message }; @@ -211,22 +211,25 @@ export const useEditorStore = defineStore("editor", () => { * Opens a file in a tab (or activates its tab). * * @param path - project-relative path. + * @returns the buffer id of the tab, or null if the open failed + * ({@link lastError} says why). */ - async function openFile(path: string): Promise { + async function openFile(path: string): Promise { openStarted.set(path, performance.now()); const result = await run(EDITOR_COMMANDS.open, { path }); if (!result.ok) { openStarted.delete(path); lastError.value = result.error; - return; + return null; } if (!isBufferInfo(result.value)) { lastError.value = { code: "ipc", message: t("common.error.unexpectedResult", { command: EDITOR_COMMANDS.open }) }; - return; + return null; } lastError.value = null; openSequence += 1; adopt(result.value); + return result.value.id; } /** diff --git a/ui/tests/editor/EditorArea.test.ts b/ui/tests/editor/EditorArea.test.ts index 23b4877..d50dc9e 100644 --- a/ui/tests/editor/EditorArea.test.ts +++ b/ui/tests/editor/EditorArea.test.ts @@ -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 }[]; 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"] = " { + 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); + }); +}); diff --git a/ui/tests/editor/engineText.test.ts b/ui/tests/editor/engineText.test.ts index 103e8fe..a9bebfd 100644 --- a/ui/tests/editor/engineText.test.ts +++ b/ui/tests/editor/engineText.test.ts @@ -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); + }); +}); diff --git a/ui/tests/editor/highlightPerf.test.ts b/ui/tests/editor/highlightPerf.test.ts new file mode 100644 index 0000000..dbfc556 --- /dev/null +++ b/ui/tests/editor/highlightPerf.test.ts @@ -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 { + 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 = " (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); +}); diff --git a/ui/tests/editor/language.test.ts b/ui/tests/editor/language.test.ts new file mode 100644 index 0000000..72f7a4e --- /dev/null +++ b/ui/tests/editor/language.test.ts @@ -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 { + const out: Record = {}; + 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", + [ + " { + const marks = highlighted(show("view.php", '
\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}\\)`)); + } + }); +}); diff --git a/ui/tests/palette/CommandPalette.test.ts b/ui/tests/palette/CommandPalette.test.ts index 58ab01a..9492532 100644 --- a/ui/tests/palette/CommandPalette.test.ts +++ b/ui/tests/palette/CommandPalette.test.ts @@ -262,6 +262,7 @@ describe("CommandPalette", () => { "palette.open", "project.openDialog", "app.quit", + "editor.reveal", "settings.open", "view.toggleProjectTree", ]); diff --git a/ui/tests/theme/contrast.test.ts b/ui/tests/theme/contrast.test.ts index 0d58bbc..c8b35f4 100644 --- a/ui/tests/theme/contrast.test.ts +++ b/ui/tests/theme/contrast.test.ts @@ -2,7 +2,9 @@ * WCAG 2.1 contrast check of the built-in themes * (https://www.w3.org/TR/WCAG21/#contrast-minimum). Every token pair the * UI draws text with reaches AA: 4.5:1 for body text, 3:1 for non-text - * marks (cursor, modified dot, settings switch knob; WCAG 1.4.11). The themes come from + * marks (cursor, modified dot, settings switch knob; WCAG 1.4.11). Every + * syntax colour (`--lt-syntax-*`) reaches 4.5:1 on the editor background + * and on the active line. The themes come from * `themes.json`, a snapshot that the Rust test `ui_snapshot_is_current` * keeps equal to `lutra_engine::theme`. */ @@ -42,6 +44,27 @@ const PAIRS: [string, string, number][] = [ ["tab-modified", "tab-active-bg", LARGE], ]; +/** Syntax token names (`--lt-syntax-`), as `SYNTAX_TOKENS` in `theme.rs`. */ +export const SYNTAX = [ + "keyword", + "string", + "number", + "comment", + "variable", + "function", + "type", + "constant", + "tag", + "attribute", + "operator", +] as const; + +/** Every syntax token as text on the editor background and the active line. */ +const SYNTAX_PAIRS: [string, string, number][] = SYNTAX.flatMap((name): [string, string, number][] => [ + [`syntax-${name}`, "editor-bg", TEXT], + [`syntax-${name}`, "editor-active-line-bg", TEXT], +]); + /** * Relative luminance of a `#rrggbb` colour (WCAG 2.1 definition). * @@ -90,7 +113,15 @@ describe.each(themes)("theme $id", (theme) => { return value; }; - it.each(PAIRS)("%s on %s reaches %d:1", (fg, bg, min) => { + it("checks every syntax token it defines", () => { + const defined = Object.keys(tokens) + .filter((name) => name.startsWith("--lt-syntax-")) + .map((name) => name.slice("--lt-syntax-".length)) + .sort(); + expect(defined).toEqual([...SYNTAX].sort()); + }); + + it.each([...PAIRS, ...SYNTAX_PAIRS])("%s on %s reaches %d:1", (fg, bg, min) => { expect(contrast(token(fg), token(bg))).toBeGreaterThanOrEqual(min); }); }); diff --git a/ui/tests/theme/themes.json b/ui/tests/theme/themes.json index 2b0fdc2..9ee730c 100644 --- a/ui/tests/theme/themes.json +++ b/ui/tests/theme/themes.json @@ -25,6 +25,17 @@ "--lt-notice-bg": "#3a3025", "--lt-statusbar-bg": "#24211f", "--lt-statusbar-fg": "#b8afa6", + "--lt-syntax-attribute": "#cdb87e", + "--lt-syntax-comment": "#958d84", + "--lt-syntax-constant": "#c4a0d6", + "--lt-syntax-function": "#e6c46f", + "--lt-syntax-keyword": "#d39a5c", + "--lt-syntax-number": "#e3a1a8", + "--lt-syntax-operator": "#b8afa6", + "--lt-syntax-string": "#a9bd7e", + "--lt-syntax-tag": "#e08b70", + "--lt-syntax-type": "#7fbac6", + "--lt-syntax-variable": "#d8bba6", "--lt-tab-active-bg": "#191817", "--lt-tab-modified": "#d39a5c" } @@ -55,6 +66,17 @@ "--lt-notice-bg": "#e9dfd4", "--lt-statusbar-bg": "#ece7e0", "--lt-statusbar-fg": "#5a5149", + "--lt-syntax-attribute": "#695d1a", + "--lt-syntax-comment": "#6e665e", + "--lt-syntax-constant": "#73479a", + "--lt-syntax-function": "#7a5808", + "--lt-syntax-keyword": "#965a22", + "--lt-syntax-number": "#a03a4e", + "--lt-syntax-operator": "#5a5149", + "--lt-syntax-string": "#4e6a24", + "--lt-syntax-tag": "#a5432a", + "--lt-syntax-type": "#24697a", + "--lt-syntax-variable": "#7f4a34", "--lt-tab-active-bg": "#fbfaf8", "--lt-tab-modified": "#965a22" }