diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/query_predicates.lua | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/query_predicates.lua b/lua/nvim-treesitter/query_predicates.lua index 2509a677c..8c0a4c947 100644 --- a/lua/nvim-treesitter/query_predicates.lua +++ b/lua/nvim-treesitter/query_predicates.lua @@ -119,6 +119,29 @@ query.add_predicate("has-type?", function(match, _pattern, _bufnr, pred) return vim.tbl_contains(types, node:type()) 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 query.add_directive("make-range!", function() end) |
