Skip to content

Commit

Permalink
diff: rely on emptiness of unified hunk lines whether to append "afte…
Browse files Browse the repository at this point in the history
…r" context

show_context_after was set once when DiffHunk::Different received, and was
never turned off. This means DiffHunk::Matching is not supposed to repeat.
Under this condition, we can assume that removed/added lines exist if lines
isn't empty.
  • Loading branch information
yuja committed Jul 2, 2024
1 parent 318f594 commit ec709e7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,13 @@ fn unified_diff_hunks<'content>(
right_line_range: 1..1,
lines: vec![],
};
let mut show_context_after = false;
let diff = Diff::for_tokenizer(&[left_content, right_content], diff::find_line_ranges);
for hunk in diff.hunks() {
match hunk {
DiffHunk::Matching(content) => {
let mut lines = content.split_inclusive(|b| *b == b'\n').fuse();
if show_context_after {
if !current_hunk.lines.is_empty() {
// The previous hunk line should be either removed/added.
current_hunk.extend_context_lines(lines.by_ref().take(num_context_lines));
}
let before_lines = lines.by_ref().rev().take(num_context_lines).collect_vec();
Expand All @@ -793,17 +793,18 @@ fn unified_diff_hunks<'content>(
lines: vec![],
};
}
// The next hunk should be of DiffHunk::Different type if any.
current_hunk.extend_context_lines(before_lines.into_iter().rev());
}
DiffHunk::Different(content) => {
show_context_after = true;
let left_lines = content[0].split_inclusive(|b| *b == b'\n');
let right_lines = content[1].split_inclusive(|b| *b == b'\n');
current_hunk.extend_removed_lines(left_lines);
current_hunk.extend_added_lines(right_lines);
}
}
}
// The last unified hunk might contain redundant "before" context lines.
if !current_hunk
.lines
.iter()
Expand Down

0 comments on commit ec709e7

Please sign in to comment.