feat(html): use gsub for mimetype lookup

This commit is contained in:
Christian Clason 2025-04-05 12:22:48 +02:00
parent 328ee3db54
commit cbfe8a20c7
6 changed files with 33 additions and 35 deletions

View file

@ -29,6 +29,7 @@ Language | Tier | Queries | Maintainer
[blade](https://github.com/EmranMR/tree-sitter-blade) | unstable | `HFIJ ` | @calebdw [blade](https://github.com/EmranMR/tree-sitter-blade) | unstable | `HFIJ ` | @calebdw
[blueprint](https://gitlab.com/gabmus/tree-sitter-blueprint) | unstable | `H  J ` | @gabmus [blueprint](https://gitlab.com/gabmus/tree-sitter-blueprint) | unstable | `H  J ` | @gabmus
[bp](https://github.com/ambroisie/tree-sitter-bp)[^bp] | unstable | `HFIJL` | @ambroisie [bp](https://github.com/ambroisie/tree-sitter-bp)[^bp] | unstable | `HFIJL` | @ambroisie
[brightscript](https://github.com/ajdelcimmuto/tree-sitter-brightscript) | unstable | `HFIJ ` | @ajdelcimmuto
[c](https://github.com/tree-sitter/tree-sitter-c) | unstable | `HFIJL` | @amaanq [c](https://github.com/tree-sitter/tree-sitter-c) | unstable | `HFIJL` | @amaanq
[c_sharp](https://github.com/tree-sitter/tree-sitter-c-sharp) | unstable | `HF JL` | @amaanq [c_sharp](https://github.com/tree-sitter/tree-sitter-c-sharp) | unstable | `HF JL` | @amaanq
[caddy](https://github.com/opa-oz/tree-sitter-caddy) | unmaintained | `HFIJ ` | @opa-oz [caddy](https://github.com/opa-oz/tree-sitter-caddy) | unmaintained | `HFIJ ` | @opa-oz

View file

@ -153,8 +153,8 @@ return {
}, },
brightscript = { brightscript = {
install_info = { install_info = {
revision = '48ce1687125c6dfefcc7a1bef19fa0f0f00426cc',
url = 'https://github.com/ajdelcimmuto/tree-sitter-brightscript', url = 'https://github.com/ajdelcimmuto/tree-sitter-brightscript',
files = { 'src/parser.c' },
}, },
maintainers = { '@ajdelcimmuto' }, maintainers = { '@ajdelcimmuto' },
tier = 2, tier = 2,

View file

@ -22,7 +22,7 @@ local filetypes = {
haskell_persistent = { 'haskellpersistent' }, haskell_persistent = { 'haskellpersistent' },
idris = { 'idris2' }, idris = { 'idris2' },
janet_simple = { 'janet' }, janet_simple = { 'janet' },
javascript = { 'javascriptreact', 'ecma', 'jsx', 'js' }, javascript = { 'javascriptreact', 'ecma', 'ecmascript', 'jsx', 'js' },
javascript_glimmer = { 'javascript.glimmer' }, javascript_glimmer = { 'javascript.glimmer' },
linkerscript = { 'ld' }, linkerscript = { 'ld' },
latex = { 'tex' }, latex = { 'tex' },

View file

@ -39,30 +39,3 @@ end, { force = true })
query.add_predicate('any-kind-eq?', function(match, _, _, pred) query.add_predicate('any-kind-eq?', function(match, _, _, pred)
return predicates['kind-eq'](match, pred, true) return predicates['kind-eq'](match, pred, true)
end, { force = true }) end, { force = true })
-- register custom directives
local mimetype_aliases = {
['importmap'] = 'json',
['module'] = 'javascript',
['application/ecmascript'] = 'javascript',
['text/ecmascript'] = 'javascript',
}
---@param match TSQueryMatch
---@param _ string
---@param bufnr integer
---@param pred string[]
---@return boolean|nil
query.add_directive('set-lang-from-mimetype!', function(match, _, bufnr, pred, metadata)
local id = pred[2]
local node = match[id]
local type_attr_value = vim.treesitter.get_node_text(node, bufnr, { metadata = metadata[id] })
local configured = mimetype_aliases[type_attr_value]
if configured then
metadata['injection.language'] = configured
else
local parts = vim.split(type_attr_value, '/', {})
metadata['injection.language'] = parts[#parts]
end
end, { force = true })

View file

@ -32,16 +32,40 @@
(#not-lua-match? @_no_type_lang "%stype%s*=") (#not-lua-match? @_no_type_lang "%stype%s*=")
(#set! injection.language "javascript")) (#set! injection.language "javascript"))
; <script type="mimetype-or-well-known-script-type"> ; <script type="foo/bar">
(script_element (script_element
(start_tag (start_tag
(attribute (attribute
(attribute_name) @_attr (attribute_name) @_attr
(#eq? @_attr "type") (#eq? @_attr "type")
(quoted_attribute_value (quoted_attribute_value
(attribute_value) @_type))) (attribute_value) @injection.language)))
(raw_text) @injection.content (raw_text) @injection.content
(#set-lang-from-mimetype! @_type)) (#gsub! @injection.language "(.+)/(.+)" "%2"))
; <script type="importmap">
((script_element
(start_tag
(attribute
(attribute_name) @_attr
(#eq? @_attr "type")
(quoted_attribute_value
(attribute_value) @_type)))
(raw_text) @injection.content)
(#eq? @_type "importmap")
(#set! injection.language "json"))
; <script type="module">
((script_element
(start_tag
(attribute
(attribute_name) @_attr
(#eq? @_attr "type")
(quoted_attribute_value
(attribute_value) @_type)))
(raw_text) @injection.content)
(#eq? @_type "module")
(#set! injection.language "javascript"))
; <a style="/* css */"> ; <a style="/* css */">
((attribute ((attribute

View file

@ -19,6 +19,8 @@
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script async defer> const x = 1 </script> <script async defer> const x = 1 </script>
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script type="module"> import { foo } from "bar" </script>
<!-- ^ @javascript -->
<script type="text/javascript"> const x = 1 </script> <script type="text/javascript"> const x = 1 </script>
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script type="text/ecmascript"> const x = 1 </script> <script type="text/ecmascript"> const x = 1 </script>
@ -27,8 +29,6 @@
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script type="application/javascript"> const x = 1 </script> <script type="application/javascript"> const x = 1 </script>
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script type="module"> import { foo } from "bar" </script>
<!-- ^ @javascript -->
<script defer type="text/javascript"> const x = 1 </script> <script defer type="text/javascript"> const x = 1 </script>
<!-- ^ @javascript --> <!-- ^ @javascript -->
<script type="text/markdown">## Hello *World*!</script> <script type="text/markdown">## Hello *World*!</script>
@ -40,7 +40,7 @@
<script type="application/json">{ "true": false }</script> <script type="application/json">{ "true": false }</script>
<!-- ^ @json --> <!-- ^ @json -->
<script type="importmap">{ "true": false }</script> <script type="importmap">{ "true": false }</script>
<!-- ^ @json --> <!-- ^ @json -->
<div style="height: 100%"> <div style="height: 100%">
<!-- ^ @css --> <!-- ^ @css -->
Test div to test css injections for style attributes Test div to test css injections for style attributes