Handle namespaced highlighting queries

pull/261/head 0.27.0
Wilfred Hughes 2022-04-18 09:35:09 +07:00
parent aa14b60933
commit a91a3e5db0
3 changed files with 43 additions and 66 deletions

@ -15,7 +15,7 @@ Improved performance in large files when changes are clustered together.
### Display ### Display
Improved syntax highlighting for keywords. Improved syntax highlighting.
Tabs are now rendered with eight spaces. Tabs are now rendered with eight spaces.

@ -32,7 +32,7 @@ sample_files/elisp_contiguous_before.el sample_files/elisp_contiguous_after.el
e3946aef566a707c718edd7a86340566 - e3946aef566a707c718edd7a86340566 -
sample_files/elm_before.elm sample_files/elm_after.elm sample_files/elm_before.elm sample_files/elm_after.elm
351dd2132fe40414d0b46b5b9380f0fb - c1ea9f99a815b2ae5ce2a7d58fb65368 -
sample_files/hack_before.php sample_files/hack_after.php sample_files/hack_before.php sample_files/hack_after.php
b8c51005df7e1eaaeaf738a4353ac88f - b8c51005df7e1eaaeaf738a4353ac88f -

@ -648,74 +648,51 @@ fn tree_highlights(
src: &str, src: &str,
config: &TreeSitterConfig, config: &TreeSitterConfig,
) -> HighlightedNodeIds { ) -> HighlightedNodeIds {
let mut keyword_ish_capture_ids = vec![]; let mut keyword_ish_capture_ids: Vec<u32> = vec![];
// TODO: Use config.highlight_query.capture_names() to find all
// the keyword.foo captures.
if let Some(idx) = config.highlight_query.capture_index_for_name("keyword") {
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config
.highlight_query
.capture_index_for_name("keyword.function")
{
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config
.highlight_query
.capture_index_for_name("keyword.operator")
{
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config
.highlight_query
.capture_index_for_name("keyword.return")
{
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("operator") {
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("repeat") {
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("constant") {
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("boolean") {
keyword_ish_capture_ids.push(idx);
}
if let Some(idx) = config
.highlight_query
.capture_index_for_name("constant.builtin")
{
keyword_ish_capture_ids.push(idx);
}
let mut string_capture_ids = vec![]; let mut string_capture_ids = vec![];
if let Some(idx) = config.highlight_query.capture_index_for_name("string") {
string_capture_ids.push(idx);
}
if let Some(idx) = config.highlight_query.capture_index_for_name("character") {
string_capture_ids.push(idx);
}
let mut type_capture_ids = vec![]; let mut type_capture_ids = vec![];
if let Some(idx) = config.highlight_query.capture_index_for_name("type") {
type_capture_ids.push(idx); // Query names are often written with namespacing, so
} // highlights.scm might contain @constant or the more specific
if let Some(idx) = config // @constant.builtin.
.highlight_query //
.capture_index_for_name("type.builtin") // We support e.g. arbitrary @constant.foo so we get the benefit
{ // of all the relevant highlighting queries.
type_capture_ids.push(idx); let cn = config.highlight_query.capture_names();
} for (idx, name) in cn.iter().enumerate() {
if let Some(idx) = config.highlight_query.capture_index_for_name("label") { if name == "type"
|| name.starts_with("type.")
|| name.starts_with("storage.type.")
|| name.starts_with("keyword.type.")
|| name == "tag"
{
// TODO: this doesn't capture (type_ref) in Elm as that
// applies to the parent node.
type_capture_ids.push(idx as u32);
} else if name == "keyword"
|| name.starts_with("keyword.")
|| name == "constant"
|| name.starts_with("constant.")
|| name == "operator"
|| name == "repeat"
|| name == "boolean"
{
keyword_ish_capture_ids.push(idx as u32);
}
if name == "string"
|| name.starts_with("string.")
|| name == "character"
|| name.starts_with("character.")
{
string_capture_ids.push(idx as u32);
}
// Rust uses 'label' for lifetimes, and highglighting // Rust uses 'label' for lifetimes, and highglighting
// lifetimes consistently with types seems reasonable. // lifetimes consistently with types seems reasonable.
type_capture_ids.push(idx); if name == "label" {
} type_capture_ids.push(idx as u32);
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();