fix(treesitter): fix :InspectTree incorrect injections

(cherry picked from commit 284b0e4fa2)
This commit is contained in:
yuukibarns
2025-03-30 23:06:16 +08:00
committed by github-actions[bot]
parent 4422b9bbd0
commit e342b9a25a

View File

@ -1,5 +1,7 @@
local api = vim.api local api = vim.api
local Range = require('vim.treesitter._range')
local M = {} local M = {}
---@class (private) vim.treesitter.dev.TSTreeView ---@class (private) vim.treesitter.dev.TSTreeView
@ -96,16 +98,20 @@ function TSTreeView:new(bufnr, lang)
parser:for_each_tree(function(parent_tree, parent_ltree) parser:for_each_tree(function(parent_tree, parent_ltree)
local parent = parent_tree:root() local parent = parent_tree:root()
local parent_range = { parent:range() }
for _, child in pairs(parent_ltree:children()) do for _, child in pairs(parent_ltree:children()) do
for _, tree in pairs(child:trees()) do for _, tree in pairs(child:trees()) do
local r = tree:root() local r = tree:root()
local node = assert(parent:named_descendant_for_range(r:range())) local r_range = { r:range() }
local id = node:id() if Range.contains(parent_range, r_range) then
if not injections[id] or r:byte_length() > injections[id].root:byte_length() then local node = assert(parent:named_descendant_for_range(r:range()))
injections[id] = { local id = node:id()
lang = child:lang(), if not injections[id] or r:byte_length() > injections[id].root:byte_length() then
root = r, injections[id] = {
} lang = child:lang(),
root = r,
}
end
end end
end end
end end