Skip to content

Commit

Permalink
diff: pad last newline by show_color_words_diff_line()
Browse files Browse the repository at this point in the history
Though this is needed only for the last line, checking it for each line is
cheap. As I'm going to add another rendering style, the condition to pad "\n"
would become more complicated.
  • Loading branch information
yuja committed Aug 21, 2024
1 parent bb686c1 commit be9b7ed
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,48 +429,42 @@ fn show_color_words_diff_hunks(
let line_diff = Diff::by_line([left, right]);
let mut line_diff_hunks = line_diff.hunks().peekable();
let mut line_number = DiffLineNumber { left: 1, right: 1 };
// Have we printed "..." for the last skipped context?
let mut skipped_context = false;

// First "before" context
if let Some(DiffHunk::Matching(content)) =
line_diff_hunks.next_if(|hunk| matches!(hunk, DiffHunk::Matching(_)))
{
if line_diff_hunks.peek().is_some() {
let (new_line_number, _) = show_color_words_context_lines(
line_number = show_color_words_context_lines(
formatter,
content,
line_number,
0,
options.context,
)?;
line_number = new_line_number;
}
}
while let Some(hunk) = line_diff_hunks.next() {
match hunk {
// Middle "after"/"before" context
DiffHunk::Matching(content) if line_diff_hunks.peek().is_some() => {
let (new_line_number, _) = show_color_words_context_lines(
line_number = show_color_words_context_lines(
formatter,
content,
line_number,
options.context,
options.context,
)?;
line_number = new_line_number;
}
// Last "after" context
DiffHunk::Matching(content) => {
let (new_line_number, skipped) = show_color_words_context_lines(
line_number = show_color_words_context_lines(
formatter,
content,
line_number,
options.context,
0,
)?;
line_number = new_line_number;
skipped_context = skipped;
}
DiffHunk::Different(contents) => {
let word_diff = Diff::by_word(&contents);
Expand All @@ -484,13 +478,6 @@ fn show_color_words_diff_hunks(
}
}

// If the last diff line doesn't end with newline, add it.
let no_hunk = left.is_empty() && right.is_empty();
let any_last_newline = left.ends_with(b"\n") || right.ends_with(b"\n");
if !skipped_context && !no_hunk && !any_last_newline {
writeln!(formatter)?;
}

Ok(())
}

Expand All @@ -501,7 +488,7 @@ fn show_color_words_context_lines(
mut line_number: DiffLineNumber,
num_after: usize,
num_before: usize,
) -> io::Result<(DiffLineNumber, bool)> {
) -> io::Result<DiffLineNumber> {
const SKIPPED_CONTEXT_LINE: &str = " ...\n";
let mut lines = content.split_inclusive(|b| *b == b'\n').fuse();
for line in lines.by_ref().take(num_after) {
Expand Down Expand Up @@ -530,7 +517,7 @@ fn show_color_words_context_lines(
line_number.left += 1;
line_number.right += 1;
}
Ok((line_number, num_skipped > 0))
Ok(line_number)
}

fn show_color_words_line_number(
Expand Down Expand Up @@ -584,7 +571,10 @@ fn show_color_words_diff_line(
formatter.write_all(data)?;
}
}

let (_, data) = diff_line.hunks.last().expect("diff line must not be empty");
if !data.ends_with(b"\n") {
writeln!(formatter)?;
};
Ok(())
}

Expand Down

0 comments on commit be9b7ed

Please sign in to comment.