Use bold for highlighting types too

Too many colours makes it harder to spot the red/green changed
sections, but it's nice seeing types distinctly.

This is probably the limit of syntax highlighting possible within the
current design, so consider #32 done for the time being.

Fixes #32
better_inline
Wilfred Hughes 2022-01-23 11:53:37 +07:00
parent d594901d29
commit f053529f76
4 changed files with 34 additions and 4 deletions

@ -6,7 +6,7 @@ Improved performance when all file changes are close together.
### Display ### Display
Added syntax highlighting for unchanged comments. Added syntax highlighting for unchanged comments, strings and types.
## 0.16 (released 22 January 2022) ## 0.16 (released 22 January 2022)

@ -203,7 +203,11 @@ pub fn color_positions(is_lhs: bool, positions: &[MatchedPos]) -> Vec<(SingleLin
_ => Color::White, _ => Color::White,
}, },
background: None, background: None,
bold: highlight == TokenKind::Atom(AtomKind::Keyword), bold: match highlight {
TokenKind::Atom(AtomKind::Keyword) => true,
TokenKind::Atom(AtomKind::Type) => true,
_ => false,
},
dimmed: false, dimmed: false,
}, },
MatchKind::Novel { highlight, .. } => Style { MatchKind::Novel { highlight, .. } => Style {

@ -507,6 +507,7 @@ impl<'a> Hash for Syntax<'a> {
pub enum AtomKind { pub enum AtomKind {
Normal, Normal,
String, String,
Type,
Comment, Comment,
Keyword, Keyword,
} }

@ -451,18 +451,39 @@ pub fn parse_to_tree(
string_capture_ids.push(idx); string_capture_ids.push(idx);
} }
let mut type_capture_ids = vec![];
if let Some(idx) = config.highlight_query.capture_index_for_name("type") {
type_capture_ids.push(idx);
}
if let Some(idx) = config
.highlight_query
.capture_index_for_name("type.builtin")
{
type_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("label") {
// Rust uses 'label' for lifetimes, and highglighting
// lifetimes consistently with types seems reasonable.
type_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("tag") {
type_capture_ids.push(idx);
}
let mut qc = ts::QueryCursor::new(); let mut qc = ts::QueryCursor::new();
let q_matches = qc.matches(&config.highlight_query, tree.root_node(), src.as_bytes()); let q_matches = qc.matches(&config.highlight_query, tree.root_node(), src.as_bytes());
let mut keyword_ids = HashSet::new(); let mut keyword_ids = HashSet::new();
let mut string_ids = HashSet::new(); let mut string_ids = HashSet::new();
let mut type_ids = HashSet::new();
for m in q_matches { for m in q_matches {
for c in m.captures { for c in m.captures {
if keyword_ish_capture_ids.contains(&c.index) { if keyword_ish_capture_ids.contains(&c.index) {
keyword_ids.insert(c.node.id()); keyword_ids.insert(c.node.id());
} } else if string_capture_ids.contains(&c.index) {
if string_capture_ids.contains(&c.index) {
string_ids.insert(c.node.id()); string_ids.insert(c.node.id());
} else if type_capture_ids.contains(&c.index) {
type_ids.insert(c.node.id());
} }
} }
} }
@ -470,6 +491,7 @@ pub fn parse_to_tree(
let highlights = HighlightedNodeIds { let highlights = HighlightedNodeIds {
keyword_ids, keyword_ids,
string_ids, string_ids,
type_ids,
}; };
(tree, highlights) (tree, highlights)
} }
@ -578,6 +600,7 @@ fn find_delim_positions(
pub struct HighlightedNodeIds { pub struct HighlightedNodeIds {
keyword_ids: HashSet<usize>, keyword_ids: HashSet<usize>,
string_ids: HashSet<usize>, string_ids: HashSet<usize>,
type_ids: HashSet<usize>,
} }
/// Convert all the tree-sitter nodes at this level to difftastic /// Convert all the tree-sitter nodes at this level to difftastic
@ -779,6 +802,8 @@ fn atom_from_cursor<'a>(
AtomKind::Keyword AtomKind::Keyword
} else if highlights.string_ids.contains(&node.id()) { } else if highlights.string_ids.contains(&node.id()) {
AtomKind::String AtomKind::String
} else if highlights.type_ids.contains(&node.id()) {
AtomKind::Type
} else { } else {
AtomKind::Normal AtomKind::Normal
}; };