summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2022-02-27 22:24:38 -0800
committerWilfred Hughes <me@wilfred.me.uk>2022-02-27 22:24:38 -0800
commita40d41ff96bd2a56e8cbec9ec017b32b89d0ccaf (patch)
tree4b1c31304b4e29748134f7a695f12a7aac491161
parentbd1cafadfb47372c13c41c7eeaa2ae54a646a790 (diff)
Highlight modified lineshtml_output
-rw-r--r--src/html.rs49
-rw-r--r--templates/summary.html27
2 files changed, 56 insertions, 20 deletions
diff --git a/src/html.rs b/src/html.rs
index 7198727fb..c9983eb11 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
use askama::Template;
@@ -7,7 +7,7 @@ use crate::{
hunks::{matched_lines_for_hunk, Hunk},
lines::{codepoint_len, LineNumber},
positions::SingleLineSpan,
- side_by_side::split_on_newlines,
+ side_by_side::{lines_with_novel, split_on_newlines},
syntax::{AtomKind, MatchKind, MatchedPos, TokenKind},
};
@@ -19,6 +19,8 @@ type NumberedLine = (LineNumber, StyledLine);
struct SummaryTemplate {
display_path: String,
paired_lines: Vec<(Option<NumberedLine>, Option<NumberedLine>)>,
+ lhs_lines_with_novel: HashSet<LineNumber>,
+ rhs_lines_with_novel: HashSet<LineNumber>,
}
fn apply_line(
@@ -33,7 +35,10 @@ fn apply_line(
res.push((line[offset..span.start_col].to_owned(), vec![]));
}
- res.push((line[span.start_col..span.end_col].to_owned(), classes.clone()));
+ res.push((
+ line[span.start_col..span.end_col].to_owned(),
+ classes.clone(),
+ ));
offset = span.end_col;
}
if offset < codepoint_len(line) {
@@ -52,21 +57,33 @@ fn apply_styles(
let line_pos = mp.pos;
let mut span_classes = vec![];
match mp.kind {
- MatchKind::UnchangedToken { highlight, .. } => match highlight {
- TokenKind::Atom(kind) => match kind {
- AtomKind::Normal => {}
- AtomKind::String => span_classes.push("pl-s"),
- AtomKind::Type => span_classes.push("pl-k"),
- AtomKind::Comment => span_classes.push("pl-c"),
- AtomKind::Keyword => span_classes.push("pl-k"),
- },
- _ => {}
- },
MatchKind::Novel { .. }
- | MatchKind::NovelLinePart { .. }
| MatchKind::NovelWord { .. } => {
span_classes.push(if is_lhs { "novel-lhs" } else { "novel-rhs" });
}
+ MatchKind::UnchangedToken { .. } => {}
+ MatchKind::NovelLinePart { .. } => {
+ // We don't want extra highlighting for a line: the
+ // relevant part is highlighted as NovelWord.
+ }
+ }
+
+ let highlight = match mp.kind {
+ MatchKind::UnchangedToken { highlight, .. } => highlight,
+ MatchKind::Novel { highlight } => highlight,
+ MatchKind::NovelLinePart { highlight, .. } => highlight,
+ MatchKind::NovelWord { highlight } => highlight,
+ };
+
+ match highlight {
+ TokenKind::Atom(kind) => match kind {
+ AtomKind::Normal => {}
+ AtomKind::String => span_classes.push("pl-s"),
+ AtomKind::Type => span_classes.push("pl-k"),
+ AtomKind::Comment => span_classes.push("pl-c"),
+ AtomKind::Keyword => span_classes.push("pl-k"),
+ },
+ _ => {}
}
let line_classes = line_styles.entry(line_pos.line).or_insert_with(Vec::new);
@@ -90,6 +107,8 @@ pub fn print(
let rhs_line_styles = apply_styles(false, rhs_mps);
let empty_styles = vec![];
+ let (lhs_lines_with_novel, rhs_lines_with_novel) = lines_with_novel(lhs_mps, rhs_mps);
+
let matched_lines = all_matched_lines_filled(lhs_mps, rhs_mps);
let mut paired_lines: Vec<(Option<NumberedLine>, Option<NumberedLine>)> = vec![];
@@ -121,6 +140,8 @@ pub fn print(
let template = SummaryTemplate {
display_path: display_path.into(),
paired_lines,
+ lhs_lines_with_novel,
+ rhs_lines_with_novel,
};
println!("{}", template.render().unwrap());
}
diff --git a/templates/summary.html b/templates/summary.html
index ec1e3237b..d9e202296 100644
--- a/templates/summary.html
+++ b/templates/summary.html
@@ -10,8 +10,11 @@
td { padding: 0 5px; }
.line-number { background-color: #eeeeee; text-align: right; font-family: monospace; }
- .novel-lhs { background-color: rgba(200, 0, 0, 0.25); }
- .novel-rhs { background-color: rgba(0, 200, 0, 0.25); }
+ .novel-lhs-line { background-color: rgba(200, 100, 100, 0.25); }
+ .novel-rhs-line { background-color: rgba(100, 200, 100, 0.25); }
+
+ .novel-lhs { background-color: rgba(200, 100, 100, 0.3); }
+ .novel-rhs { background-color: rgba(100, 200, 100, 0.3); }
.comment { color: blue; }
.keyword { font-weight: bold; }
@@ -164,9 +167,15 @@
{%- when Some with (num_content) -%}
<td class="line-number">{{num_content.0.one_indexed()}}</td>
- <td><pre>{%- for part in num_content.1 -%}
+ {%- if lhs_lines_with_novel.contains(num_content.0) -%}
+ <td class="novel-lhs-line">
+ {% else %}
+ <td>
+ {%- endif -%}
+ <pre>{%- for part in num_content.1 -%}
<span class="{{part.1.join(" ")}}">{{- part.0 -}}</span>
- {%- endfor -%}&nbsp;</pre></td>
+ {%- endfor -%}&nbsp;</pre>
+ </td>
{% when None %}
<td class="line-number"></td>
@@ -177,9 +186,15 @@
{%- when Some with (num_content) -%}
<td class="line-number">{{num_content.0.one_indexed()}}</td>
- <td><pre>{%- for part in num_content.1 -%}
+ {%- if rhs_lines_with_novel.contains(num_content.0) -%}
+ <td class="novel-rhs-line">
+ {% else %}
+ <td>
+ {%- endif -%}
+ <pre>{%- for part in num_content.1 -%}
<span class="{{part.1.join(" ")}}">{{- part.0 -}}</span>
- {%- endfor -%}&nbsp;</pre></td>
+ {%- endfor -%}&nbsp;</pre>
+ </td>
{% when None %}
<td class="line-number"></td>