Skip to content

Commit

Permalink
diff: deduplicate write() calls in show_unified_diff_hunks()
Browse files Browse the repository at this point in the history
I'm going to add word-level highlighting there.
  • Loading branch information
yuja committed Jul 4, 2024
1 parent 034859b commit fa442b6
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,26 +903,15 @@ fn show_unified_diff_hunks(
hunk.right_line_range.len()
)?;
for (line_type, content) in hunk.lines {
match line_type {
DiffLineType::Context => {
formatter.with_label("context", |formatter| {
write!(formatter, " ")?;
formatter.write_all(content)
})?;
}
DiffLineType::Removed => {
formatter.with_label("removed", |formatter| {
write!(formatter, "-")?;
formatter.write_all(content)
})?;
}
DiffLineType::Added => {
formatter.with_label("added", |formatter| {
write!(formatter, "+")?;
formatter.write_all(content)
})?;
}
}
let (label, sigil) = match line_type {
DiffLineType::Context => ("context", " "),
DiffLineType::Removed => ("removed", "-"),
DiffLineType::Added => ("added", "+"),
};
formatter.with_label(label, |formatter| {
write!(formatter, "{sigil}")?;
formatter.write_all(content)
})?;
if !content.ends_with(b"\n") {
write!(formatter, "\n\\ No newline at end of file\n")?;
}
Expand Down

0 comments on commit fa442b6

Please sign in to comment.