mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-14 01:10:02 -04:00
feat(markdown): configured aliases for fenced code block languages (#4659)
* feat(markdown): configured aliases for languages * refactor: use vim.treesitter.match * refactor: rename local vars * fix: query syntax
This commit is contained in:
parent
c79c37927b
commit
cdc45ac6ba
2 changed files with 50 additions and 14 deletions
|
|
@ -1,5 +1,25 @@
|
||||||
local query = require "vim.treesitter.query"
|
local query = require "vim.treesitter.query"
|
||||||
|
|
||||||
|
local html_script_type_languages = {
|
||||||
|
["importmap"] = "json",
|
||||||
|
["module"] = "javascript",
|
||||||
|
["application/ecmascript"] = "javascript",
|
||||||
|
["text/ecmascript"] = "javascript",
|
||||||
|
}
|
||||||
|
|
||||||
|
local non_filetype_match_injection_language_aliases = {
|
||||||
|
ex = "elixir",
|
||||||
|
pl = "perl",
|
||||||
|
sh = "bash",
|
||||||
|
uxn = "uxntal",
|
||||||
|
ts = "typescript",
|
||||||
|
}
|
||||||
|
|
||||||
|
local function get_parser_from_markdown_info_string(injection_alias)
|
||||||
|
local match = vim.filetype.match { filename = "a." .. injection_alias }
|
||||||
|
return match or non_filetype_match_injection_language_aliases[injection_alias] or injection_alias
|
||||||
|
end
|
||||||
|
|
||||||
local function error(str)
|
local function error(str)
|
||||||
vim.api.nvim_err_writeln(str)
|
vim.api.nvim_err_writeln(str)
|
||||||
end
|
end
|
||||||
|
|
@ -119,19 +139,17 @@ 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 = {
|
---@param match (TSNode|nil)[]
|
||||||
["importmap"] = "json",
|
---@param _ string
|
||||||
["module"] = "javascript",
|
---@param bufnr integer
|
||||||
["application/ecmascript"] = "javascript",
|
---@param pred string[]
|
||||||
["text/ecmascript"] = "javascript",
|
|
||||||
}
|
|
||||||
|
|
||||||
---@param match string
|
|
||||||
---@param metadata table
|
|
||||||
---@return boolean|nil
|
---@return boolean|nil
|
||||||
query.add_directive("set-lang-from-mimetype!", function(match, pattern, bufnr, predicate, metadata)
|
query.add_directive("set-lang-from-mimetype!", function(match, _, bufnr, pred, metadata)
|
||||||
local capture_id = predicate[2]
|
local capture_id = pred[2]
|
||||||
local node = match[capture_id]
|
local node = match[capture_id]
|
||||||
|
if not node then
|
||||||
|
return
|
||||||
|
end
|
||||||
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
|
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
|
||||||
local configured = html_script_type_languages[type_attr_value]
|
local configured = html_script_type_languages[type_attr_value]
|
||||||
if configured then
|
if configured then
|
||||||
|
|
@ -142,6 +160,21 @@ query.add_directive("set-lang-from-mimetype!", function(match, pattern, bufnr, p
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---@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)
|
||||||
|
metadata.language = get_parser_from_markdown_info_string(injection_alias)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
(fenced_code_block
|
(fenced_code_block
|
||||||
(info_string
|
(info_string
|
||||||
(language) @language)
|
(language) @_lang)
|
||||||
(#not-match? @language "elm")
|
(#not-match? @_lang "elm") ; prevent segfault when using elm parser
|
||||||
(code_fence_content) @content (#exclude_children! @content))
|
(code_fence_content)
|
||||||
|
@content
|
||||||
|
(#set-lang-from-info-string! @_lang)
|
||||||
|
(#exclude_children! @content))
|
||||||
|
|
||||||
((html_block) @html)
|
((html_block) @html)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue