Rebuild diff rendering: structured model, syntax highlighting, word-level marking
## What that costs, concretely
From `/r/pikchr/checkin/a7f1c35bc044.../diff`:
- /* Remove leading and trailing whitespace from each argument. + /* Remove leading and trailing whitespace from each argument (including
Only `(including` was added. The reader gets two fully-coloured lines and has to diff it by eye. Same hunk, a few lines down, a `while` condition gains one set of parentheses and reads as a wholesale replacement.
The specific gaps:
- **No syntax highlighting.** chroma is vendored and already highlights the file page (internal/api/register.go:1837). The diff page never calls it, so every diff is monochrome regardless of language. - **No intra-line diff** at word or character granularity — the case above. - **No line numbers.** You cannot say "look at 5223". - **`+`/`-` are in the text, not a gutter**, so copying a hunk copies the markers. - **Raw unified-diff chrome** (`--- a/pikchr.y`, `+++ b/pikchr.y`) instead of a file header. - **No context expansion**, no per-file navigation for a multi-file changeset, no unified/split choice.
## This is an API problem, not only a template problem
`checkin.diff` (internal/api/register.go:1754) returns:
type diffGetOut struct { From, To string Files []zitcli.DiffEntry Unified string // <- the entire diff, as one opaque blob Note string }
Principle 5 says a feature is not done until its full capability is in the API, and that the HTML and the API share one query layer. A structured diff has to land in the **op**, or we build a good page on top of a string and the API stays screen-scrape-only. `Unified` should remain (it is what a script wants) but stop being the only shape.
## Proposed model
Build it in Go. The factoring below is adapted from @pierre/diffs' public types (Apache-2.0) — worth borrowing because it survived a second, wildly different renderer (see references), which is the real evidence that the model is separable from the markup:
type FileDiff struct { Path, PrevPath string Change ChangeKind // change | new | deleted | rename-pure | rename-changed Binary bool Hunks []Hunk } type Hunk struct { OldStart, OldLines, NewStart, NewLines int Header string // the text after @@ … @@ Lines []Line } type Line struct { Kind LineKind // context | addition | deletion | expanded | metadata OldNum, NewNum int // 0 when the side has no number Tokens []Token // syntax tokens, positional Spans []Span // intra-line changed ranges, in byte offsets } type Token struct { Col int; Class string; Text string }
Two details carried over deliberately:
1. **A token is positional — `(column, class, text)` — not nested markup.** This is what makes the whole thing tractable, and it is the one idea worth taking above all others. 2. **Rename is split into `rename-pure` and `rename-changed`.** A pure rename should not render a diff body at all.
## The hard part
Syntax highlighting and intra-line diff both want to emit spans over the same text, and they do not nest: a changed range routinely starts and ends mid-token. Pierre resolves this by keeping highlight output positional and splicing at render time; that is why hunk can drive the same model into a terminal grid.
We can do the same directly, because chroma hands us tokens rather than only HTML (internal/highlight/highlight.go:41):
it, err := lexer.Tokenise(nil, string(code))
That iterator yields `chroma.Token{Type, Value}`. The pipeline:
1. Parse the unified diff into files/hunks/lines (replacing `classifyDiff`). 2. Pair each deletion run with its addition run; compute a word-level diff over each pair to get changed byte ranges. 3. Tokenise each line with chroma, tracking byte offsets. 4. Split tokens at diff-range boundaries so every emitted span carries exactly one syntax class and one change state. 5. Render through the existing sanitizer gate, as `highlight.Code` already does.
Step 2 needs a word-level diff. Either a small dependency or a Myers pass over word tokens — decide when we get there, not now.
## Scope
In:
- structured diff in the op, `Unified` retained - syntax highlighting via chroma, reusing the existing class-based stylesheet - word-level intra-line marking - line numbers, and `+`/`-` moved to a gutter so selection copies clean code - proper file headers with the change kind, and per-file navigation - context expansion
Out, deliberately:
- **Line-anchored review comments.** 0002 §1 makes this an explicit divergence: conversations anchor to artifacts, and the UI says "discuss this check-in", not "review". Pierre models annotations; we are not taking them. - **Merge-conflict rendering.** Pierre models conflict marker rows. Not a forge-display concern for us. - **Structural/AST diffing.** hunk's own comparison notes only difftastic does this. Out of scope, and worth a separate ticket if ever wanted. - **Adopting @pierre/diffs or hunkdiff as dependencies.** Measured: @pierre/diffs has no dependency-free entry point — even `./ssr` is 115 files / 492 KB and needs Shiki, a WASM-Oniguruma highlighter with TextMate grammars. That means Node in the build plus a second highlighter, to get what we can build on the one already in the binary. hunkdiff is 19.6 MB and targets a terminal via OpenTUI.
## References
- @pierre/diffs — Apache-2.0, v1.3.5, https://diffs.com/ — the reference implementation and the source of the model factoring above. Reading its `dist/types.d.ts` is the fastest way into the design. - hunk / hunkdiff — MIT, v0.19.0, https://hunk.dev/, https://github.com/modem-dev/hunk — a terminal diff viewer that pins `@pierre/[email protected]` and renders it via OpenTUI. Useful as proof the model is render-target-agnostic, and its feature list (split/stack responsive layout, multi-file review stream, sidebar navigation) is a good requirements checklist.
Licensing note: both are permissive and compatible. If we adapt anything concrete from either — type factoring, CSS metrics, layout values — it needs a `third_party/PROVENANCE.md` row, the way fossil's `default.css` metrics already carry one. No code should cross; this is the same [MAP]-not-[COPY] relationship 0002 §1 defines.
Built (cc6091b1), on the model this ticket specified.
internal/diff — the Pierre-factored model in Go: FileDiff (change kind: change/new/deleted, /dev/null recognised), Hunk (starts + header), Line (kind + old/new numbers), and POSITIONAL tokens — one syntax class and one change state per span, the one idea worth taking above all others. Intra-line marking is an LCS over word tokens (letters/digits vs punctuation split separately, so a paren edit marks the paren, not the word beside it), with the rewrite guard: lines sharing under a third of their words mark NOTHING — a fully-marked pair says less than plain colour. The chroma splice tracks byte offsets and splits tokens at mark boundaries; a reassembly test pins that the splice never loses or duplicates a byte.
The op — checkin.diff gains structured (files/hunks/lines/
tokens); unified remains for scripts. Principle 5 held: the page
renders the same shape the API serves.
The page — per-file sections with change-kind headers and the file list doubling as anchor navigation; hunk headers with the @@ context; line numbers and the +/- marker in user-select:none gutters so copying a hunk copies clean code; syntax classes riding the chroma stylesheet already on every page; marks as tinted ranges inside add/del rows.
Verified on the ticket's own case — pikchr a7f1c35b: (including
is a marked span, everything else on that line is not; 13 marked
ranges across the changeset; gutters number from 5220. A Go diff
carries full syntax (1.4k identifier spans, keywords, strings).
pikchr.y itself gets structure and marks but plain tokens — chroma has
no yacc lexer; degrade-to-plain is the designed behavior.
Remainders, honestly: context expansion (needs a new op serving file lines at a revision plus an expand affordance) and a unified/ split toggle — both listed in the original gaps, neither in this pass. Line-level chroma also cannot see multi-line constructs (a string spanning lines tokenises per-line); the price of not fetching whole files, degrades to plain.
Shipped. The diff is a structured model now: per-line number, mark and code cells, chroma syntax highlighting, and word-level marking inside a changed line. Verified live on /r/pikchr/checkin/a7f1c35bc044/diff.