Dim unchanged comments as a crude syntax highlighting technique

pull/41/head
Wilfred Hughes 2021-09-30 00:07:37 +07:00
parent b747cdcb01
commit 67916d260d
3 changed files with 37 additions and 8 deletions

@ -12,6 +12,9 @@ for instructions.
### Display ### 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 Characters that don't have a position in the parsed syntax tree are
now displayed in purple, to make bugs more obvious. Previously they now displayed in purple, to make bugs more obvious. Previously they
were dimmed. were dimmed.

@ -1,10 +1,6 @@
//! Apply colours and styling to strings. //! Apply colours and styling to strings.
use crate::{ use crate::{lines::{codepoint_len, substring_by_codepoint, LineNumber}, positions::SingleLineSpan, syntax::{HighlightKind, MatchKind, MatchedPos}};
lines::{codepoint_len, substring_by_codepoint, LineNumber},
positions::SingleLineSpan,
syntax::{MatchKind, MatchedPos},
};
use colored::*; use colored::*;
use std::{cmp::min, collections::HashMap}; use std::{cmp::min, collections::HashMap};
@ -13,6 +9,7 @@ struct Style {
foreground: Color, foreground: Color,
background: Option<Color>, background: Option<Color>,
bold: bool, bold: bool,
dimmed: bool,
} }
impl Style { impl Style {
@ -21,6 +18,9 @@ impl Style {
if self.bold { if self.bold {
res = res.bold(); res = res.bold();
} }
if self.dimmed {
res = res.dimmed();
}
if let Some(background) = self.background { if let Some(background) = self.background {
res = res.on_color(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![]; let mut styles = vec![];
for pos in positions { for pos in positions {
let style = match pos.kind { let style = match pos.kind {
MatchKind::Unchanged { .. } => Style { MatchKind::Unchanged { highlight, .. } => Style {
foreground: Color::White, foreground: Color::White,
background: None, background: None,
bold: false, bold: false,
dimmed: highlight == HighlightKind::Comment,
}, },
MatchKind::Novel | MatchKind::ChangedCommentPart => Style { MatchKind::Novel | MatchKind::ChangedCommentPart => Style {
foreground: if is_lhs { foreground: if is_lhs {
@ -112,11 +113,13 @@ pub fn apply_colors(s: &str, is_lhs: bool, positions: &[MatchedPos]) -> String {
}, },
background: None, background: None,
bold: true, bold: true,
dimmed: false,
}, },
MatchKind::UnchangedCommentPart { .. } => Style { MatchKind::UnchangedCommentPart { .. } => Style {
foreground: if is_lhs { Color::Red } else { Color::Green }, foreground: if is_lhs { Color::Red } else { Color::Green },
background: None, background: None,
bold: false, bold: false,
dimmed: false,
}, },
}; };
for line_pos in &pos.pos { for line_pos in &pos.pos {

@ -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)] #[derive(PartialEq, Eq, Debug, Clone)]
pub enum MatchKind { pub enum MatchKind {
Unchanged { Unchanged {
highlight: HighlightKind,
opposite_pos: (Vec<SingleLineSpan>, Vec<SingleLineSpan>), opposite_pos: (Vec<SingleLineSpan>, Vec<SingleLineSpan>),
}, },
Novel, Novel,
@ -576,6 +583,7 @@ fn split_comment_words(
impl MatchedPos { impl MatchedPos {
fn new( fn new(
ck: ChangeKind, ck: ChangeKind,
is_comment: bool,
pos: Vec<SingleLineSpan>, pos: Vec<SingleLineSpan>,
prev_opposite_pos: Vec<SingleLineSpan>, prev_opposite_pos: Vec<SingleLineSpan>,
) -> Vec<Self> { ) -> Vec<Self> {
@ -610,7 +618,14 @@ impl MatchedPos {
Atom { position, .. } => (position.clone(), position.clone()), 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, Novel => MatchKind::Novel,
}; };
@ -683,6 +698,7 @@ fn change_positions_<'a>(
positions.extend(MatchedPos::new( positions.extend(MatchedPos::new(
change, change,
false,
open_position.clone(), open_position.clone(),
prev_opposite_pos.clone(), prev_opposite_pos.clone(),
)); ));
@ -708,11 +724,17 @@ fn change_positions_<'a>(
} }
positions.extend(MatchedPos::new( positions.extend(MatchedPos::new(
change, change,
false,
close_position.clone(), close_position.clone(),
prev_opposite_pos.clone(), prev_opposite_pos.clone(),
)); ));
} }
Atom { info, position, .. } => { Atom {
info,
position,
is_comment,
..
} => {
let change = info let change = info
.change .change
.get() .get()
@ -733,6 +755,7 @@ fn change_positions_<'a>(
} }
positions.extend(MatchedPos::new( positions.extend(MatchedPos::new(
change, change,
*is_comment,
position.clone(), position.clone(),
prev_opposite_pos.clone(), prev_opposite_pos.clone(),
)); ));