Fix missing context on misaligned hunks

Fixes #134
html_output
Wilfred Hughes 2022-02-12 12:26:00 +07:00
parent 92cf448081
commit 84a2dc2dac
2 changed files with 40 additions and 1 deletions

@ -12,6 +12,9 @@ whilst still having acceptable performance.
Fixed an issue where whole file additions/removals were printed twice. Fixed an issue where whole file additions/removals were printed twice.
Fixed an issue where difftastic didn't show context on hunks where the
unchanged content was on different lines.
### Command Line Interface ### Command Line Interface
Difftastic will now use a text dif for large files that are too big to Difftastic will now use a text dif for large files that are too big to

@ -663,7 +663,6 @@ pub fn matched_lines_for_hunk(
for (i, matched_line) in matched_lines.iter().enumerate() { for (i, matched_line) in matched_lines.iter().enumerate() {
if either_side_equal(matched_line, hunk_last) { if either_side_equal(matched_line, hunk_last) {
end_i = Some(i + 1); end_i = Some(i + 1);
break;
} }
} }
@ -847,4 +846,41 @@ mod tests {
] ]
); );
} }
#[test]
fn test_matched_lines_for_hunk_misaligned() {
let matched_lines = &[
(Some(0.into()), Some(0.into())),
(Some(1.into()), Some(1.into())),
(Some(2.into()), Some(2.into())),
(Some(3.into()), Some(3.into())),
(Some(4.into()), Some(4.into())),
(Some(5.into()), Some(5.into())),
];
let novel_lhs = HashSet::from_iter([1.into()]);
let novel_rhs = HashSet::from_iter([2.into()]);
let hunk = Hunk {
novel_lhs,
novel_rhs,
// LHS and RHS are misaligned
lines: vec![(Some(1.into()), Some(2.into()))],
};
let res = matched_lines_for_hunk(matched_lines, &hunk);
assert_eq!(
res,
vec![
(Some(0.into()), Some(0.into())),
(Some(1.into()), Some(1.into())),
(Some(2.into()), Some(2.into())),
// We want to show the full 3 lines of padding after
// the lower of the two lines, so up to line 5
// inclusive.
(Some(3.into()), Some(3.into())),
(Some(4.into()), Some(4.into())),
(Some(5.into()), Some(5.into())),
]
);
}
} }