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:
Benny Powers 2023-04-18 15:51:14 +03:00 committed by GitHub
parent c79c37927b
commit cdc45ac6ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

View file

@ -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)

View file

@ -1,8 +1,11 @@
(fenced_code_block
(info_string
(language) @language)
(#not-match? @language "elm")
(code_fence_content) @content (#exclude_children! @content))
(language) @_lang)
(#not-match? @_lang "elm") ; prevent segfault when using elm parser
(code_fence_content)
@content
(#set-lang-from-info-string! @_lang)
(#exclude_children! @content))
((html_block) @html)