diff options
| author | altermo <107814000+altermo@users.noreply.github.com> | 2024-02-06 21:51:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-06 14:51:53 -0600 |
| commit | c0b99bb1de8de967d82fc29780996ed4060516c1 (patch) | |
| tree | ce7d547282a95ffbe428871c93bbfb21611eaf5a /runtime | |
| parent | c4417ae70c03815c2fb64edb479017e79d223cf7 (diff) | |
| download | rneovim-c0b99bb1de8de967d82fc29780996ed4060516c1.tar.gz rneovim-c0b99bb1de8de967d82fc29780996ed4060516c1.tar.bz2 rneovim-c0b99bb1de8de967d82fc29780996ed4060516c1.zip | |
feat(treesitter): show root nodes in :InspectTree (#26944)
Co-authored-by: altermo <>
Co-authored-by: Jongwook Choi <wookayin@gmail.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/doc/news.txt | 1 | ||||
| -rw-r--r-- | runtime/lua/vim/treesitter/dev.lua | 25 |
2 files changed, 14 insertions, 12 deletions
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 3dc85eddbb..c67713b657 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -225,6 +225,7 @@ The following new APIs and features were added. • Improved error messages for query parsing. • `:InspectTree` (|vim.treesitter.inspect_tree()|) shows node ranges in 0-based indexing instead of 1-based indexing. + • `:InspectTree` (|vim.treesitter.inspect_tree()|) shows root nodes • |vim.ui.open()| opens URIs using the system default handler (macOS `open`, Windows `explorer`, Linux `xdg-open`, etc.) diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua index e1f93a654b..551067533a 100644 --- a/runtime/lua/vim/treesitter/dev.lua +++ b/runtime/lua/vim/treesitter/dev.lua @@ -43,25 +43,26 @@ local TSTreeView = {} --- ---@param node TSNode Starting node to begin traversal |tsnode| ---@param depth integer Current recursion depth +---@param field string|nil The field of the current node ---@param lang string Language of the tree currently being traversed ---@param injections table<string, TSP.Injection> Mapping of node ids to root nodes --- of injected language trees (see explanation above) ---@param tree TSP.Node[] Output table containing a list of tables each representing a node in the tree -local function traverse(node, depth, lang, injections, tree) +local function traverse(node, depth, field, lang, injections, tree) + table.insert(tree, { + node = node, + depth = depth, + lang = lang, + field = field, + }) + local injection = injections[node:id()] if injection then - traverse(injection.root, depth, injection.lang, injections, tree) + traverse(injection.root, depth + 1, nil, injection.lang, injections, tree) end - for child, field in node:iter_children() do - table.insert(tree, { - node = child, - field = field, - depth = depth, - lang = lang, - }) - - traverse(child, depth + 1, lang, injections, tree) + for child, child_field in node:iter_children() do + traverse(child, depth + 1, child_field, lang, injections, tree) end return tree @@ -106,7 +107,7 @@ function TSTreeView:new(bufnr, lang) end end) - local nodes = traverse(root, 0, parser:lang(), injections, {}) + local nodes = traverse(root, 0, nil, parser:lang(), injections, {}) local named = {} ---@type TSP.Node[] for _, v in ipairs(nodes) do |