Skip to main content

Crate ashlar_diff

Crate ashlar_diff 

Source
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§

DiffOptions
Options for FileDiff::from_texts_with.
Edit
An edit script from the old to the new sequence.
FileDiff
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.
PatchError
A patch that did not parse, and where.

Enums§

FileStatus
How a file changed.
LineKind
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.
PatchErrorKind
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² / 2 words, 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_cost caps one Myers run (see the module docs); it is clamped to 1..=2048.
parse_patch
Parses a unified patch into one FileDiff per file, with word-level emphasis.
word_diff
The byte ranges that changed between old and new, word by word: (in old, in new). Adjacent changed tokens merge into one range. None when 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_patch for git diff output).