Skip to content

Commit a91a3e5

Browse files
committed
Handle namespaced highlighting queries
1 parent aa14b60 commit a91a3e5

File tree

3 files changed

+43
-66
lines changed

3 files changed

+43
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Improved performance in large files when changes are clustered together.
1515

1616
### Display
1717

18-
Improved syntax highlighting for keywords.
18+
Improved syntax highlighting.
1919

2020
Tabs are now rendered with eight spaces.
2121

sample_files/compare.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sample_files/elisp_contiguous_before.el sample_files/elisp_contiguous_after.el
3232
e3946aef566a707c718edd7a86340566 -
3333

3434
sample_files/elm_before.elm sample_files/elm_after.elm
35-
351dd2132fe40414d0b46b5b9380f0fb -
35+
c1ea9f99a815b2ae5ce2a7d58fb65368 -
3636

3737
sample_files/hack_before.php sample_files/hack_after.php
3838
b8c51005df7e1eaaeaf738a4353ac88f -

src/tree_sitter_parser.rs

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -648,74 +648,51 @@ fn tree_highlights(
648648
src: &str,
649649
config: &TreeSitterConfig,
650650
) -> HighlightedNodeIds {
651-
let mut keyword_ish_capture_ids = vec![];
652-
// TODO: Use config.highlight_query.capture_names() to find all
653-
// the keyword.foo captures.
654-
if let Some(idx) = config.highlight_query.capture_index_for_name("keyword") {
655-
keyword_ish_capture_ids.push(idx);
656-
}
657-
if let Some(idx) = config
658-
.highlight_query
659-
.capture_index_for_name("keyword.function")
660-
{
661-
keyword_ish_capture_ids.push(idx);
662-
}
663-
if let Some(idx) = config
664-
.highlight_query
665-
.capture_index_for_name("keyword.operator")
666-
{
667-
keyword_ish_capture_ids.push(idx);
668-
}
669-
if let Some(idx) = config
670-
.highlight_query
671-
.capture_index_for_name("keyword.return")
672-
{
673-
keyword_ish_capture_ids.push(idx);
674-
}
675-
if let Some(idx) = config.highlight_query.capture_index_for_name("operator") {
676-
keyword_ish_capture_ids.push(idx);
677-
}
678-
if let Some(idx) = config.highlight_query.capture_index_for_name("repeat") {
679-
keyword_ish_capture_ids.push(idx);
680-
}
681-
if let Some(idx) = config.highlight_query.capture_index_for_name("constant") {
682-
keyword_ish_capture_ids.push(idx);
683-
}
684-
if let Some(idx) = config.highlight_query.capture_index_for_name("boolean") {
685-
keyword_ish_capture_ids.push(idx);
686-
}
687-
if let Some(idx) = config
688-
.highlight_query
689-
.capture_index_for_name("constant.builtin")
690-
{
691-
keyword_ish_capture_ids.push(idx);
692-
}
693-
651+
let mut keyword_ish_capture_ids: Vec<u32> = vec![];
694652
let mut string_capture_ids = vec![];
695-
if let Some(idx) = config.highlight_query.capture_index_for_name("string") {
696-
string_capture_ids.push(idx);
697-
}
698-
if let Some(idx) = config.highlight_query.capture_index_for_name("character") {
699-
string_capture_ids.push(idx);
700-
}
701-
702653
let mut type_capture_ids = vec![];
703-
if let Some(idx) = config.highlight_query.capture_index_for_name("type") {
704-
type_capture_ids.push(idx);
705-
}
706-
if let Some(idx) = config
707-
.highlight_query
708-
.capture_index_for_name("type.builtin")
709-
{
710-
type_capture_ids.push(idx);
711-
}
712-
if let Some(idx) = config.highlight_query.capture_index_for_name("label") {
654+
655+
// Query names are often written with namespacing, so
656+
// highlights.scm might contain @constant or the more specific
657+
// @constant.builtin.
658+
//
659+
// We support e.g. arbitrary @constant.foo so we get the benefit
660+
// of all the relevant highlighting queries.
661+
let cn = config.highlight_query.capture_names();
662+
for (idx, name) in cn.iter().enumerate() {
663+
if name == "type"
664+
|| name.starts_with("type.")
665+
|| name.starts_with("storage.type.")
666+
|| name.starts_with("keyword.type.")
667+
|| name == "tag"
668+
{
669+
// TODO: this doesn't capture (type_ref) in Elm as that
670+
// applies to the parent node.
671+
type_capture_ids.push(idx as u32);
672+
} else if name == "keyword"
673+
|| name.starts_with("keyword.")
674+
|| name == "constant"
675+
|| name.starts_with("constant.")
676+
|| name == "operator"
677+
|| name == "repeat"
678+
|| name == "boolean"
679+
{
680+
keyword_ish_capture_ids.push(idx as u32);
681+
}
682+
683+
if name == "string"
684+
|| name.starts_with("string.")
685+
|| name == "character"
686+
|| name.starts_with("character.")
687+
{
688+
string_capture_ids.push(idx as u32);
689+
}
690+
713691
// Rust uses 'label' for lifetimes, and highglighting
714692
// lifetimes consistently with types seems reasonable.
715-
type_capture_ids.push(idx);
716-
}
717-
if let Some(idx) = config.highlight_query.capture_index_for_name("tag") {
718-
type_capture_ids.push(idx);
693+
if name == "label" {
694+
type_capture_ids.push(idx as u32);
695+
}
719696
}
720697

721698
let mut qc = ts::QueryCursor::new();

0 commit comments

Comments
 (0)