diff --git a/CHANGELOG.md b/CHANGELOG.md index 589cfc9fa..4a36c3a34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ Improved performance when all file changes are close together. ### Display -Added syntax highlighting for unchanged comments. +Added syntax highlighting for unchanged comments, strings and types. ## 0.16 (released 22 January 2022) diff --git a/src/style.rs b/src/style.rs index bccf49f75..34ead9f13 100644 --- a/src/style.rs +++ b/src/style.rs @@ -203,7 +203,11 @@ pub fn color_positions(is_lhs: bool, positions: &[MatchedPos]) -> Vec<(SingleLin _ => Color::White, }, background: None, - bold: highlight == TokenKind::Atom(AtomKind::Keyword), + bold: match highlight { + TokenKind::Atom(AtomKind::Keyword) => true, + TokenKind::Atom(AtomKind::Type) => true, + _ => false, + }, dimmed: false, }, MatchKind::Novel { highlight, .. } => Style { diff --git a/src/syntax.rs b/src/syntax.rs index abbced75d..e22a03010 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -507,6 +507,7 @@ impl<'a> Hash for Syntax<'a> { pub enum AtomKind { Normal, String, + Type, Comment, Keyword, } diff --git a/src/tree_sitter_parser.rs b/src/tree_sitter_parser.rs index a77b7df1d..ab061fa08 100644 --- a/src/tree_sitter_parser.rs +++ b/src/tree_sitter_parser.rs @@ -451,18 +451,39 @@ pub fn parse_to_tree( 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 q_matches = qc.matches(&config.highlight_query, tree.root_node(), src.as_bytes()); let mut keyword_ids = HashSet::new(); let mut string_ids = HashSet::new(); + let mut type_ids = HashSet::new(); for m in q_matches { for c in m.captures { if keyword_ish_capture_ids.contains(&c.index) { keyword_ids.insert(c.node.id()); - } - if string_capture_ids.contains(&c.index) { + } else if string_capture_ids.contains(&c.index) { 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 { keyword_ids, string_ids, + type_ids, }; (tree, highlights) } @@ -578,6 +600,7 @@ fn find_delim_positions( pub struct HighlightedNodeIds { keyword_ids: HashSet, string_ids: HashSet, + type_ids: HashSet, } /// Convert all the tree-sitter nodes at this level to difftastic @@ -779,6 +802,8 @@ fn atom_from_cursor<'a>( AtomKind::Keyword } else if highlights.string_ids.contains(&node.id()) { AtomKind::String + } else if highlights.type_ids.contains(&node.id()) { + AtomKind::Type } else { AtomKind::Normal };