Skip to content

ALI-2661: Colour code in Quick View (comments, strings, keywords, numbers) - #68

Merged
michaelaguiar merged 5 commits into
mainfrom
aliasbot/ali-2661-syntax-highlighting
Dec 4, 2026
Merged

michaelaguiar merged 5 commits into
mainfrom
aliasbot/ali-2661-syntax-highlighting

Conversation

@michaelaguiar

@michaelaguiar michaelaguiar commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Fixes ALI-2661.

Quick View showed source files, and code blocks in Markdown, as plain text. This colours comments, strings, keywords and numbers, with a small highlighter of its own and no new crates.

Behaviour

  • Languages: Rust, C/C++, Python, JS/TS, shell, Go, PHP, JSON, TOML, YAML, HTML and CSS, picked by file name, a script's #! line, or a Markdown fence's language. Anything else stays plain, as does text with more than 1% replacement characters.
  • Multi-line state: each line starts from the state the line before left: block comments (nested in Rust), strings that run on, shell and PHP heredocs and nowdocs, and HTML vs PHP.
  • PHP:
    • <?php … ?> and <?= … ?> switch between HTML and PHP, and an HTML attribute string resumes after <?= … ?>;
    • #[ is an attribute, not a comment, and ?> ends a line comment;
    • keywords match in any case.
  • Only colours change. Layout text, offsets, copying, caret mode and the screen-reader text are identical to plain layout (a unit test compares them). Selected text keeps the on-selection colour.

Colours

Four new roles per theme (syntax-comment, -string, -keyword, -number), in design/tokens.json with usage notes. They're derived in crates/core/src/theme.rs from each theme's terminal colours (color8, color2, color5, color3), then moved toward the text colour until they reach 4.5:1 on both base and surface. design/build --check now checks 44 more pairs.

Token Lowest ratio Theme
Comment 4.51:1 aliasOS Amber
String 4.57:1 built-in light
Keyword 4.57:1 Retro 82
Number 4.60:1 built-in light

Colour is decoration only: the text reads the same without it.

Cost

Line-start states are worked out only as lines are drawn (up to 64 past the last drawn line) and remembered, so scrolling and Ctrl+End carry on from the nearest known state. Nothing runs at idle.

10,000-line Rust file (Quick View shows the first 128 KiB, 3,802 lines), page-down scrolling, 5 fresh headless sway runs:

main this branch change
Median paint 0.548 ms 0.578 ms +5.5%
Mean 0.529 ms 0.548 ms +3.6%
p99 0.699 ms 0.710 ms +1.6%

The first End from the top works out every line start once: 0.83 ms on main and 1.77 ms here, a one-off cost. Release binary: +38,128 bytes (+0.42%).

Checks

  • Unit tests pass, with new ones for each language's rules, comments and strings across the visible edge, PHP's HTML switching, heredocs, nowdocs and #[, fences with and without a language, wrapped lines, a cold jump to the end, colouring only near drawn lines, and the selection colour.
  • Clippy: 0 warnings. design/build --check passes.
  • e2e: 130 pass. The new code_is_coloured_in_light_and_dark_themes reads the screen back and finds all four colours in a Rust file and a Rust fence, in light and aliasOS dark.
  • Review: two rounds, plus fuzzing (500k random inputs across all 12 languages) with no panics or hangs. The fixes cover shell arithmetic and escapes, CRLF, raw <script> and <style> bodies, member names, and a cap on long-line regex scans.
  • Known limits:
    • In nested CSS (a selector with : inside @media {}), an #id can be coloured as a hex value.
    • A regex longer than 1 KiB on one line isn't coloured.
  • Not yet: Mike's try of the dev build (~/.cache/gannet-dev/gannet-syntax), and his call on brighter colours for dark themes. In aliasOS dark, keywords and comments come out as similar greys from the theme's muted palette. A follow-up can switch dark themes to their bright palette entries if he wants.

Quick View now colours comments, strings, keywords and numbers in source
files and in Markdown code blocks whose fence names a language. Code is much
quicker to read at a glance that way.

Languages: Rust, C and C++, Python, JavaScript and TypeScript, shell, Go,
PHP, JSON, TOML, YAML, HTML and CSS. A text file's language comes from its
name, or a script's #! line; a fence's from the first word of its info
string, which the decoder passes across by name and the app reads back only
as a name it knows.

