diff options
-rw-r--r-- | runtime/doc/treesitter.txt | 3 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 19 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/playground.lua | 10 |
3 files changed, 21 insertions, 11 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index 71a5fdaad0..096cec6678 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -526,6 +526,7 @@ get_node_at_pos({bufnr}, {row}, {col}, {opts}) • {row} (number) Position row • {col} (number) Position column • {opts} (table) Optional keyword arguments: + • lang string|nil Parser language • ignore_injections boolean Ignore injected languages (default true) @@ -612,6 +613,8 @@ show_tree({opts}) *vim.treesitter.show_tree()* Parameters: ~ • {opts} (table|nil) Optional options table with the following possible keys: + • lang (string|nil): The language of the source buffer. If + omitted, the filetype of the source buffer is used. • bufnr (number|nil): Buffer to draw the tree into. If omitted, a new buffer is created. • winid (number|nil): Window id to display the tree buffer in. diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 5c9210cc2d..7813c2edb2 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -275,6 +275,7 @@ end ---@param row number Position row ---@param col number Position column ---@param opts table Optional keyword arguments: +--- - lang string|nil Parser language --- - ignore_injections boolean Ignore injected languages (default true) --- ---@return userdata|nil |tsnode| under the cursor @@ -284,7 +285,7 @@ function M.get_node_at_pos(bufnr, row, col, opts) end local ts_range = { row, col, row, col } - local root_lang_tree = M.get_parser(bufnr) + local root_lang_tree = M.get_parser(bufnr, opts.lang) if not root_lang_tree then return end @@ -354,6 +355,8 @@ end --- cursor in the source buffer. --- ---@param opts table|nil Optional options table with the following possible keys: +--- - lang (string|nil): The language of the source buffer. If omitted, the +--- filetype of the source buffer is used. --- - bufnr (number|nil): Buffer to draw the tree into. If omitted, a new --- buffer is created. --- - winid (number|nil): Window id to display the tree buffer in. If omitted, @@ -368,12 +371,12 @@ function M.show_tree(opts) opts = { opts, 't', true }, }) + opts = opts or {} + local Playground = require('vim.treesitter.playground') local buf = a.nvim_get_current_buf() local win = a.nvim_get_current_win() - local pg = assert(Playground:new(buf)) - - opts = opts or {} + local pg = assert(Playground:new(buf, opts.lang)) -- Close any existing playground window if vim.b[buf].playground then @@ -473,8 +476,10 @@ function M.show_tree(opts) a.nvim_buf_clear_namespace(b, pg.ns, 0, -1) local cursor = a.nvim_win_get_cursor(win) - local cursor_node = - M.get_node_at_pos(buf, cursor[1] - 1, cursor[2], { ignore_injections = false }) + local cursor_node = M.get_node_at_pos(buf, cursor[1] - 1, cursor[2], { + lang = opts.lang, + ignore_injections = false, + }) if not cursor_node then return end @@ -503,7 +508,7 @@ function M.show_tree(opts) return true end - pg = assert(Playground:new(buf)) + pg = assert(Playground:new(buf, opts.lang)) pg:draw(b) end, }) diff --git a/runtime/lua/vim/treesitter/playground.lua b/runtime/lua/vim/treesitter/playground.lua index 325d303df5..bb073290c6 100644 --- a/runtime/lua/vim/treesitter/playground.lua +++ b/runtime/lua/vim/treesitter/playground.lua @@ -3,6 +3,7 @@ local api = vim.api local M = {} ---@class Playground +---@field ns number API namespace ---@field opts table Options table with the following keys: --- - anon (boolean): If true, display anonymous nodes --- - lang (boolean): If true, display the language alongside each node @@ -79,13 +80,14 @@ end --- Create a new Playground object. --- ---@param bufnr number Source buffer number +---@param lang string|nil Language of source buffer --- ---@return Playground|nil ---@return string|nil Error message, if any --- ---@private -function M.new(self, bufnr) - local ok, parser = pcall(vim.treesitter.get_parser, bufnr or 0) +function M.new(self, bufnr, lang) + local ok, parser = pcall(vim.treesitter.get_parser, bufnr or 0, lang) if not ok then return nil, 'No parser available for the given buffer' end @@ -95,13 +97,13 @@ function M.new(self, bufnr) -- the root in the child tree to the {injections} table. local root = parser:parse()[1]:root() local injections = {} - parser:for_each_child(function(child, lang) + parser:for_each_child(function(child, lang_) child:for_each_tree(function(tree) local r = tree:root() local node = root:named_descendant_for_range(r:range()) if node then injections[node:id()] = { - lang = lang, + lang = lang_, root = r, } end |