Merge pull request #2550 from amaanq/self-parent-set

fix(injections): only allow setting self/parent via `#set!`
This commit is contained in:
Amaan Qureshi 2023-08-22 02:17:33 -04:00 committed by GitHub
commit 49c35b3f93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View file

@ -362,9 +362,18 @@ All of these examples can be modeled in terms of a *parent* syntax tree and one
The language injection behavior can also be configured by some properties associated with patterns:
* `injection.language` - can be used to hard-code the name of a specific language.
* `injection.combined` - indicates that *all* of the matching nodes in the tree should have their content parsed as *one* nested document.
* `injection.include-children` - indicates that the `@injection.content` node's *entire* text should be re-parsed, including the text of its child nodes. By default, child nodes' text will be *excluded* from the injected document.
* `injection.self` - indicates that the `@injection.content` node should be parsed using the same language as the parent node. This is useful for cases where the parent node's language is not known until runtime (e.g. via inheriting another language)
* `injection.combined` - indicates that *all* of the matching nodes in the tree
should have their content parsed as *one* nested document.
* `injection.include-children` - indicates that the `@injection.content` node's
*entire* text should be re-parsed, including the text of its child nodes. By default,
child nodes' text will be *excluded* from the injected document.
* `injection.self` - indicates that the `@injection.content` node should be parsed
using the same language as the node itself. This is useful for cases where the
node's language is not known until runtime (e.g. via inheriting another language)
* `injection.parent` indicates that the `@injection.content` node should be parsed
using the same language as the node's parent language. This is only meant for injections
that need to refer back to the parent language to parse the node's text inside
the injected language.
#### Examples

View file

@ -113,8 +113,6 @@ pub struct HighlightConfiguration {
non_local_variable_patterns: Vec<bool>,
injection_content_capture_index: Option<u32>,
injection_language_capture_index: Option<u32>,
injection_parent_capture_index: Option<u32>,
injection_self_capture_index: Option<u32>,
local_scope_capture_index: Option<u32>,
local_def_capture_index: Option<u32>,
local_def_value_capture_index: Option<u32>,
@ -315,8 +313,6 @@ impl HighlightConfiguration {
// Store the numeric ids for all of the special captures.
let mut injection_content_capture_index = None;
let mut injection_language_capture_index = None;
let mut injection_parent_capture_index = None;
let mut injection_self_capture_index = None;
let mut local_def_capture_index = None;
let mut local_def_value_capture_index = None;
let mut local_ref_capture_index = None;
@ -326,8 +322,6 @@ impl HighlightConfiguration {
match name.as_str() {
"injection.content" => injection_content_capture_index = i,
"injection.language" => injection_language_capture_index = i,
"injection.parent" => injection_parent_capture_index = i,
"injection.self" => injection_self_capture_index = i,
"local.definition" => local_def_capture_index = i,
"local.definition-value" => local_def_value_capture_index = i,
"local.reference" => local_ref_capture_index = i,
@ -349,8 +343,6 @@ impl HighlightConfiguration {
non_local_variable_patterns,
injection_content_capture_index,
injection_language_capture_index,
injection_parent_capture_index,
injection_self_capture_index,
local_def_capture_index,
local_def_value_capture_index,
local_ref_capture_index,
@ -1145,12 +1137,9 @@ fn injection_for_match<'a>(
) -> (Option<&'a str>, Option<Node<'a>>, bool) {
let content_capture_index = config.injection_content_capture_index;
let language_capture_index = config.injection_language_capture_index;
let parent_capture_index = config.injection_parent_capture_index;
let self_capture_index = config.injection_self_capture_index;
let mut language_name = None;
let mut content_node = None;
let parent_name = parent_name.unwrap_or_default();
for capture in query_match.captures {
let index = Some(capture.index);
@ -1158,14 +1147,6 @@ fn injection_for_match<'a>(
language_name = capture.node.utf8_text(source).ok();
} else if index == content_capture_index {
content_node = Some(capture.node);
} else if index == parent_capture_index && !parent_name.is_empty() {
language_name = Some(parent_name);
content_node = Some(capture.node);
} else if index == self_capture_index {
if let Ok(name) = capture.node.utf8_text(source) {
language_name = Some(name);
content_node = Some(capture.node);
}
}
}
@ -1190,6 +1171,15 @@ fn injection_for_match<'a>(
}
}
// Setting the `injection.parent` key can be used to specify that
// the language name should be the same as the language of the
// parent layer
"injection.parent" => {
if language_name.is_none() {
language_name = parent_name;
}
}
// By default, injections do not include the *children* of an
// `injection.content` node - only the ranges that belong to the
// node itself. This can be changed using a `#set!` predicate that