Repository navigation
ALI-2661: Colour code in Quick View (comments, strings, keywords, numbers) - #68
Conversation
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.
|
Review round 1. The code-review skill ran, plus checks by hand and a standalone fuzz of Confirmed fine:
Being fixed:
Declined:
|
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.
|
Round 1 fixes are in 7fd0338.
Unit tests: 567 + 493 + 5 + 1. Clippy: 0 warnings. |
|
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. Being fixed (P3): the member-name rule also caught keywords after Declined (P3): a |
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.
|
Round 2's P3 is fixed in 6c07f69. Keywords after |
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
#!line, or a Markdown fence's language. Anything else stays plain, as does text with more than 1% replacement characters.<?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;Colours
Four new roles per theme (
syntax-comment,-string,-keyword,-number), indesign/tokens.jsonwith usage notes. They're derived incrates/core/src/theme.rsfrom each theme's terminal colours (color8, color2, color5, color3), then moved toward the text colour until they reach 4.5:1 on bothbaseandsurface.design/build --checknow checks 44 more pairs.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:
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
#[, fences with and without a language, wrapped lines, a cold jump to the end, colouring only near drawn lines, and the selection colour.design/build --checkpasses.code_is_coloured_in_light_and_dark_themesreads the screen back and finds all four colours in a Rust file and a Rust fence, in light and aliasOS dark.<script>and<style>bodies, member names, and a cap on long-line regex scans.:inside@media {}), an#idcan be coloured as a hex value.~/.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.