mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-19 11:50:09 -04:00
fix: remove upstreamed directives
`#inject-lang!` and `#trim!`; fix `set-lang-from-mimetype`
This commit is contained in:
parent
83358dfbef
commit
62e5c7ab1d
3 changed files with 11 additions and 76 deletions
|
|
@ -26,6 +26,7 @@ local filetypes = {
|
||||||
t32 = { 'trace32' },
|
t32 = { 'trace32' },
|
||||||
tlaplus = { 'tla' },
|
tlaplus = { 'tla' },
|
||||||
tsx = { 'typescriptreact', 'typescript.tsx' },
|
tsx = { 'typescriptreact', 'typescript.tsx' },
|
||||||
|
typescript = { 'ts' },
|
||||||
uxntal = { 'tal' },
|
uxntal = { 'tal' },
|
||||||
v = { 'vlang' },
|
v = { 'vlang' },
|
||||||
verilog = { 'sysverilog' },
|
verilog = { 'sysverilog' },
|
||||||
|
|
|
||||||
|
|
@ -30,12 +30,13 @@ local mimetype_aliases = {
|
||||||
---@param pred string[]
|
---@param pred string[]
|
||||||
---@return boolean|nil
|
---@return boolean|nil
|
||||||
query.add_directive('set-lang-from-mimetype!', function(match, _, bufnr, pred, metadata)
|
query.add_directive('set-lang-from-mimetype!', function(match, _, bufnr, pred, metadata)
|
||||||
local capture_id = pred[2]
|
local id = pred[2]
|
||||||
local node = match[capture_id]
|
local node = match[id]
|
||||||
if not node then
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
|
|
||||||
|
local type_attr_value = vim.treesitter.get_node_text(node, bufnr, { metadata = metadata[id] })
|
||||||
local configured = mimetype_aliases[type_attr_value]
|
local configured = mimetype_aliases[type_attr_value]
|
||||||
if configured then
|
if configured then
|
||||||
metadata['injection.language'] = configured
|
metadata['injection.language'] = configured
|
||||||
|
|
@ -45,41 +46,17 @@ query.add_directive('set-lang-from-mimetype!', function(match, _, bufnr, pred, m
|
||||||
end
|
end
|
||||||
end, true)
|
end, true)
|
||||||
|
|
||||||
local injection_aliases = {
|
|
||||||
ex = 'elixir',
|
|
||||||
pl = 'perl',
|
|
||||||
sh = 'bash',
|
|
||||||
uxn = 'uxntal',
|
|
||||||
ts = 'typescript',
|
|
||||||
}
|
|
||||||
|
|
||||||
---@param match (TSNode|nil)[]
|
|
||||||
---@param _ string
|
|
||||||
---@param bufnr integer
|
|
||||||
---@param pred string[]
|
|
||||||
---@return boolean|nil
|
|
||||||
query.add_directive('set-lang-from-info-string!', function(match, _, bufnr, pred, metadata)
|
|
||||||
local capture_id = pred[2]
|
|
||||||
local node = match[capture_id]
|
|
||||||
if not node then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local injection_alias = vim.treesitter.get_node_text(node, bufnr)
|
|
||||||
local filetype = vim.filetype.match({ filename = 'a.' .. injection_alias })
|
|
||||||
metadata['injection.language'] = filetype or injection_aliases[injection_alias] or injection_alias
|
|
||||||
end, true)
|
|
||||||
|
|
||||||
query.add_directive('downcase!', function(match, _, bufnr, pred, metadata)
|
query.add_directive('downcase!', function(match, _, bufnr, pred, metadata)
|
||||||
local text, key, value ---@type string|string[], string, string|integer
|
local text, key, value ---@type string|string[], string, string|integer
|
||||||
|
local id = pred[2]
|
||||||
|
|
||||||
if #pred == 3 then
|
if #pred == 3 then
|
||||||
-- (#downcase! @capture "key")
|
-- (#downcase! @capture "key")
|
||||||
key = pred[3]
|
key = pred[3]
|
||||||
value = metadata[pred[2]][key]
|
value = metadata[id][key]
|
||||||
else
|
else
|
||||||
-- (#downcase! "key")
|
-- (#downcase! "key")
|
||||||
key = pred[2]
|
key = id
|
||||||
value = metadata[key]
|
value = metadata[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -87,55 +64,12 @@ query.add_directive('downcase!', function(match, _, bufnr, pred, metadata)
|
||||||
text = value
|
text = value
|
||||||
else
|
else
|
||||||
local node = match[value]
|
local node = match[value]
|
||||||
text = vim.treesitter.get_node_text(node, bufnr) or ''
|
text = vim.treesitter.get_node_text(node, bufnr, { metadata = metadata[id] }) or ''
|
||||||
end
|
end
|
||||||
|
|
||||||
if #pred == 3 then
|
if #pred == 3 then
|
||||||
metadata[pred[2]][key] = string.lower(text)
|
metadata[id][key] = string.lower(text)
|
||||||
else
|
else
|
||||||
metadata[key] = string.lower(text)
|
metadata[key] = string.lower(text)
|
||||||
end
|
end
|
||||||
end, true)
|
end, true)
|
||||||
|
|
||||||
-- Trim blank lines from end of the region
|
|
||||||
-- Arguments are the captures to trim.
|
|
||||||
---@param match (TSNode|nil)[]
|
|
||||||
---@param _ string
|
|
||||||
---@param bufnr integer
|
|
||||||
---@param pred string[]
|
|
||||||
---@param metadata table
|
|
||||||
---TODO(clason): upstream
|
|
||||||
query.add_directive('trim!', function(match, _, bufnr, pred, metadata)
|
|
||||||
for _, id in ipairs({ select(2, unpack(pred)) }) do
|
|
||||||
local node = match[id]
|
|
||||||
if not node then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local start_row, start_col, end_row, end_col = node:range()
|
|
||||||
|
|
||||||
-- Don't trim if region ends in middle of a line
|
|
||||||
if end_col ~= 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
-- As we only care when end_col == 0, always inspect one line above end_row.
|
|
||||||
local end_line = vim.api.nvim_buf_get_lines(bufnr, end_row - 1, end_row, true)[1]
|
|
||||||
|
|
||||||
if end_line ~= '' then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
end_row = end_row - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
-- If this produces an invalid range, we just skip it.
|
|
||||||
if start_row < end_row or (start_row == end_row and start_col <= end_col) then
|
|
||||||
if not metadata[id] then
|
|
||||||
metadata[id] = {}
|
|
||||||
end
|
|
||||||
metadata[id].range = { start_row, start_col, end_row, end_col }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end, true)
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
(info_string
|
(info_string
|
||||||
(language) @_lang)
|
(language) @_lang)
|
||||||
(code_fence_content) @injection.content
|
(code_fence_content) @injection.content
|
||||||
(#set-lang-from-info-string! @_lang))
|
(#inject-language! @_lang))
|
||||||
|
|
||||||
((html_block) @injection.content
|
((html_block) @injection.content
|
||||||
(#set! injection.language "html")
|
(#set! injection.language "html")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue