From f5573fba3d460ddf479826b7404e568cc825ed57 Mon Sep 17 00:00:00 2001
From: Pham Huy Hoang <hoangtun0810@gmail.com>
Date: Fri, 1 Dec 2023 01:37:42 +0900
Subject: [PATCH] fix: Remove nested for_each_tree in TSTreeView (#26328)

Problem:
`LanguageTree:for_each_tree` calls itself for child nodes, so when we
calls `for_each_tree` inside `for_each_tree`, this quickly leads to
exponential tree calls.

Solution:
Use `pairs(child:trees())` directly in this case, as we don't need the
extra callback for each children, this is already handled from the outer
`for_each_tree` call
---
 runtime/lua/vim/treesitter/dev.lua | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua
index aad639d85a..870761b7c7 100644
--- a/runtime/lua/vim/treesitter/dev.lua
+++ b/runtime/lua/vim/treesitter/dev.lua
@@ -87,17 +87,17 @@ function TSTreeView:new(bufnr, lang)
   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)
+      for _, tree in pairs(child:trees()) do
         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(),
+            lang = child:lang(),
             root = r,
           }
         end
-      end)
+      end
     end
   end)