Skip to content

Commit c79bac3

Browse files
authored
Merge pull request #2156 from Urgau/gh-range-diff-edge-cases
Improves line colors for edge-cases in our range-diff
2 parents d489199 + 08eaebb commit c79bac3

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/gh_range_diff.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub async fn gh_range_diff(
213213
</head>
214214
<body>
215215
<h3>range-diff of {oldbase}<wbr>...{oldhead} {newbase}<wbr>...{newhead}</h3>
216-
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span> | {ADDED_BLOCK_SIGN}&nbsp;added {REMOVED_BLOCK_SIGN}&nbsp;removed</p>
216+
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span> | {ADDED_BLOCK_SIGN}&nbsp;<span class="added-line">+</span> adds a line | {ADDED_BLOCK_SIGN}&nbsp;<span class="removed-line">-</span> removes a line | {REMOVED_BLOCK_SIGN}&nbsp;<span class="removed-line">+</span> removes the added line | {REMOVED_BLOCK_SIGN}&nbsp;- cancel the removal</p>
217217
"#
218218
)?;
219219

@@ -311,15 +311,44 @@ pub async fn gh_range_diff(
311311
const REMOVED_BLOCK_SIGN: &str = r#"<span class="removed-block"> - </span>"#;
312312
const ADDED_BLOCK_SIGN: &str = r#"<span class="added-block"> + </span>"#;
313313

314+
enum HunkTokenStatus {
315+
Added,
316+
Removed,
317+
}
318+
314319
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
315320

316321
impl HtmlDiffPrinter<'_> {
317-
fn handle_hunk_token(&self, mut f: impl fmt::Write, class: &str, token: &str) -> fmt::Result {
318-
write!(f, " ")?;
322+
fn handle_hunk_token(
323+
&self,
324+
mut f: impl fmt::Write,
325+
hunk_token_status: HunkTokenStatus,
326+
token: &str,
327+
) -> fmt::Result {
328+
// Show the hunk status
329+
match hunk_token_status {
330+
HunkTokenStatus::Added => write!(f, "{ADDED_BLOCK_SIGN} ")?,
331+
HunkTokenStatus::Removed => write!(f, "{REMOVED_BLOCK_SIGN} ")?,
332+
};
333+
334+
let is_add = token.starts_with('+');
335+
let is_remove = token.starts_with('-');
336+
319337
// Highlight the whole the line only if it has changes it-self, otherwise
320338
// only highlight the `+`, `-` to avoid distracting users with context
321339
// changes.
322-
if token.starts_with('+') || token.starts_with('-') {
340+
if is_add || is_remove {
341+
let class = match (hunk_token_status, is_add) {
342+
// adds a line
343+
(HunkTokenStatus::Added, true) => "added-line",
344+
// removes a line
345+
(HunkTokenStatus::Added, false) => "removed-line",
346+
// removes the added line
347+
(HunkTokenStatus::Removed, true) => "removed-line",
348+
// removes the removed line, so nothing changed
349+
(HunkTokenStatus::Removed, false) => "",
350+
};
351+
323352
write!(f, r#"<span class="{class}">"#)?;
324353
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
325354
write!(f, "</span>")?;
@@ -362,8 +391,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
362391
if let Some(&last) = before.last() {
363392
for &token in before {
364393
let token = self.0[token];
365-
write!(f, "{REMOVED_BLOCK_SIGN}")?;
366-
self.handle_hunk_token(&mut f, "removed-line", token)?;
394+
self.handle_hunk_token(&mut f, HunkTokenStatus::Removed, token)?;
367395
}
368396
if !self.0[last].ends_with('\n') {
369397
writeln!(f)?;
@@ -373,8 +401,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
373401
if let Some(&last) = after.last() {
374402
for &token in after {
375403
let token = self.0[token];
376-
write!(f, "{ADDED_BLOCK_SIGN}")?;
377-
self.handle_hunk_token(&mut f, "added-line", token)?;
404+
self.handle_hunk_token(&mut f, HunkTokenStatus::Added, token)?;
378405
}
379406
if !self.0[last].ends_with('\n') {
380407
writeln!(f)?;

0 commit comments

Comments
 (0)