Skip to content
quarry Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ff7ec3266c8a5e4c636860c8f0a87d150c980c45c4a3c0a62e1f6e7998d7ea08
Ticket: e16ad1d395752c1eb2ced0b324e096ffee618218
Rebuild diff rendering: structured model, syntax highlighting, word-level marking
User & Date: dmestas on 2026-08-17 19:35:29
Changes

  1. comment changed to:
    Diffs are the page a forge lives or dies by, and ours is the weakest thing we render. The whole renderer is `classifyDiff` (internal/web/web.go:1217): twenty lines that prefix-match each line of a unified diff and tag it `add`/`del`/`hunk`/`head`/`ctx`. Nothing else happens.
    
    ## 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.
    
    
  2. status changed to: "Open"
  3. title changed to:
    Rebuild diff rendering: structured model, syntax highlighting, word-level marking