Use standard injection.language and injection.content captures (#22268)
Closes #9656. Continuation of #9654, but with the addition of backwards compatibility for the existing captures. Release Notes: - Improved Tree-sitter support with added compatibility for standard injections captures --------- Co-authored-by: Finn Evers <finn.evers@outlook.de>
This commit is contained in:
parent
f3e75d8ff6
commit
d58f006498
21 changed files with 181 additions and 159 deletions
|
@ -3115,8 +3115,8 @@ fn html_lang() -> Language {
|
|||
.with_injection_query(
|
||||
r#"
|
||||
(script_element
|
||||
(raw_text) @content
|
||||
(#set! "language" "javascript"))
|
||||
(raw_text) @injection.content
|
||||
(#set! injection.language "javascript"))
|
||||
"#,
|
||||
)
|
||||
.unwrap()
|
||||
|
@ -3138,15 +3138,15 @@ fn erb_lang() -> Language {
|
|||
.with_injection_query(
|
||||
r#"
|
||||
(
|
||||
(code) @content
|
||||
(#set! "language" "ruby")
|
||||
(#set! "combined")
|
||||
(code) @injection.content
|
||||
(#set! injection.language "ruby")
|
||||
(#set! injection.combined)
|
||||
)
|
||||
|
||||
(
|
||||
(content) @content
|
||||
(#set! "language" "html")
|
||||
(#set! "combined")
|
||||
(content) @injection.content
|
||||
(#set! injection.language "html")
|
||||
(#set! injection.combined)
|
||||
)
|
||||
"#,
|
||||
)
|
||||
|
@ -3278,11 +3278,11 @@ pub fn markdown_lang() -> Language {
|
|||
r#"
|
||||
(fenced_code_block
|
||||
(info_string
|
||||
(language) @language)
|
||||
(code_fence_content) @content)
|
||||
(language) @injection.language)
|
||||
(code_fence_content) @injection.content)
|
||||
|
||||
((inline) @content
|
||||
(#set! "language" "markdown-inline"))
|
||||
((inline) @injection.content
|
||||
(#set! injection.language "markdown-inline"))
|
||||
"#,
|
||||
)
|
||||
.unwrap()
|
||||
|
|
|
@ -1273,23 +1273,45 @@ impl Language {
|
|||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut language_capture_ix = None;
|
||||
let mut injection_language_capture_ix = None;
|
||||
let mut content_capture_ix = None;
|
||||
let mut injection_content_capture_ix = None;
|
||||
get_capture_indices(
|
||||
&query,
|
||||
&mut [
|
||||
("language", &mut language_capture_ix),
|
||||
("injection.language", &mut injection_language_capture_ix),
|
||||
("content", &mut content_capture_ix),
|
||||
("injection.content", &mut injection_content_capture_ix),
|
||||
],
|
||||
);
|
||||
language_capture_ix = match (language_capture_ix, injection_language_capture_ix) {
|
||||
(None, Some(ix)) => Some(ix),
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(anyhow!(
|
||||
"both language and injection.language captures are present"
|
||||
));
|
||||
}
|
||||
_ => language_capture_ix,
|
||||
};
|
||||
content_capture_ix = match (content_capture_ix, injection_content_capture_ix) {
|
||||
(None, Some(ix)) => Some(ix),
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(anyhow!(
|
||||
"both content and injection.content captures are present"
|
||||
));
|
||||
}
|
||||
_ => content_capture_ix,
|
||||
};
|
||||
let patterns = (0..query.pattern_count())
|
||||
.map(|ix| {
|
||||
let mut config = InjectionPatternConfig::default();
|
||||
for setting in query.property_settings(ix) {
|
||||
match setting.key.as_ref() {
|
||||
"language" => {
|
||||
"language" | "injection.language" => {
|
||||
config.language.clone_from(&setting.value);
|
||||
}
|
||||
"combined" => {
|
||||
"combined" | "injection.combined" => {
|
||||
config.combined = true;
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -1193,15 +1193,15 @@ fn erb_lang() -> Language {
|
|||
.with_injection_query(
|
||||
r#"
|
||||
(
|
||||
(code) @content
|
||||
(#set! "language" "ruby")
|
||||
(#set! "combined")
|
||||
(code) @injection.content
|
||||
(#set! injection.language "ruby")
|
||||
(#set! injection.combined)
|
||||
)
|
||||
|
||||
(
|
||||
(content) @content
|
||||
(#set! "language" "html")
|
||||
(#set! "combined")
|
||||
(content) @injection.content
|
||||
(#set! injection.language "html")
|
||||
(#set! injection.combined)
|
||||
)
|
||||
"#,
|
||||
)
|
||||
|
@ -1230,8 +1230,8 @@ fn rust_lang() -> Language {
|
|||
.with_injection_query(
|
||||
r#"
|
||||
(macro_invocation
|
||||
(token_tree) @content
|
||||
(#set! "language" "rust"))
|
||||
(token_tree) @injection.content
|
||||
(#set! injection.language "rust"))
|
||||
"#,
|
||||
)
|
||||
.unwrap()
|
||||
|
@ -1277,13 +1277,13 @@ fn heex_lang() -> Language {
|
|||
(partial_expression_value)
|
||||
(expression_value)
|
||||
(ending_expression_value)
|
||||
] @content)
|
||||
(#set! language "elixir")
|
||||
(#set! combined)
|
||||
] @injection.content)
|
||||
(#set! injection.language "elixir")
|
||||
(#set! injection.combined)
|
||||
)
|
||||
|
||||
((expression (expression_value) @content)
|
||||
(#set! language "elixir"))
|
||||
((expression (expression_value) @injection.content)
|
||||
(#set! injection.language "elixir"))
|
||||
"#,
|
||||
)
|
||||
.unwrap()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue