refactor(#2942): multi instance: move get_node_from_path to Explorer

This commit is contained in:
Alexander Courtis 2025-09-08 15:10:38 +10:00
parent d087a8204d
commit 29d8c83ba8
3 changed files with 36 additions and 3 deletions

View File

@ -32,7 +32,7 @@ function M.fn(path)
local profile = log.profile_start("find file %s", path_real) local profile = log.profile_start("find file %s", path_real)
-- refresh the contents of all parents, expanding groups as needed -- 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")) explorer:refresh_parent_nodes_for_path(vim.fn.fnamemodify(path_real, ":h"))
end end

View File

@ -572,6 +572,33 @@ function Explorer:find_node_line(node)
return -1 return -1
end 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 ---Api.tree.get_nodes
---@return nvim_tree.api.Node ---@return nvim_tree.api.Node
function Explorer:get_nodes() function Explorer:get_nodes()

View File

@ -233,7 +233,13 @@ local function reload_tree_at(toplevel)
end end
log.line("watcher", "git event executing '%s'", toplevel) 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 if not root_node then
return return
end end
@ -252,7 +258,7 @@ local function reload_tree_at(toplevel)
end) end)
:iterate() :iterate()
root_node.explorer.renderer:draw() explorer.renderer:draw()
end) end)
end end