Skip to content

Commit df6aad3

Browse files
committed
Avoid highlighting the whole line for context changes
1 parent cde8e38 commit df6aad3

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/gh_range_diff.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,28 @@ pub async fn gh_range_diff(
274274

275275
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
276276

277+
impl HtmlDiffPrinter<'_> {
278+
fn handle_hunk_token(
279+
&self,
280+
mut f: impl fmt::Write,
281+
span_open: &str,
282+
token: &str,
283+
) -> fmt::Result {
284+
write!(f, "{span_open}")?;
285+
// Highlight the whole the line only if it has changes it-self, otherwise
286+
// only highlight the `+`, `-` to avoid distracting users with context
287+
// changes.
288+
if token.starts_with('+') || token.starts_with('-') {
289+
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
290+
write!(f, "</span>")?;
291+
} else {
292+
write!(f, "</span>")?;
293+
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
294+
}
295+
Ok(())
296+
}
297+
}
298+
277299
impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
278300
fn display_header(
279301
&self,
@@ -306,9 +328,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
306328
if let Some(&last) = before.last() {
307329
for &token in before {
308330
let token = self.0[token];
309-
write!(f, r#"<span style="color:red;">-"#)?;
310-
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
311-
write!(f, "</span>")?;
331+
self.handle_hunk_token(&mut f, r#"<span style="color:red;">-"#, token)?;
312332
}
313333
if !self.0[last].ends_with('\n') {
314334
writeln!(f)?;
@@ -318,9 +338,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
318338
if let Some(&last) = after.last() {
319339
for &token in after {
320340
let token = self.0[token];
321-
write!(f, r#"<span style="color:green;">+"#)?;
322-
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
323-
write!(f, "</span>")?;
341+
self.handle_hunk_token(&mut f, r#"<span style="color:green;">+"#, token)?;
324342
}
325343
if !self.0[last].ends_with('\n') {
326344
writeln!(f)?;

0 commit comments

Comments
 (0)