Expand description
Headless text diffs for ashlar: line diffs with a cost cap, word-level changes, hunks with context and collapsible unchanged ranges, and a unified-patch parser and writer.
No dependencies, no UI; builds for wasm32-unknown-unknown. The view lives in
ashlar_ui::diff_view.
use ashlar_diff::{FileDiff, LineKind};
let d = FileDiff::from_texts("a\nb\nc\n", "a\nB\nc\n").with_paths("x.txt", "x.txt");
let hunk = &d.hunks[0];
assert_eq!((hunk.old_start, hunk.old_len, hunk.new_start, hunk.new_len), (1, 3, 1, 3));
assert_eq!(hunk.lines[1].kind, LineKind::Removed);
assert_eq!(d.to_patch(), "--- a/x.txt\n+++ b/x.txt\n@@ -1,3 +1,3 @@\n a\n-b\n+B\n c\n");Patches from git diff parse into one FileDiff per file and write back byte for byte:
let patch = "diff --git a/f b/f\nindex 1..2 100644\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n-x\n+y\n";
let files = ashlar_diff::parse_patch(patch).unwrap();
assert_eq!(files[0].new_path.as_deref(), Some("f"));
assert_eq!(ashlar_diff::write_patch(&files), patch);Structs§
- Diff
Options - Options for
FileDiff::from_texts_with. - Edit
- An edit script from the old to the new sequence.
- File
Diff - The changes to one file.
- Gap
- An unchanged range between hunks (or before the first, or after the last), which a view shows collapsed.
- Hunk
- A run of changes with the context around them.
- Line
- One line of a hunk.
- Patch
Error - A patch that did not parse, and where.
Enums§
- File
Status - How a file changed.
- Line
Kind - What a diff line is.
- Op
- One run of an edit script. Indices are 0-based positions in the old (
a) and new (b) inputs. - Patch
Error Kind - Why a patch did not parse.
Constants§
- DEFAULT_
MAX_ COST - The default cost cap: the largest edit distance one Myers run may reach. Its trace takes about
cap² / 2words, so the default keeps it near 4 MB. - MAX_
WORD_ DIFF_ LINE - Lines longer than this (bytes) get no word-level emphasis.
Functions§
- diff_
ops - Diffs two sequences.
max_costcaps one Myers run (see the module docs); it is clamped to1..=2048. - parse_
patch - Parses a unified patch into one
FileDiffper file, with word-level emphasis. - word_
diff - The byte ranges that changed between
oldandnew, word by word:(in old, in new). Adjacent changed tokens merge into one range.Nonewhen either line is too long, the lines are equal, or they share no word (emphasis would then cover everything and say nothing). - write_
patch - Writes files as one unified patch (the inverse of
parse_patchforgit diffoutput).