Each family has a small hand-written tokenizer (crates/app/src/syntax.rs)
rather than syntect or tree-sitter, which would bring a regex engine and
grammars, so there are no new crates. A line is tokenized from the state
its line starts in: inside a block comment (Rust's nest), a string that runs
on (Rust's raw strings, Python's and TOML's triple quotes, template
literals, Go's raw strings, PHP's and the shell's quotes), a heredoc (the
shell's <<EOF and <<-EOF, PHP's heredoc and nowdoc), or for PHP, HTML around
the code, including an attribute's string that <?= … ?> sits in. PHP
handles //, # and /* */ comments, #[ attributes (not comments), ?> ending a
line comment, and keywords in any case.

Only lines being drawn are coloured. The layout remembers the state each
source line starts in, worked out on demand to just past the last line
drawn, so scrolling or Ctrl+End goes on from the last one known instead of
from the top. Nothing runs while the window is idle. Text with more than 1%
replacement characters stays plain.

Colouring changes only the colour pieces are drawn in, never the layout's
text or offsets, so selection, caret mode, copying and the screen reader
get the same text as before. Selected text keeps the on-selection colour.

Colours: four new roles per theme (syntax-comment, -string, -keyword,
-number), derived in theme.rs from the theme's own terminal colours (color8,
color2, color5, color3), moved toward the text until each reads at 4.5:1 on
both the window (a text file) and the code block's surface. The token file
lists them for all 11 themes with usage notes, the shipped-themes table
gains the seed colours from aliasOS c3e9f60, and design/build --check holds
the eight new pairs (each role on base and on surface). The theme tests
check the derivation matches the tokens and that every pair passes in any
readable theme.
An e2e check opens a Rust file and a Markdown file with a rust fence in
Quick View, in the built-in light theme and in aliasOS's own, and reads the
screen back (grim's PPM) to see each of comment, string, keyword and number
drawn in its token colour. It also takes shots of a PHP page that mixes HTML
and PHP, with an attribute around <?= ?>, a #[ attribute and a heredoc, for
a person to look at.
@michaelaguiar

Copy link
Copy Markdown
Member Author

Review round 1. The code-review skill ran, plus checks by hand and a standalone fuzz of syntax.rs (200k random inputs in all 12 languages): no panics, no infinite loops, and spans always in range and on character boundaries.

Confirmed fine:

  • the colour state is per layout and resets on a new document or relayout;
  • a jump and then scrolling back is correct;
  • memory is bounded by the number of lines;
  • offsets, copy text, caret mode and AT-SPI text are unchanged;
  • the selection keeps its colour;
  • nothing runs at idle;
  • theme.rs's toward() always terminates.

Being fixed:

  • P2:
    • shell $(( 1<<n )) read as a heredoc;
    • shell \' and \" outside quotes opening strings;
    • CRLF line ends stopping heredocs from closing and breaking YAML keys;
    • a stray < inside inline <script> entering tag state (<script> and <style> bodies become raw text);
    • the selection-colour unit test never running in CI.
  • P3:
    • the e2e PPM header parse;
    • a quadratic regex scan on very long JS lines;
    • YAML apostrophes and C++ digit separators read as strings;
    • keywords after $, ->, . and ::;
    • CSS #id selectors read as colours;
    • Python string prefixes after an adjacent string;
    • a heredoc lost when a string opens later on its line;
    • a README cost claim.

Declined:

  • P3: a user theme whose own foreground is under 4.5:1 gets syntax colours under 4.5:1. That's the same as the existing muted and danger colours.
  • P3: near-identical hues for different tokens in some themes (Retro 82, Osaka Jade, Matte Black). That waits on Mike's call on brighter dark-theme colours.
  • The missing performance numbers: they're in the PR body's Cost section.

Shell: a backslash outside quotes now escapes the next character, so
`it\'s` doesn't open a string, and `<<` inside `(( … ))` or `$(( … ))` is a
shift, not a heredoc.

HTML (and PHP's HTML): a <script> or <style> element's text is raw until
its closing tag, so a `<` or a quote in it starts no tag or attribute.

A heredoc that starts on a line where a string or comment then opens comes
first, as its lines are next; before, the string's state won and the
heredoc was lost.

A trailing \r is ignored, for heredoc labels and YAML keys. Quick View's
decoder already turns CRLF into plain line breaks, so this only guards the
tokenizer itself.

Smaller fixes: YAML quotes start strings only where a value or an item
does (`don't` is a word), C++ digit separators (1'000'000) are numbers,
keywords after `$`, `.`, `->` or `::` are names (PHP's $list and ->class,
JS's obj.type; Rust's .await stays a keyword), and CSS's `#abc` is a colour
only after a `:` inside a `{ }` block, kept across lines, not in a selector.
A JavaScript regex is looked for only up to the line's last `/` and 1 KiB,
so a long minified line of divisions no longer takes quadratic time (about
1 s for 129 KB before).

Splitting a piece into runs (tabs, selection ends, coloured runs) is now
its own function, tested without fonts, so the check that selected code
keeps the on-selection colour runs wherever the tests do. The e2e check
reads the PPM header by position instead of splitting the data, which
could eat pixel bytes that look like spaces. The README no longer says a
long file costs no more than a short one: a jump works out the lines it
skipped once.
@michaelaguiar

Copy link
Copy Markdown
Member Author

Round 1 fixes are in 7fd0338.

  • Shell heredocs vs shifts: << inside (( or $(( is a shift. Declined in part: "label right after <<", because cat << EOF with a space is valid and common shell, and a test keeps it working.
  • Shell backslash outside quotes escapes the next character.
  • CRLF: a trailing \r is dropped per line. Quick View already turns CRLF into \n in doc::excerpt, so this only guards the tokenizer.
  • <script> and <style> bodies are raw text until their closing tag. <?php still switches inside them.
  • Selection colour: run splitting moved into piece_runs, with a font-free test that runs in CI.
  • e2e PPM: the header is parsed by token, and the pixel length is asserted.
  • Long lines: a regex is looked for only up to the line's last / and at most 1 KiB. A 140 KB line test must finish in under 200 ms.
  • Quotes inside words: a YAML quote starts a string only after :, -, [, {, ,, ? or at the line start. C++ digit separators are numbers.
  • Keywords: words after $, ., -> and :: are names (Rust's .await stays a keyword).
  • CSS: #abc is a colour only after : inside a block. Known limit: nested CSS selectors inside @media {}.
  • Python prefixes: not reproduced; a test pins the current, correct behaviour.
  • A heredoc is kept when a string opens later on its line.
  • README cost wording.

Unit tests: 567 + 493 + 5 + 1. Clippy: 0 warnings. design/build --check passes. e2e: 130 pass.

@michaelaguiar

Copy link
Copy Markdown
Member Author

Review round 2 (by hand, narrow check of 7fd0338, plus a 300k-input fuzz across all 12 languages): no P1 or P2, no panics, and every span in bounds and on character boundaries. The line state stays 12 bytes. piece_runs paints the same as the old loop, and the regex cap only drops regexes over 1 KiB.

Being fixed (P3): the member-name rule also caught keywords after .. and ... (Rust 0..self.n, JS ...this), and after -> and :: in Rust, C++ and Python (->Self, ::new).

Declined (P3): a : in a selector inside @media or nested CSS still turns on value mode, and css_value is per line. This is the known limit noted in round 1: rare, and colour only.

The rule that a word after `.`, `->` or `::` is a member name, not a
keyword, was too broad. After `..` or `...` a keyword is a keyword again
(Rust's 0..self.n, JS's ...this, Go's ...interface{}). And `->` and `::`
lead to a member only in PHP (and JS, which has neither), so Rust's -> Self
and Self::new, Python's -> None and C++'s -> int keep their colour.
@michaelaguiar

Copy link
Copy Markdown
Member Author

Round 2's P3 is fixed in 6c07f69. Keywords after .. and ... stay keywords. After -> and ::, a word counts as a member name only in PHP and JS, so -> Self, Self::new and -> None keep their colour. Test: keywords_after_a_range_a_spread_or_a_path_stay_keywords. Unit tests and Clippy pass; the e2e was skipped, since only syntax.rs changed. The review loop stops here, because round 2 had no P1 or P2.

@michaelaguiar
michaelaguiar marked this pull request as ready for review December 4, 2026 16:54
@michaelaguiar
michaelaguiar merged commit ca763c2 into main Dec 4, 2026
3 checks passed
@michaelaguiar
michaelaguiar deleted the aliasbot/ali-2661-syntax-highlighting branch December 4, 2026 17:27
@michaelaguiar michaelaguiar mentioned this pull request Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant