mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-02 03:26:52 -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 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)
|
||||
vim.api.nvim_err_writeln(str)
|
||||
end
|
||||
|
|
@ -119,19 +139,17 @@ 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
|
||||
---@param match (TSNode|nil)[]
|
||||
---@param _ string
|
||||
---@param bufnr integer
|
||||
---@param pred string[]
|
||||
---@return boolean|nil
|
||||
query.add_directive("set-lang-from-mimetype!", function(match, pattern, bufnr, predicate, metadata)
|
||||
local capture_id = predicate[2]
|
||||
query.add_directive("set-lang-from-mimetype!", function(match, _, bufnr, pred, metadata)
|
||||
local capture_id = pred[2]
|
||||
local node = match[capture_id]
|
||||
if not node then
|
||||
return
|
||||
end
|
||||
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
|
||||
local configured = html_script_type_languages[type_attr_value]
|
||||
if configured then
|
||||
|
|
@ -142,6 +160,21 @@ query.add_directive("set-lang-from-mimetype!", function(match, pattern, bufnr, p
|
|||
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
|
||||
query.add_directive("make-range!", function() end)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue