mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
Template literal injection queries previously captured the entire
`template_string` node with `injection.include-children`, which caused
template substitutions (e.g. `${expr}`) to be included in the injected
language's parse input. This broke highlighting in cases like:
html`<p class="static ${classMap({ dynamic })} after"></p>`
where the HTML parser's attribute_value node would span across the
template substitution gap, and the lit-html `${` injection query
(in html_tags) would match the buffer text at that range, injecting
JS at the wrong offset.
Two fixes:
1. ecma/injections.scm: capture `string_fragment` nodes instead of
`template_string`, and use `injection.combined` to merge them. This
aligns with upstream tree-sitter-javascript's approach. The `#offset!`
directives are removed since `string_fragment` already excludes the
backticks.
2. html_tags/injections.scm: anchor the lit-html `${` pattern with `^`
so it only matches attribute values that *start* with `${`, not
attribute values whose buffer text happens to contain `${` in the
middle (which occurs when the attribute_value node spans a template
substitution gap).
Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
223 lines
6.1 KiB
Scheme
223 lines
6.1 KiB
Scheme
(((comment) @_jsdoc_comment
|
|
(#lua-match? @_jsdoc_comment "^/[*][*][^*].*[*]/$")) @injection.content
|
|
(#set! injection.language "jsdoc"))
|
|
|
|
((comment) @injection.content
|
|
(#set! injection.language "comment"))
|
|
|
|
; html(`...`), html`...`, sql(`...`), etc.
|
|
(call_expression
|
|
function: (identifier) @injection.language
|
|
arguments: [
|
|
(arguments
|
|
(template_string
|
|
(string_fragment) @injection.content))
|
|
(template_string
|
|
(string_fragment) @injection.content)
|
|
]
|
|
(#lua-match? @injection.language "^[a-zA-Z][a-zA-Z0-9]*$")
|
|
(#set! injection.combined)
|
|
; Languages excluded from auto-injection due to special rules
|
|
; - svg uses the html parser
|
|
; - css uses the styled parser
|
|
(#not-any-of? @injection.language "svg" "css"))
|
|
|
|
; svg`...` or svg(`...`)
|
|
(call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "svg")
|
|
arguments: [
|
|
(arguments
|
|
(template_string
|
|
(string_fragment) @injection.content))
|
|
(template_string
|
|
(string_fragment) @injection.content)
|
|
]
|
|
(#set! injection.combined)
|
|
(#set! injection.language "html"))
|
|
|
|
; Vercel PostgreSQL
|
|
; foo.sql`...` or foo.sql(`...`)
|
|
(call_expression
|
|
function: (member_expression
|
|
property: (property_identifier) @injection.language)
|
|
arguments: [
|
|
(arguments
|
|
(template_string
|
|
(string_fragment) @injection.content))
|
|
(template_string
|
|
(string_fragment) @injection.content)
|
|
]
|
|
(#eq? @injection.language "sql")
|
|
(#set! injection.combined))
|
|
|
|
; Sanity CMS GROQ query
|
|
; defineQuery(`...`)
|
|
(call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "defineQuery")
|
|
arguments: (arguments
|
|
(template_string
|
|
(string_fragment) @injection.content))
|
|
(#set! injection.combined)
|
|
(#set! injection.language "groq"))
|
|
|
|
; gql`...` or gql(`...`)
|
|
(call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "gql")
|
|
arguments: [
|
|
(arguments
|
|
(template_string
|
|
(string_fragment) @injection.content))
|
|
(template_string
|
|
(string_fragment) @injection.content)
|
|
]
|
|
(#set! injection.combined)
|
|
(#set! injection.language "graphql"))
|
|
|
|
(call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "hbs")
|
|
arguments: (template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "glimmer"))
|
|
|
|
; css`<css>`, keyframes`<css>`
|
|
(call_expression
|
|
function: (identifier) @_name
|
|
(#any-of? @_name "css" "keyframes")
|
|
arguments: (template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "styled"))
|
|
|
|
; styled.div`<css>`
|
|
(call_expression
|
|
function: (member_expression
|
|
object: (identifier) @_name
|
|
(#eq? @_name "styled"))
|
|
arguments: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "styled")))
|
|
|
|
; styled(Component)`<css>`
|
|
(call_expression
|
|
function: (call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "styled"))
|
|
arguments: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "styled")))
|
|
|
|
; styled.div.attrs({ prop: "foo" })`<css>`
|
|
(call_expression
|
|
function: (call_expression
|
|
function: (member_expression
|
|
object: (member_expression
|
|
object: (identifier) @_name
|
|
(#eq? @_name "styled"))))
|
|
arguments: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "styled")))
|
|
|
|
; styled(Component).attrs({ prop: "foo" })`<css>`
|
|
(call_expression
|
|
function: (call_expression
|
|
function: (member_expression
|
|
object: (call_expression
|
|
function: (identifier) @_name
|
|
(#eq? @_name "styled"))))
|
|
arguments: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "styled")))
|
|
|
|
((regex_pattern) @injection.content
|
|
(#set! injection.language "regex"))
|
|
|
|
; ((comment) @_gql_comment
|
|
; (#eq? @_gql_comment "/* GraphQL */")
|
|
; (template_string) @injection.content
|
|
; (#set! injection.language "graphql"))
|
|
((template_string
|
|
(string_fragment) @injection.content)
|
|
(#lua-match? @injection.content "^#graphql")
|
|
(#set! injection.combined)
|
|
(#set! injection.language "graphql"))
|
|
|
|
; el.innerHTML = `<html>`
|
|
(assignment_expression
|
|
left: (member_expression
|
|
property: (property_identifier) @_prop
|
|
(#any-of? @_prop "outerHTML" "innerHTML"))
|
|
right: (template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "html"))
|
|
|
|
; el.innerHTML = '<html>'
|
|
(assignment_expression
|
|
left: (member_expression
|
|
property: (property_identifier) @_prop
|
|
(#any-of? @_prop "outerHTML" "innerHTML"))
|
|
right: (string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.language "html"))
|
|
|
|
;---- Angular injections -----
|
|
; @Component({
|
|
; template: `<html>`
|
|
; })
|
|
(decorator
|
|
(call_expression
|
|
function: ((identifier) @_name
|
|
(#eq? @_name "Component"))
|
|
arguments: (arguments
|
|
(object
|
|
(pair
|
|
key: ((property_identifier) @_prop
|
|
(#eq? @_prop "template"))
|
|
value: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "angular")))))))
|
|
|
|
; @Component({
|
|
; styles: [`<css>`]
|
|
; })
|
|
(decorator
|
|
(call_expression
|
|
function: ((identifier) @_name
|
|
(#eq? @_name "Component"))
|
|
arguments: (arguments
|
|
(object
|
|
(pair
|
|
key: ((property_identifier) @_prop
|
|
(#eq? @_prop "styles"))
|
|
value: (array
|
|
((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "css"))))))))
|
|
|
|
; @Component({
|
|
; styles: `<css>`
|
|
; })
|
|
(decorator
|
|
(call_expression
|
|
function: ((identifier) @_name
|
|
(#eq? @_name "Component"))
|
|
arguments: (arguments
|
|
(object
|
|
(pair
|
|
key: ((property_identifier) @_prop
|
|
(#eq? @_prop "styles"))
|
|
value: ((template_string
|
|
(string_fragment) @injection.content)
|
|
(#set! injection.combined)
|
|
(#set! injection.language "css")))))))
|