From 757bcdda30493d7d8a0efb939a165ae1bd92eeb3 Mon Sep 17 00:00:00 2001 From: Pham Huy Hoang Date: Fri, 17 Nov 2023 11:13:32 +0900 Subject: [PATCH] fix: make InspectTree handle nested injection fixup: Use `TSNode:byte_length` instead --- runtime/lua/vim/treesitter/dev.lua | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua index 39c03cd1a5..59cd62aa9f 100644 --- a/runtime/lua/vim/treesitter/dev.lua +++ b/runtime/lua/vim/treesitter/dev.lua @@ -101,18 +101,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 - 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, {})