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

This commit is contained in:
Alexander Courtis 2025-09-08 15:28:20 +10:00
parent a20c81733a
commit f030ea6efa
5 changed files with 24 additions and 29 deletions

View File

@ -56,7 +56,7 @@ local function collapse(node, opts)
:iterate()
explorer.renderer:draw()
utils.focus_node_or_parent(node_at_cursor)
explorer:focus_node_or_parent(node_at_cursor)
end

View File

@ -280,7 +280,7 @@ function Filters:toggle(type)
local node = self.explorer:get_node_at_cursor()
self.explorer:reload_explorer()
if node then
utils.focus_node_or_parent(node)
self.explorer:focus_node_or_parent(node)
end
end

View File

@ -607,6 +607,26 @@ function Explorer:focus_file(path)
view.set_cursor({ i + 1, 1 })
end
---Focus node passed as parameter if visible, otherwise focus first visible parent.
---If none of the parents is visible focus root.
---If node is nil do nothing.
---@param node Node? node to focus
function Explorer:focus_node_or_parent(node)
while node do
local found_node, i = utils.find_node(self.nodes, function(node_)
return node_.absolute_path == node.absolute_path
end)
if found_node or node.parent == nil then
require("nvim-tree.view").set_cursor({ i + 1, 1 })
break
end
node = node.parent
end
end
---Api.tree.get_nodes
---@return nvim_tree.api.Node
function Explorer:get_nodes()

View File

@ -227,9 +227,9 @@ function Marks:navigate(up)
end
if up then
utils.focus_node_or_parent(prev or last)
self.explorer:focus_node_or_parent(prev or last)
else
utils.focus_node_or_parent(next or first)
self.explorer:focus_node_or_parent(next or first)
end
end

View File

@ -447,31 +447,6 @@ function M.debounce(context, timeout, callback)
end)
end
---Focus node passed as parameter if visible, otherwise focus first visible parent.
---If none of the parents is visible focus root.
---If node is nil do nothing.
---@param node Node? node to focus
function M.focus_node_or_parent(node)
local explorer = require("nvim-tree.core").get_explorer()
if explorer == nil then
return
end
while node do
local found_node, i = M.find_node(explorer.nodes, function(node_)
return node_.absolute_path == node.absolute_path
end)
if found_node or node.parent == nil then
require("nvim-tree.view").set_cursor({ i + 1, 1 })
break
end
node = node.parent
end
end
---@param path string
---@return integer|nil
---@return integer|nil