aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorBenny Powers <bennypowers@users.noreply.github.com>2023-04-18 15:51:14 +0300
committerGitHub <noreply@github.com>2023-04-18 21:51:14 +0900
commitcdc45ac6bad52d95802b54e8a6d4f7aaa163edc5 (patch)
tree00ffe4aecdbb045866068732d5dc9923c791eca7 /lua
parentrefactor(smali): refactor queries from upstream (diff)
downloadnvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar.gz
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar.bz2
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar.lz
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar.xz
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.tar.zst
nvim-treesitter-cdc45ac6bad52d95802b54e8a6d4f7aaa163edc5.zip
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
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/query_predicates.lua55
1 files changed, 44 insertions, 11 deletions
diff --git a/lua/nvim-treesitter/query_predicates.lua b/lua/nvim-treesitter/query_predicates.lua
index 106f90a8d..c3d474e6c 100644
--- a/lua/nvim-treesitter/query_predicates.lua
+++ b/lua/nvim-treesitter/query_predicates.lua
@@ -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)