From 29d8c83ba87a58abf8d485e3496a0bc3a367127d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 8 Sep 2025 15:10:38 +1000 Subject: [PATCH] refactor(#2942): multi instance: move get_node_from_path to Explorer --- lua/nvim-tree/actions/finders/find-file.lua | 2 +- lua/nvim-tree/explorer/init.lua | 27 +++++++++++++++++++++ lua/nvim-tree/git/init.lua | 10 ++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 55e2f23d..4f5d8089 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -32,7 +32,7 @@ function M.fn(path) local profile = log.profile_start("find file %s", path_real) -- refresh the contents of all parents, expanding groups as needed - if utils.get_node_from_path(path_real) == nil then + if explorer:get_node_from_path(path_real) == nil then explorer:refresh_parent_nodes_for_path(vim.fn.fnamemodify(path_real, ":h")) end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index c81ce927..a3cadd9c 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -572,6 +572,33 @@ function Explorer:find_node_line(node) return -1 end +-- get the node in the tree state depending on the absolute path of the node +-- (grouped or hidden too) +---@param path string +---@return Node|nil +---@return number|nil +function Explorer:get_node_from_path(path) + + if self.absolute_path == path then + return self + end + + return Iterator.builder(self.nodes) + :hidden() + :matcher(function(node) + return node.absolute_path == path or node.link_to == path + end) + :recursor(function(node) + if node.group_next then + return { node.group_next } + end + if node.nodes then + return node.nodes + end + end) + :iterate() +end + ---Api.tree.get_nodes ---@return nvim_tree.api.Node function Explorer:get_nodes() diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 663e57c3..6277a6a4 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -233,7 +233,13 @@ local function reload_tree_at(toplevel) end log.line("watcher", "git event executing '%s'", toplevel) - local root_node = utils.get_node_from_path(toplevel) + + local explorer = require("nvim-tree.core").get_explorer() + if not explorer then + return nil + end + + local root_node = explorer:get_node_from_path(toplevel) if not root_node then return end @@ -252,7 +258,7 @@ local function reload_tree_at(toplevel) end) :iterate() - root_node.explorer.renderer:draw() + explorer.renderer:draw() end) end