diff #
CodeDiff renders a unified diff of two versions of a source text,
or the same diff side by side with split. Both sides are lexed as whole
documents, so the normal syntax highlighting is preserved and the diff information — row
tints, line-number gutters, intra-line <mark> emphasis — is overlaid on
top. Added and removed rows are semantic <ins>/<del> elements, the markers and gutters stay out of copied text, and unchanged regions collapse
into expandable <details> blocks with no JS. Requires theme_diff.css.
Within a change, a removed line pairs with the added line most similar to it. The pair gets the emphasis and shares a row in the split view. A diff of two static strings is rendered at build time by the preprocessor, described in usage.
<script>
import '@fuzdev/fuz_code/theme_diff.css';
import CodeDiff from '@fuzdev/fuz_code/CodeDiff.svelte';
</script>
<CodeDiff {a} {b} lang="ts" /> export const VERSION = 2;
Edit either version and the diff re-renders:
Prepared models #
For sides that outlive one diff, or a diff computed elsewhere, prepare the pieces: create_diff_side lexes one text once, and create_diff_model pairs two sides with their lines — from diff_lines by default, or supplied, like git's own diff converted to DiffLines. A side serves any number of models, so a consumer caches it by
content and moving the compared range lexes nothing twice. Pass the model as source, which takes any DiffRowSource.
Every row carries data-a and data-b line numbers, absent on the
side it lacks, and data-hunk inside a hunk, for event delegation. The highlight prop puts the diff_highlight class on lines of either
side, and the component's scroll_to_line method brings one into view.
const sides = new Map(); // by blob hash, say
const side = (hash: string, text: string) => {
let s = sides.get(hash);
if (!s) sides.set(hash, (s = create_diff_side(text, {lang: 'ts'})));
return s;
};
const model = create_diff_model(side(a_hash, a), side(b_hash, b)); <CodeDiff bind:this={diff} source={model} highlight={[{side: 'b', start: 4, end: 5}]} /> export const VERSION = 2;
Expanding elided lines #
elide="expand" keeps the DOM bounded on big files and gives the hidden lines
back on demand: each elided region is a count row whose buttons reveal 20 lines down from
the hunk above, 20 up from the hunk below, or all of them (expand_step sets the
20). Only the revealed rows render; the rest of the diff's DOM stays as it was. What's
revealed is expanded, ranges of b line numbers — internal by
default, or bound to persist and restore it. Line numbers rather than rows keep it
meaningful across models of the same diff. scroll_to_line on a hidden line
reveals the lines around it first.
The default 'details' needs no JS but puts every elided line in the DOM, and 'omit' renders only the count. For a static render, render_diff_segments draws the same rows in keyed pieces.
<CodeDiff bind:this={diff} {a} {b} lang="ts" elide="expand" bind:expanded /> expanded: []
Patches #
A file too big to fetch whole can render from its patch alone: pass the hunks — one file's DiffHunks, which fuz_util parses from git diff output — as hunks, through create_diff_patch_source. Each run of
hunks is lexed on its own, so highlighting is approximate where a hunk starts inside a
comment or string opened above it (Svelte, HTML, and Markdown guess whether a hunk sits
inside a <script>, a <style>, or a fence, through the
language's snippet_prefix), and the lines between hunks show as counts with
nothing to expand. The lines after the last hunk count when b_line_count is
known, or when patch_context, the patch's -U, proves the last hunk
reaches the end of the file. To cache the patch source, or to handle malformed hunks rather
than have hunks throw, build it yourself and pass it as source.
When both texts arrive, swap hunks for a model as source (or a and b) on the same component: the view upgrades in place,
keeping expanded and highlight, and the first row in view stays
where it was.
<CodeDiff {...(model ? {source: model} : {hunks})} patch_context={3} lang="ts" elide="expand" bind:expanded />