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.
This commit is contained in:
Pham Huy Hoang 2023-11-29 23:16:52 +09:00 committed by GitHub
parent 7bc5ee7f93
commit b6e339eb90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,18 +105,23 @@ function TSTreeView:new(bufnr, lang)
-- the root in the child tree to the {injections} table. -- the root in the child tree to the {injections} table.
local root = parser:parse(true)[1]:root() local root = parser:parse(true)[1]:root()
local injections = {} ---@type table<integer,table> local injections = {} ---@type table<integer,table>
for _, child in pairs(parser:children()) do
child:for_each_tree(function(tree, ltree) parser:for_each_tree(function(parent_tree, parent_ltree)
local r = tree:root() local parent = parent_tree:root()
local node = root:named_descendant_for_range(r:range()) for _, child in pairs(parent_ltree:children()) do
if node then child:for_each_tree(function(tree, ltree)
injections[node:id()] = { local r = tree:root()
lang = ltree:lang(), local node = assert(parent:named_descendant_for_range(r:range()))
root = r, local id = node:id()
} if not injections[id] or r:byte_length() > injections[id].root:byte_length() then
end injections[id] = {
end) lang = ltree:lang(),
end root = r,
}
end
end)
end
end)
local nodes = traverse(root, 0, parser:lang(), injections, {}) local nodes = traverse(root, 0, parser:lang(), injections, {})