Always display all lines in a hunk

Previously we were assuming that the first/last line pairs in a hunk
contained the earliest/latest lines on both sides. This isn't true
when there are no common items between the lines.

This fixes some display issues in load_before/after.js, but include a
new integration test that is smaller and easier to eyeball.

Fixes #133
pull/166/head
Wilfred Hughes 2022-03-15 00:11:30 +07:00
parent 6d9dc8322f
commit b2229d66a8
5 changed files with 54 additions and 6 deletions

@ -10,6 +10,10 @@ incorrect diffs.
Fixed an issue where lines were not aligned correctly after correcting
sliders.
### Display
Fixed an issue where some lines in a hunk were not displayed.
## 0.22 (release 10th March 2022)
Difftastic now requires Rust 1.56 to build.

@ -10,6 +10,9 @@ sample_files/clojure_before.clj sample_files/clojure_after.clj
sample_files/comments_before.rs sample_files/comments_after.rs
898ffdafebc08cdf4e9fe43cb3619e6c -
sample_files/context_before.rs sample_files/context_after.rs
5f8518317cbaa2920c9d8c86d060c84f -
sample_files/contiguous_before.js sample_files/contiguous_after.js
b5e718eb8328f8df8aff0eddf4e46d1a -
@ -53,7 +56,7 @@ sample_files/jsx_before.jsx sample_files/jsx_after.jsx
5584ecaed7f41540d646afe39df31156 -
sample_files/load_before.js sample_files/load_after.js
c7a5800de30556a6a2cf795744fc28b9 -
bf8527f91baff9a5f56380784b2a438e -
sample_files/modules_before.ml sample_files/modules_after.ml
d182f73a8729211cc41e297aeab8f7a9 -

@ -0,0 +1,14 @@
fn print_diff_result() {
match () {
x => {
let opposite_to_lhs = opposite_positions(&summary.lhs_positions);
let hunks = merge_adjacent(
hunks,
opposite_to_lhs,
);
let lang_name;
}
}
}

@ -0,0 +1,15 @@
fn print_diff_result() {
if summary {
if print_unchanged {
}
}
let opposite_to_lhs = opposite_positions(&summary.lhs_positions);
let hunks = merge_adjacent(
hunks,
opposite_to_lhs,
);
let lang_name;
}

@ -651,13 +651,25 @@ pub fn matched_lines_for_hunk(
matched_lines: &[(Option<LineNumber>, Option<LineNumber>)],
hunk: &Hunk,
) -> Vec<(Option<LineNumber>, Option<LineNumber>)> {
// TODO: Use binary search instead.
let hunk_first = hunk.lines.first().expect("Hunks are non-empty");
let hunk_last = hunk.lines.last().expect("Hunks are non-empty");
let mut hunk_lhs_novel = hunk.novel_lhs.iter().copied().collect::<Vec<_>>();
hunk_lhs_novel.sort();
let mut hunk_rhs_novel = hunk.novel_rhs.iter().copied().collect::<Vec<_>>();
hunk_rhs_novel.sort();
let hunk_smallest = (
hunk_lhs_novel.first().copied(),
hunk_rhs_novel.first().copied(),
);
let hunk_largest = (
hunk_lhs_novel.last().copied(),
hunk_rhs_novel.last().copied(),
);
// TODO: Use binary search instead.
let mut start_i = None;
for (i, matched_line) in matched_lines.iter().enumerate() {
if either_side_equal(matched_line, hunk_first) {
if either_side_equal(matched_line, &hunk_smallest) {
start_i = Some(i);
break;
}
@ -665,7 +677,7 @@ pub fn matched_lines_for_hunk(
let mut end_i = None;
for (i, matched_line) in matched_lines.iter().enumerate().rev() {
if either_side_equal(matched_line, hunk_last) {
if either_side_equal(matched_line, &hunk_largest) {
end_i = Some(i + 1);
break;
}