aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/dev.lua
diff options
context:
space:
mode:
authorPham Huy Hoang <hoangtun0810@gmail.com>2023-11-29 23:16:52 +0900
committerGitHub <noreply@github.com>2023-11-29 08:16:52 -0600
commitb6e339eb90e1a04f407f381739e46ad3c84f69c5 (patch)
tree73df467d5d9a8dc63ff03e8d773c2b77e7dc26da /runtime/lua/vim/treesitter/dev.lua
parent7bc5ee7f9327e8210c78bd21935130840aaf63f2 (diff)
downloadrneovim-b6e339eb90e1a04f407f381739e46ad3c84f69c5.tar.gz
rneovim-b6e339eb90e1a04f407f381739e46ad3c84f69c5.tar.bz2
rneovim-b6e339eb90e1a04f407f381739e46ad3c84f69c5.zip
fix(treesitter): make InspectTree correctly handle nested injections (#26085)
Problem: Only injections under the top level tree are found. Solution: Iterate through all trees to find injections. When two injections are contained within the same node in the parent tree, prefer the injection with the larger byte length.
Diffstat (limited to 'runtime/lua/vim/treesitter/dev.lua')
-rw-r--r--runtime/lua/vim/treesitter/dev.lua29
1 files changed, 17 insertions, 12 deletions
diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua
index 1c581e3f29..aa4331946a 100644
--- a/runtime/lua/vim/treesitter/dev.lua
+++ b/runtime/lua/vim/treesitter/dev.lua
@@ -105,18 +105,23 @@ function TSTreeView:new(bufnr, lang)
-- the root in the child tree to the {injections} table.
local root = parser:parse(true)[1]:root()
local injections = {} ---@type table<integer,table>
- for _, child in pairs(parser:children()) do
- child:for_each_tree(function(tree, ltree)
- local r = tree:root()
- local node = root:named_descendant_for_range(r:range())
- if node then
- injections[node:id()] = {
- lang = ltree:lang(),
- root = r,
- }
- end
- end)
- end
+
+ parser:for_each_tree(function(parent_tree, parent_ltree)
+ local parent = parent_tree:root()
+ for _, child in pairs(parent_ltree:children()) do
+ child:for_each_tree(function(tree, ltree)
+ local r = tree:root()
+ local node = assert(parent:named_descendant_for_range(r:range()))
+ local id = node:id()
+ if not injections[id] or r:byte_length() > injections[id].root:byte_length() then
+ injections[id] = {
+ lang = ltree:lang(),
+ root = r,
+ }
+ end
+ end)
+ end
+ end)
local nodes = traverse(root, 0, parser:lang(), injections, {})