mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-19 03:40:04 -04:00
feat(html): injections in <script type>
refactor(html): remove superfluous injection query fix(html): associate "module" type with "javascript"
This commit is contained in:
parent
972da989bc
commit
19d9af8fb0
2 changed files with 33 additions and 10 deletions
|
|
@ -119,6 +119,29 @@ query.add_predicate("has-type?", function(match, _pattern, _bufnr, pred)
|
||||||
return vim.tbl_contains(types, node:type())
|
return vim.tbl_contains(types, node:type())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local html_script_type_languages = {
|
||||||
|
["importmap"] = "json",
|
||||||
|
["module"] = "javascript",
|
||||||
|
["application/ecmascript"] = "javascript",
|
||||||
|
["text/ecmascript"] = "javascript",
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param match string
|
||||||
|
---@param metadata table
|
||||||
|
---@return boolean|nil
|
||||||
|
query.add_directive("set-lang-from-mimetype!", function(match, pattern, bufnr, predicate, metadata)
|
||||||
|
local capture_id = predicate[2]
|
||||||
|
local node = match[capture_id]
|
||||||
|
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
|
||||||
|
local configured = html_script_type_languages[type_attr_value]
|
||||||
|
if configured then
|
||||||
|
metadata.language = configured
|
||||||
|
else
|
||||||
|
local parts = vim.split(type_attr_value, "/", {})
|
||||||
|
metadata.language = parts[#parts]
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- Just avoid some annoying warnings for this directive
|
-- Just avoid some annoying warnings for this directive
|
||||||
query.add_directive("make-range!", function() end)
|
query.add_directive("make-range!", function() end)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,23 +27,23 @@
|
||||||
(#not-match? @_no_type_lang "\\s(lang|type)\\s*\\=")
|
(#not-match? @_no_type_lang "\\s(lang|type)\\s*\\=")
|
||||||
(raw_text) @javascript))
|
(raw_text) @javascript))
|
||||||
|
|
||||||
(
|
; <script type="mimetype-or-well-known-script-type">
|
||||||
(script_element
|
(script_element
|
||||||
(start_tag
|
(start_tag
|
||||||
(attribute
|
((attribute
|
||||||
(attribute_name) @_type
|
(attribute_name) @_attr (#eq? @_attr "type")
|
||||||
(quoted_attribute_value (attribute_value) @_javascript)))
|
(quoted_attribute_value (attribute_value) @_type))))
|
||||||
(raw_text) @javascript)
|
(raw_text) @content (#set-lang-from-mimetype! @_type))
|
||||||
(#eq? @_type "type")
|
|
||||||
(#any-of? @_javascript "text/javascript" "module")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
; <a style="/* css */">
|
||||||
((attribute
|
((attribute
|
||||||
(attribute_name) @_attr
|
(attribute_name) @_attr
|
||||||
(quoted_attribute_value (attribute_value) @css))
|
(quoted_attribute_value (attribute_value) @css))
|
||||||
(#eq? @_attr "style"))
|
(#eq? @_attr "style"))
|
||||||
|
|
||||||
; lit-html style template interpolation
|
; lit-html style template interpolation
|
||||||
|
; <a @click=${e => console.log(e)}>
|
||||||
|
; <a @click="${e => console.log(e)}">
|
||||||
((attribute
|
((attribute
|
||||||
(quoted_attribute_value (attribute_value) @javascript))
|
(quoted_attribute_value (attribute_value) @javascript))
|
||||||
(#match? @javascript "\\$\\{")
|
(#match? @javascript "\\$\\{")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue