Skip to content

Commit 3d7c70d

Browse files
committed
Avoid highlighting the whole line for context changes
1 parent 8133824 commit 3d7c70d

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
@@ -271,6 +271,28 @@ pub async fn gh_range_diff(
271271

272272
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
273273

274+
impl HtmlDiffPrinter<'_> {
275+
fn handle_hunk_token(
276+
&self,
277+
mut f: impl fmt::Write,
278+
span_open: &str,
279+
token: &str,
280+
) -> fmt::Result {
281+
write!(f, "{span_open}")?;
282+
// Highlight the whole the line only if it has changes it-self, otherwise
283+
// only highlight the `+`, `-` to avoid distracting users with context
284+
// changes.
285+
if token.starts_with('+') || token.starts_with('-') {
286+
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
287+
write!(f, "</span>")?;
288+
} else {
289+
write!(f, "</span>")?;
290+
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
291+
}
292+
Ok(())
293+
}
294+
}
295+
274296
impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
275297
fn display_header(
276298
&self,
@@ -303,9 +325,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
303325
if let Some(&last) = before.last() {
304326
for &token in before {
305327
let token = self.0[token];
306-
write!(f, r#"<span style="color:red;">-"#)?;
307-
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
308-
write!(f, "</span>")?;
328+
self.handle_hunk_token(&mut f, r#"<span style="color:red;">-"#, token)?;
309329
}
310330
if !self.0[last].ends_with('\n') {
311331
writeln!(f)?;
@@ -315,9 +335,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
315335
if let Some(&last) = after.last() {
316336
for &token in after {
317337
let token = self.0[token];
318-
write!(f, r#"<span style="color:green;">+"#)?;
319-
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
320-
write!(f, "</span>")?;
338+
self.handle_hunk_token(&mut f, r#"<span style="color:green;">+"#, token)?;
321339
}
322340
if !self.0[last].ends_with('\n') {
323341
writeln!(f)?;

0 commit comments

Comments
 (0)