Skip to content

Commit

Permalink
Clarify enum variant NovelLinePart and expand doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jul 30, 2024
1 parent 5ad8b1b commit 0973998
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/display/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn matched_lines_from_unchanged(
for mp in mps {
let opposite_line = match &mp.kind {
MatchKind::UnchangedToken { opposite_pos, .. }
| MatchKind::NovelLinePart { opposite_pos, .. } => {
| MatchKind::UnchangedPartOfNovelItem { opposite_pos, .. } => {
if let Some(highest_opposite_side) = highest_opposite_line {
opposite_pos
.iter()
Expand Down Expand Up @@ -344,7 +344,7 @@ pub(crate) fn opposite_positions(
opposite_lines.insert(opposite_span.line);
}
}
MatchKind::NovelLinePart {
MatchKind::UnchangedPartOfNovelItem {
opposite_pos,
self_pos,
..
Expand Down Expand Up @@ -837,7 +837,7 @@ mod tests {
fn test_all_lines() {
let mps = [
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Delimiter,
self_pos: SingleLineSpan {
line: 0.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/display/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Highlight {
MatchKind::UnchangedToken { highlight, .. } => highlight,
MatchKind::Novel { highlight, .. } => highlight,
MatchKind::NovelWord { highlight, .. } => highlight,
MatchKind::NovelLinePart { highlight, .. } => highlight,
MatchKind::UnchangedPartOfNovelItem { highlight, .. } => highlight,
};

match highlight {
Expand Down
2 changes: 1 addition & 1 deletion src/display/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ pub(crate) fn color_positions(
style = style.italic();
}
}
MatchKind::NovelLinePart { highlight, .. } => {
MatchKind::UnchangedPartOfNovelItem { highlight, .. } => {
style = novel_style(style, side, background);
if syntax_highlight && matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic();
Expand Down
2 changes: 1 addition & 1 deletion src/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

mps.push(MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Normal),
self_pos: lhs_pos[0],
opposite_pos: rhs_pos,
Expand Down
44 changes: 32 additions & 12 deletions src/parse/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,33 @@ pub(crate) enum MatchKind {
},
/// A novel token in an AST diff.
Novel { highlight: TokenKind },
/// When doing a textual line-based diff, the part of a novel line
/// that exists on both sides.
NovelLinePart {
/// When we have a novel item, we often want to highlight novel
/// words more prominently. UnchangedPartOfNovelItem represents
/// 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,
self_pos: SingleLineSpan,
opposite_pos: Vec<SingleLineSpan>,
},
/// When doing a textual line-based diff, the part of a novel line
/// that is actually novel. E.g. the newly added word on a line.
/// The novel part of the novel item. For line-based diffs, this
/// is the words that are unique to this line.
///
/// See the discussion in `UnchangedPartOfNovelItem`.
NovelWord { highlight: TokenKind },
/// A syntactic token that was ignored by the AST diff (e.g. when
/// ignoring comments for diffing).
Expand All @@ -617,7 +635,9 @@ impl MatchKind {
pub(crate) fn is_novel(&self) -> bool {
matches!(
self,
MatchKind::Novel { .. } | MatchKind::NovelWord { .. } | MatchKind::NovelLinePart { .. }
MatchKind::Novel { .. }
| MatchKind::NovelWord { .. }
| MatchKind::UnchangedPartOfNovelItem { .. }
)
}
}
Expand Down Expand Up @@ -700,7 +720,7 @@ fn split_atom_words(
);

mps.push(MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(kind),
self_pos: word_pos,
opposite_pos: opposite_word_pos,
Expand Down Expand Up @@ -1184,7 +1204,7 @@ mod tests {
res,
vec![
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan {
line: 0.into(),
Expand All @@ -1204,7 +1224,7 @@ mod tests {
}
},
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan {
line: 0.into(),
Expand All @@ -1224,7 +1244,7 @@ mod tests {
}
},
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan {
line: 0.into(),
Expand All @@ -1244,7 +1264,7 @@ mod tests {
}
},
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan {
line: 0.into(),
Expand All @@ -1264,7 +1284,7 @@ mod tests {
}
},
MatchedPos {
kind: MatchKind::NovelLinePart {
kind: MatchKind::UnchangedPartOfNovelItem {
highlight: TokenKind::Atom(AtomKind::Comment),
self_pos: SingleLineSpan {
line: 0.into(),
Expand Down

0 comments on commit 0973998

Please sign in to comment.