From ec709e7f8b98a2a7ce5509798a11d493fa7c81a3 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 29 Jun 2024 00:18:46 +0900 Subject: [PATCH] diff: rely on emptiness of unified hunk lines whether to append "after" 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. --- cli/src/diff_util.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 30fe05fee..d43569bd9 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -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(); @@ -793,10 +793,10 @@ 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); @@ -804,6 +804,7 @@ fn unified_diff_hunks<'content>( } } } + // The last unified hunk might contain redundant "before" context lines. if !current_hunk .lines .iter()