Fix crash when diff touches first line

pull/25/head
Wilfred Hughes 2021-07-08 01:07:33 +07:00
parent 31e358a7c6
commit 1dc6ab3977
2 changed files with 19 additions and 3 deletions

@ -3,6 +3,8 @@
Fixed an issue where complex diffs would not display some unchanged
lines.
Fixed a crash when diff context included the first line.
## 0.3
Diffs are now displayed with unchanged lines aligned to the other side.

@ -556,16 +556,16 @@ pub fn aligned_lines(
rhs_lines: &[LineNumber],
lhs_line_matches: &HashMap<LineNumber, LineNumber>,
) -> Vec<(Option<LineNumber>, Option<LineNumber>)> {
let mut rhs_highest_matched = rhs_lines.first().unwrap().number - 1;
let mut rhs_highest_matched = rhs_lines.first().unwrap().number as isize - 1;
// For every LHS line, if there is a RHS line that is included in
// `rhs_lines` and hasn't yet been paired up, add it to
// matched_lines.
let mut matched_lines = vec![];
for lhs_line in lhs_lines {
if let Some(rhs_line) = lhs_line_matches.get(lhs_line) {
if rhs_line.number > rhs_highest_matched {
if rhs_line.number as isize > rhs_highest_matched {
matched_lines.push((lhs_line, rhs_line));
rhs_highest_matched = rhs_line.number;
rhs_highest_matched = rhs_line.number as isize;
}
}
}
@ -770,6 +770,20 @@ mod tests {
);
}
#[test]
fn test_aligned_first_line() {
let lhs_lines: Vec<LineNumber> = vec![0.into()];
let rhs_lines: Vec<LineNumber> = vec![0.into()];
let mut line_matches: HashMap<LineNumber, LineNumber> = HashMap::new();
line_matches.insert(0.into(), 0.into());
assert_eq!(
aligned_lines(&lhs_lines, &rhs_lines, &line_matches),
vec![(Some(0.into()), Some(0.into()))]
);
}
/// Ensure that we assign prev_opposite_pos even if the change is on the first node.
#[test]
fn test_prev_opposite_pos_first_node() {