Improve diff op grouping for full matches

This changes the behavior of the diff op grouping function to return
an empty result if the diff is a full match.  This also adds an example
to demonstrate unified diff rendering.
This commit is contained in:
Armin Ronacher 2021-01-30 23:11:35 +01:00
parent 4459c5ee3a
commit 020701c4d5
5 changed files with 62 additions and 3 deletions

View file

@ -222,8 +222,9 @@ pub fn group_diff_ops(mut ops: Vec<DiffOp>, n: usize) -> Vec<Vec<DiffOp>> {
pending_group.push(op);
}
if !pending_group.is_empty() {
rv.push(pending_group);
match &pending_group[..] {
&[] | &[DiffOp::Equal { .. }] => {}
_ => rv.push(pending_group),
}
rv

View file

@ -0,0 +1,25 @@
---
source: src/text/udiff.rs
expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()"
---
--- a.txt
+++ b.txt
@@ -15,3 +19,3 @@
p
q
r
-s
+S
t
u
v
@@ -37,3 +41,3 @@
L
M
N
-O
+o
P
Q
R

View file

@ -178,8 +178,12 @@ impl<'diff, 'old, 'new, 'bufs> fmt::Display for UnifiedDiffHunk<'diff, 'old, 'ne
} else {
"\n"
};
writeln!(f, "{}", self.header())?;
let mut wrote_header = false;
for change in self.iter_changes() {
if !wrote_header {
writeln!(f, "{}", self.header())?;
wrote_header = true;
}
write!(
f,
"{}{}{}",
@ -238,3 +242,8 @@ fn test_unified_diff() {
);
insta::assert_snapshot!(&diff.unified_diff().header("a.txt", "b.txt").to_string());
}
#[test]
fn test_empty_unified_diff() {
let diff = TextDiff::from_lines("abc", "abc");
assert_eq!(diff.unified_diff().header("a.txt", "b.txt").to_string(), "");
}