From 3d458c3e589f4250a0fd8cfca2844a47d10d9577 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 28 Jun 2024 23:34:35 +0900 Subject: [PATCH] diff: extract helpers that push unified-diff hunk lines --- cli/src/diff_util.rs | 57 +++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index cfe65238a..5f87b7a91 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -735,6 +735,30 @@ struct UnifiedDiffHunk<'content> { lines: Vec<(DiffLineType, &'content [u8])>, } +impl<'content> UnifiedDiffHunk<'content> { + fn extend_context_lines(&mut self, lines: impl IntoIterator) { + let old_len = self.lines.len(); + self.lines + .extend(lines.into_iter().map(|line| (DiffLineType::Context, line))); + self.left_line_range.end += self.lines.len() - old_len; + self.right_line_range.end += self.lines.len() - old_len; + } + + fn extend_removed_lines(&mut self, lines: impl IntoIterator) { + let old_len = self.lines.len(); + self.lines + .extend(lines.into_iter().map(|line| (DiffLineType::Removed, line))); + self.left_line_range.end += self.lines.len() - old_len; + } + + fn extend_added_lines(&mut self, lines: impl IntoIterator) { + let old_len = self.lines.len(); + self.lines + .extend(lines.into_iter().map(|line| (DiffLineType::Added, line))); + self.right_line_range.end += self.lines.len() - old_len; + } +} + fn unified_diff_hunks<'content>( left_content: &'content [u8], right_content: &'content [u8], @@ -758,11 +782,7 @@ fn unified_diff_hunks<'content>( } else { 0 }); - current_hunk.left_line_range.end += num_after_lines; - current_hunk.right_line_range.end += num_after_lines; - for line in lines.iter().take(num_after_lines) { - current_hunk.lines.push((DiffLineType::Context, line)); - } + current_hunk.extend_context_lines(lines.iter().copied().take(num_after_lines)); let num_skip_lines = lines .len() .saturating_sub(num_after_lines) @@ -779,29 +799,16 @@ fn unified_diff_hunks<'content>( lines: vec![], }; } - let num_before_lines = lines.len() - num_after_lines - num_skip_lines; - current_hunk.left_line_range.end += num_before_lines; - current_hunk.right_line_range.end += num_before_lines; - for line in lines.iter().skip(num_after_lines + num_skip_lines) { - current_hunk.lines.push((DiffLineType::Context, line)); - } + current_hunk.extend_context_lines( + lines.iter().copied().skip(num_after_lines + num_skip_lines), + ); } DiffHunk::Different(content) => { show_context_after = true; - let left_lines = content[0].split_inclusive(|b| *b == b'\n').collect_vec(); - let right_lines = content[1].split_inclusive(|b| *b == b'\n').collect_vec(); - if !left_lines.is_empty() { - current_hunk.left_line_range.end += left_lines.len(); - for line in left_lines { - current_hunk.lines.push((DiffLineType::Removed, line)); - } - } - if !right_lines.is_empty() { - current_hunk.right_line_range.end += right_lines.len(); - for line in right_lines { - current_hunk.lines.push((DiffLineType::Added, line)); - } - } + 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); } } }