feat(treesitter): add 'lang' option to show_tree() (#21341)

This is necessary for now to support filetypes that use a parser with a
different name (e.g. the "terraform" filetype uses the "hcl" parser).
This commit is contained in:
Gregory Anders 2022-12-08 09:51:46 -07:00 committed by GitHub
parent d44699800c
commit 42009ac7df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 11 deletions

View File

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

View File

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

View File

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