Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/gh_range_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ fn process_old_new(
color: white;
background-color: rgb(63, 128, 94);
}}
.spacer {{
margin-bottom: 1rem;
}}
.hide {{
display: none;
}}
#show-context-only-changes-cb:checked ~ .hide {{
display: block;
}}
@media (prefers-color-scheme: dark) {{
body {{
background: #0C0C0C;
Expand Down Expand Up @@ -330,7 +339,10 @@ fn process_old_new(
</head>
<body>
<h3>range-diff of {a_oldbase}..{a_oldhead} {a_newbase}..{a_newhead} in {owner}/{repo}</h3>
<p>Legend: {REMOVED_BLOCK_SIGN}&nbsp;before | {ADDED_BLOCK_SIGN}&nbsp;after</p>
<span>Legend: {REMOVED_BLOCK_SIGN}&nbsp;before | {ADDED_BLOCK_SIGN}&nbsp;after</span>
<input type="checkbox" id="show-context-only-changes-cb">
<label for="show-context-only-changes-cb"> Show context-only changes</label>
<div class="spacer"></div>
"#
)?;

Expand All @@ -355,6 +367,22 @@ fn process_old_new(
let has_hunks = diff.hunks().next().is_some();

if has_hunks {
let has_content_changes = 'context: {
for mut hunk in diff.hunks() {
let contains_diff_marker = |idx: u32, source: &[Token]| {
let line = &input.interner[source[idx as usize]];
line.starts_with('+') || line.starts_with('-')
};

if hunk.before.any(|i| contains_diff_marker(i, &input.before))
|| hunk.after.any(|i| contains_diff_marker(i, &input.after))
{
break 'context true;
}
}
false
};

let printer = HtmlDiffPrinter(&input.interner);
let diff = diff.unified_diff(&printer, config.clone(), &input);

Expand All @@ -363,10 +391,22 @@ fn process_old_new(
let after_href =
format_args!("https://github.com/{owner}/{repo}/blob/{newhead}/{filename}");

writeln!(
if has_content_changes {
write!(html, r#"<details open=""><summary>{filename}"#)?;
} else {
/* Context only changes, hide by default */
write!(
html,
r#"<details open="" class="hide" data-context-only-changes=""><summary>{filename} <i>(context-only changes)</i>"#
)?;
}

write!(
html,
r#"<details open=""><summary>{filename} <a href="{before_href}">before</a> <a href="{after_href}">after</a></summary><pre class="diff-content">{diff}</pre></details>"#
r#" <a href="{before_href}">before</a> <a href="{after_href}">after</a></summary><pre class="diff-content">{diff}</pre>"#
)?;
writeln!(html, "</details>")?;

diff_displayed += 1;
}
Ok(())
Expand Down