Skip to content

Commit

Permalink
Use standard injection.language and injection.content captures (#22268)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
uncenter and MrSubidubi authored Jan 7, 2025
1 parent f3e75d8 commit d58f006
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 159 deletions.
8 changes: 4 additions & 4 deletions crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5962,8 +5962,8 @@ async fn test_autoclose_with_embedded_language(cx: &mut gpui::TestAppContext) {
.with_injection_query(
r#"
(script_element
(raw_text) @content
(#set! "language" "javascript"))
(raw_text) @injection.content
(#set! injection.language "javascript"))
"#,
)
.unwrap(),
Expand Down Expand Up @@ -9068,8 +9068,8 @@ async fn test_toggle_block_comment(cx: &mut gpui::TestAppContext) {
.with_injection_query(
r#"
(script_element
(raw_text) @content
(#set! "language" "javascript"))
(raw_text) @injection.content
(#set! injection.language "javascript"))
"#,
)
.unwrap(),
Expand Down
24 changes: 12 additions & 12 deletions crates/language/src/buffer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
)
"#,
)
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 24 additions & 2 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
_ => {}
Expand Down
26 changes: 13 additions & 13 deletions crates/language/src/syntax_map/syntax_map_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
"#,
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions crates/languages/src/c/injections.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(preproc_def
value: (preproc_arg) @content
(#set! "language" "c"))
value: (preproc_arg) @injection.content
(#set! injection.language "c"))

(preproc_function_def
value: (preproc_arg) @content
(#set! "language" "c"))
value: (preproc_arg) @injection.content
(#set! injection.language "c"))
12 changes: 6 additions & 6 deletions crates/languages/src/cpp/injections.scm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(preproc_def
value: (preproc_arg) @content
(#set! "language" "c++"))
value: (preproc_arg) @injection.content
(#set! injection.language "c++"))

(preproc_function_def
value: (preproc_arg) @content
(#set! "language" "c++"))
value: (preproc_arg) @injection.content
(#set! injection.language "c++"))

(raw_string_literal
delimiter: (raw_string_delimiter) @language
(raw_string_content) @content)
delimiter: (raw_string_delimiter) @injection.language
(raw_string_content) @injection.content)
4 changes: 2 additions & 2 deletions crates/languages/src/go/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
[
(raw_string_literal)
(interpreted_string_literal)
] @content
(#set! "language" "regex")))
] @injection.content
(#set! injection.language "regex")))
44 changes: 22 additions & 22 deletions crates/languages/src/javascript/injections.scm
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
(((comment) @_jsdoc_comment
(#match? @_jsdoc_comment "(?s)^/[*][*][^*].*[*]/$")) @content
(#set! "language" "jsdoc"))
(#match? @_jsdoc_comment "(?s)^/[*][*][^*].*[*]/$")) @injection.content
(#set! injection.language "jsdoc"))

((regex) @content
(#set! "language" "regex"))
((regex) @injection.content
(#set! injection.language "regex"))

(call_expression
function: (identifier) @_name (#eq? @_name "css")
arguments: (template_string (string_fragment) @content
(#set! "language" "css"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "css"))
)

(call_expression
function: (identifier) @_name (#eq? @_name "html")
arguments: (template_string) @content
(#set! "language" "html")
arguments: (template_string) @injection.content
(#set! injection.language "html")
)

(call_expression
function: (identifier) @_name (#eq? @_name "js")
arguments: (template_string (string_fragment) @content
(#set! "language" "javascript"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "javascript"))
)

(call_expression
function: (identifier) @_name (#eq? @_name "json")
arguments: (template_string (string_fragment) @content
(#set! "language" "json"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "json"))
)

(call_expression
function: (identifier) @_name (#eq? @_name "sql")
arguments: (template_string (string_fragment) @content
(#set! "language" "sql"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "sql"))
)

(call_expression
function: (identifier) @_name (#eq? @_name "ts")
arguments: (template_string (string_fragment) @content
(#set! "language" "typescript"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "typescript"))
)

(call_expression
function: (identifier) @_name (#match? @_name "^ya?ml$")
arguments: (template_string (string_fragment) @content
(#set! "language" "yaml"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "yaml"))
)

(call_expression
function: (identifier) @_name (#match? @_name "^g(raph)?ql$")
arguments: (template_string (string_fragment) @content
(#set! "language" "graphql"))
arguments: (template_string (string_fragment) @injection.content
(#set! injection.language "graphql"))
)

(call_expression
function: (identifier) @_name (#match? @_name "^g(raph)?ql$")
arguments: (arguments (template_string (string_fragment) @content
(#set! "language" "graphql")))
arguments: (arguments (template_string (string_fragment) @injection.content
(#set! injection.language "graphql")))
)
16 changes: 8 additions & 8 deletions crates/languages/src/markdown/injections.scm
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(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"))

((html_block) @content
(#set! "language" "html"))
((html_block) @injection.content
(#set! injection.language "html"))

((minus_metadata) @content (#set! "language" "yaml"))
((minus_metadata) @injection.content (#set! injection.language "yaml"))

((plus_metadata) @content (#set! "language" "toml"))
((plus_metadata) @injection.content (#set! injection.language "toml"))
8 changes: 4 additions & 4 deletions crates/languages/src/rust/injections.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(macro_invocation
(token_tree) @content
(#set! "language" "rust"))
(token_tree) @injection.content
(#set! injection.language "rust"))

(macro_rule
(token_tree) @content
(#set! "language" "rust"))
(token_tree) @injection.content
(#set! injection.language "rust"))
Loading

0 comments on commit d58f006

Please sign in to comment.