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
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.

@ -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<Color>,
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 {

@ -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<SingleLineSpan>, Vec<SingleLineSpan>),
},
Novel,
@ -576,6 +583,7 @@ fn split_comment_words(
impl MatchedPos {
fn new(
ck: ChangeKind,
is_comment: bool,
pos: Vec<SingleLineSpan>,
prev_opposite_pos: Vec<SingleLineSpan>,
) -> Vec<Self> {
@ -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(),
));