Clarify enum variant NovelLinePart and expand doc comments

pull/748/head
Wilfred Hughes 2024-07-30 15:16:45 +07:00
parent 5ad8b1bb31
commit 0973998de2
5 changed files with 38 additions and 18 deletions

@ -143,7 +143,7 @@ fn matched_lines_from_unchanged(
for mp in mps { for mp in mps {
let opposite_line = match &mp.kind { let opposite_line = match &mp.kind {
MatchKind::UnchangedToken { opposite_pos, .. } MatchKind::UnchangedToken { opposite_pos, .. }
| MatchKind::NovelLinePart { opposite_pos, .. } => { | MatchKind::UnchangedPartOfNovelItem { opposite_pos, .. } => {
if let Some(highest_opposite_side) = highest_opposite_line { if let Some(highest_opposite_side) = highest_opposite_line {
opposite_pos opposite_pos
.iter() .iter()
@ -344,7 +344,7 @@ pub(crate) fn opposite_positions(
opposite_lines.insert(opposite_span.line); opposite_lines.insert(opposite_span.line);
} }
} }
MatchKind::NovelLinePart { MatchKind::UnchangedPartOfNovelItem {
opposite_pos, opposite_pos,
self_pos, self_pos,
.. ..
@ -837,7 +837,7 @@ mod tests {
fn test_all_lines() { fn test_all_lines() {
let mps = [ let mps = [
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Delimiter, highlight: TokenKind::Delimiter,
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),

@ -255,7 +255,7 @@ impl Highlight {
MatchKind::UnchangedToken { highlight, .. } => highlight, MatchKind::UnchangedToken { highlight, .. } => highlight,
MatchKind::Novel { highlight, .. } => highlight, MatchKind::Novel { highlight, .. } => highlight,
MatchKind::NovelWord { highlight, .. } => highlight, MatchKind::NovelWord { highlight, .. } => highlight,
MatchKind::NovelLinePart { highlight, .. } => highlight, MatchKind::UnchangedPartOfNovelItem { highlight, .. } => highlight,
}; };
match highlight { match highlight {

@ -393,7 +393,7 @@ pub(crate) fn color_positions(
style = style.italic(); style = style.italic();
} }
} }
MatchKind::NovelLinePart { highlight, .. } => { MatchKind::UnchangedPartOfNovelItem { highlight, .. } => {
style = novel_style(style, side, background); style = novel_style(style, side, background);
if syntax_highlight && matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { if syntax_highlight && matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic(); style = style.italic();

@ -192,7 +192,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
rhs_lp.from_region(rhs_offset, rhs_offset + rhs_word.len()); rhs_lp.from_region(rhs_offset, rhs_offset + rhs_word.len());
mps.push(MatchedPos { mps.push(MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Normal), highlight: TokenKind::Atom(AtomKind::Normal),
self_pos: lhs_pos[0], self_pos: lhs_pos[0],
opposite_pos: rhs_pos, opposite_pos: rhs_pos,

@ -598,15 +598,33 @@ pub(crate) enum MatchKind {
}, },
/// A novel token in an AST diff. /// A novel token in an AST diff.
Novel { highlight: TokenKind }, Novel { highlight: TokenKind },
/// When doing a textual line-based diff, the part of a novel line /// When we have a novel item, we often want to highlight novel
/// that exists on both sides. /// words more prominently. UnchangedPartOfNovelItem represents
NovelLinePart { /// the parts that don't get this special highlighting.
///
/// For example, line-based diffs we want to highlight `a` and `b`
/// differently to `foo` here.
///
/// foo a
/// foo b
///
/// Whereas for syntactic diffs, we want to do the same thing for
/// strings and comments.
///
/// "foo a"
/// "foo b"
///
/// The whole string is a distinct value, but the `a` and `b` are
/// the most interesting parts.
UnchangedPartOfNovelItem {
highlight: TokenKind, highlight: TokenKind,
self_pos: SingleLineSpan, self_pos: SingleLineSpan,
opposite_pos: Vec<SingleLineSpan>, opposite_pos: Vec<SingleLineSpan>,
}, },
/// When doing a textual line-based diff, the part of a novel line /// The novel part of the novel item. For line-based diffs, this
/// that is actually novel. E.g. the newly added word on a line. /// is the words that are unique to this line.
///
/// See the discussion in `UnchangedPartOfNovelItem`.
NovelWord { highlight: TokenKind }, NovelWord { highlight: TokenKind },
/// A syntactic token that was ignored by the AST diff (e.g. when /// A syntactic token that was ignored by the AST diff (e.g. when
/// ignoring comments for diffing). /// ignoring comments for diffing).
@ -617,7 +635,9 @@ impl MatchKind {
pub(crate) fn is_novel(&self) -> bool { pub(crate) fn is_novel(&self) -> bool {
matches!( matches!(
self, self,
MatchKind::Novel { .. } | MatchKind::NovelWord { .. } | MatchKind::NovelLinePart { .. } MatchKind::Novel { .. }
| MatchKind::NovelWord { .. }
| MatchKind::UnchangedPartOfNovelItem { .. }
) )
} }
} }
@ -700,7 +720,7 @@ fn split_atom_words(
); );
mps.push(MatchedPos { mps.push(MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(kind), highlight: TokenKind::Atom(kind),
self_pos: word_pos, self_pos: word_pos,
opposite_pos: opposite_word_pos, opposite_pos: opposite_word_pos,
@ -1184,7 +1204,7 @@ mod tests {
res, res,
vec![ vec![
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment), highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),
@ -1204,7 +1224,7 @@ mod tests {
} }
}, },
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment), highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),
@ -1224,7 +1244,7 @@ mod tests {
} }
}, },
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment), highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),
@ -1244,7 +1264,7 @@ mod tests {
} }
}, },
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment), highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),
@ -1264,7 +1284,7 @@ mod tests {
} }
}, },
MatchedPos { MatchedPos {
kind: MatchKind::NovelLinePart { kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment), highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan { self_pos: SingleLineSpan {
line: 0.into(), line: 0.into(),