mirror of
https://github.com/neovim/neovim
synced 2025-07-16 09:11:51 +00:00
feat(treesitter): show root nodes in :InspectTree (#26944)
Co-authored-by: altermo <> Co-authored-by: Jongwook Choi <wookayin@gmail.com>
This commit is contained in:
@ -225,6 +225,7 @@ The following new APIs and features were added.
|
|||||||
• Improved error messages for query parsing.
|
• Improved error messages for query parsing.
|
||||||
• `:InspectTree` (|vim.treesitter.inspect_tree()|) shows node ranges in
|
• `:InspectTree` (|vim.treesitter.inspect_tree()|) shows node ranges in
|
||||||
0-based indexing instead of 1-based indexing.
|
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`,
|
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
|
||||||
Windows `explorer`, Linux `xdg-open`, etc.)
|
Windows `explorer`, Linux `xdg-open`, etc.)
|
||||||
|
@ -43,25 +43,26 @@ local TSTreeView = {}
|
|||||||
---
|
---
|
||||||
---@param node TSNode Starting node to begin traversal |tsnode|
|
---@param node TSNode Starting node to begin traversal |tsnode|
|
||||||
---@param depth integer Current recursion depth
|
---@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 lang string Language of the tree currently being traversed
|
||||||
---@param injections table<string, TSP.Injection> Mapping of node ids to root nodes
|
---@param injections table<string, TSP.Injection> Mapping of node ids to root nodes
|
||||||
--- of injected language trees (see explanation above)
|
--- 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
|
---@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()]
|
local injection = injections[node:id()]
|
||||||
if injection then
|
if injection then
|
||||||
traverse(injection.root, depth, injection.lang, injections, tree)
|
traverse(injection.root, depth + 1, nil, injection.lang, injections, tree)
|
||||||
end
|
end
|
||||||
|
|
||||||
for child, field in node:iter_children() do
|
for child, child_field in node:iter_children() do
|
||||||
table.insert(tree, {
|
traverse(child, depth + 1, child_field, lang, injections, tree)
|
||||||
node = child,
|
|
||||||
field = field,
|
|
||||||
depth = depth,
|
|
||||||
lang = lang,
|
|
||||||
})
|
|
||||||
|
|
||||||
traverse(child, depth + 1, lang, injections, tree)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return tree
|
return tree
|
||||||
@ -106,7 +107,7 @@ function TSTreeView:new(bufnr, lang)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local nodes = traverse(root, 0, parser:lang(), injections, {})
|
local nodes = traverse(root, 0, nil, parser:lang(), injections, {})
|
||||||
|
|
||||||
local named = {} ---@type TSP.Node[]
|
local named = {} ---@type TSP.Node[]
|
||||||
for _, v in ipairs(nodes) do
|
for _, v in ipairs(nodes) do
|
||||||
|
@ -26,9 +26,10 @@ describe('vim.treesitter.inspect_tree', function()
|
|||||||
]])
|
]])
|
||||||
|
|
||||||
expect_tree [[
|
expect_tree [[
|
||||||
(function_call ; [0, 0] - [0, 7]
|
(chunk ; [0, 0] - [2, 0]
|
||||||
name: (identifier) ; [0, 0] - [0, 5]
|
(function_call ; [0, 0] - [0, 7]
|
||||||
arguments: (arguments)) ; [0, 5] - [0, 7]
|
name: (identifier) ; [0, 0] - [0, 5]
|
||||||
|
arguments: (arguments))) ; [0, 5] - [0, 7]
|
||||||
]]
|
]]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -44,11 +45,12 @@ describe('vim.treesitter.inspect_tree', function()
|
|||||||
feed('a')
|
feed('a')
|
||||||
|
|
||||||
expect_tree [[
|
expect_tree [[
|
||||||
(function_call ; [0, 0] - [0, 7]
|
(chunk ; [0, 0] - [2, 0]
|
||||||
name: (identifier) ; [0, 0] - [0, 5]
|
(function_call ; [0, 0] - [0, 7]
|
||||||
arguments: (arguments ; [0, 5] - [0, 7]
|
name: (identifier) ; [0, 0] - [0, 5]
|
||||||
"(" ; [0, 5] - [0, 6]
|
arguments: (arguments ; [0, 5] - [0, 7]
|
||||||
")")) ; [0, 6] - [0, 7]
|
"(" ; [0, 5] - [0, 6]
|
||||||
|
")"))) ; [0, 6] - [0, 7]
|
||||||
]]
|
]]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -66,16 +68,18 @@ describe('vim.treesitter.inspect_tree', function()
|
|||||||
]])
|
]])
|
||||||
|
|
||||||
expect_tree [[
|
expect_tree [[
|
||||||
(section ; [0, 0] - [4, 0]
|
(document ; [0, 0] - [4, 0]
|
||||||
(fenced_code_block ; [0, 0] - [3, 0]
|
(section ; [0, 0] - [4, 0]
|
||||||
(fenced_code_block_delimiter) ; [0, 0] - [0, 3]
|
(fenced_code_block ; [0, 0] - [3, 0]
|
||||||
(info_string ; [0, 3] - [0, 6]
|
(fenced_code_block_delimiter) ; [0, 0] - [0, 3]
|
||||||
(language)) ; [0, 3] - [0, 6]
|
(info_string ; [0, 3] - [0, 6]
|
||||||
(block_continuation) ; [1, 0] - [1, 0]
|
(language)) ; [0, 3] - [0, 6]
|
||||||
(code_fence_content ; [1, 0] - [2, 0]
|
(block_continuation) ; [1, 0] - [1, 0]
|
||||||
(return_statement) ; [1, 0] - [1, 6]
|
(code_fence_content ; [1, 0] - [2, 0]
|
||||||
(block_continuation)) ; [2, 0] - [2, 0]
|
(chunk ; [1, 0] - [2, 0]
|
||||||
(fenced_code_block_delimiter))) ; [2, 0] - [2, 3]
|
(return_statement)) ; [1, 0] - [1, 6]
|
||||||
|
(block_continuation)) ; [2, 0] - [2, 0]
|
||||||
|
(fenced_code_block_delimiter)))) ; [2, 0] - [2, 3]
|
||||||
]]
|
]]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -94,16 +98,18 @@ describe('vim.treesitter.inspect_tree', function()
|
|||||||
feed('I')
|
feed('I')
|
||||||
|
|
||||||
expect_tree [[
|
expect_tree [[
|
||||||
(section ; [0, 0] - [4, 0] markdown
|
(document ; [0, 0] - [4, 0] markdown
|
||||||
(fenced_code_block ; [0, 0] - [3, 0] markdown
|
(section ; [0, 0] - [4, 0] markdown
|
||||||
(fenced_code_block_delimiter) ; [0, 0] - [0, 3] markdown
|
(fenced_code_block ; [0, 0] - [3, 0] markdown
|
||||||
(info_string ; [0, 3] - [0, 6] markdown
|
(fenced_code_block_delimiter) ; [0, 0] - [0, 3] markdown
|
||||||
(language)) ; [0, 3] - [0, 6] markdown
|
(info_string ; [0, 3] - [0, 6] markdown
|
||||||
(block_continuation) ; [1, 0] - [1, 0] markdown
|
(language)) ; [0, 3] - [0, 6] markdown
|
||||||
(code_fence_content ; [1, 0] - [2, 0] markdown
|
(block_continuation) ; [1, 0] - [1, 0] markdown
|
||||||
(return_statement) ; [1, 0] - [1, 6] lua
|
(code_fence_content ; [1, 0] - [2, 0] markdown
|
||||||
(block_continuation)) ; [2, 0] - [2, 0] markdown
|
(chunk ; [1, 0] - [2, 0] lua
|
||||||
(fenced_code_block_delimiter))) ; [2, 0] - [2, 3] markdown
|
(return_statement)) ; [1, 0] - [1, 6] lua
|
||||||
|
(block_continuation)) ; [2, 0] - [2, 0] markdown
|
||||||
|
(fenced_code_block_delimiter)))) ; [2, 0] - [2, 3] markdown
|
||||||
]]
|
]]
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user