diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb52ca36..818cb7fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ for instructions. ### Display +Unchanged comments are now dimmed, to distinguish them from other +unchanged code. + Characters that don't have a position in the parsed syntax tree are now displayed in purple, to make bugs more obvious. Previously they were dimmed. diff --git a/src/style.rs b/src/style.rs index c83399cf1..f34cdcc84 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,10 +1,6 @@ //! Apply colours and styling to strings. -use crate::{ - lines::{codepoint_len, substring_by_codepoint, LineNumber}, - positions::SingleLineSpan, - syntax::{MatchKind, MatchedPos}, -}; +use crate::{lines::{codepoint_len, substring_by_codepoint, LineNumber}, positions::SingleLineSpan, syntax::{HighlightKind, MatchKind, MatchedPos}}; use colored::*; use std::{cmp::min, collections::HashMap}; @@ -13,6 +9,7 @@ struct Style { foreground: Color, background: Option, bold: bool, + dimmed: bool, } impl Style { @@ -21,6 +18,9 @@ impl Style { if self.bold { res = res.bold(); } + if self.dimmed { + res = res.dimmed(); + } if let Some(background) = self.background { res = res.on_color(background); }; @@ -99,10 +99,11 @@ pub fn apply_colors(s: &str, is_lhs: bool, positions: &[MatchedPos]) -> String { let mut styles = vec![]; for pos in positions { let style = match pos.kind { - MatchKind::Unchanged { .. } => Style { + MatchKind::Unchanged { highlight, .. } => Style { foreground: Color::White, background: None, bold: false, + dimmed: highlight == HighlightKind::Comment, }, MatchKind::Novel | MatchKind::ChangedCommentPart => Style { foreground: if is_lhs { @@ -112,11 +113,13 @@ pub fn apply_colors(s: &str, is_lhs: bool, positions: &[MatchedPos]) -> String { }, background: None, bold: true, + dimmed: false, }, MatchKind::UnchangedCommentPart { .. } => Style { foreground: if is_lhs { Color::Red } else { Color::Green }, background: None, bold: false, + dimmed: false, }, }; for line_pos in &pos.pos { diff --git a/src/syntax.rs b/src/syntax.rs index b821f9b4d..1ce8731c1 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -476,9 +476,16 @@ impl<'a> Hash for Syntax<'a> { } } +#[derive(PartialEq, Eq, Debug, Clone, Copy)] +pub enum HighlightKind { + Normal, + Comment, +} + #[derive(PartialEq, Eq, Debug, Clone)] pub enum MatchKind { Unchanged { + highlight: HighlightKind, opposite_pos: (Vec, Vec), }, Novel, @@ -576,6 +583,7 @@ fn split_comment_words( impl MatchedPos { fn new( ck: ChangeKind, + is_comment: bool, pos: Vec, prev_opposite_pos: Vec, ) -> Vec { @@ -610,7 +618,14 @@ impl MatchedPos { Atom { position, .. } => (position.clone(), position.clone()), }; - MatchKind::Unchanged { opposite_pos } + MatchKind::Unchanged { + highlight: if is_comment { + HighlightKind::Comment + } else { + HighlightKind::Normal + }, + opposite_pos, + } } Novel => MatchKind::Novel, }; @@ -683,6 +698,7 @@ fn change_positions_<'a>( positions.extend(MatchedPos::new( change, + false, open_position.clone(), prev_opposite_pos.clone(), )); @@ -708,11 +724,17 @@ fn change_positions_<'a>( } positions.extend(MatchedPos::new( change, + false, close_position.clone(), prev_opposite_pos.clone(), )); } - Atom { info, position, .. } => { + Atom { + info, + position, + is_comment, + .. + } => { let change = info .change .get() @@ -733,6 +755,7 @@ fn change_positions_<'a>( } positions.extend(MatchedPos::new( change, + *is_comment, position.clone(), prev_opposite_pos.clone(), ));