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

This commit is contained in:
Alexander Courtis 2025-09-08 16:00:09 +10:00
parent 570b905c78
commit 3a3565ed84
3 changed files with 22 additions and 25 deletions

View File

@ -1,6 +1,4 @@
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {}
@ -29,7 +27,7 @@ function M.fn(should_close)
return
end
local _, line = utils.find_node(parent.explorer.nodes, function(n)
local _, line = parent.explorer:find_node(function(n)
return n.absolute_path == parent.absolute_path
end)

View File

@ -601,7 +601,7 @@ end
---@param path string
function Explorer:focus_file(path)
local _, i = utils.find_node(self.nodes, function(node)
local _, i = self:find_node(function(node)
return node.absolute_path == path
end)
view.set_cursor({ i + 1, 1 })
@ -614,7 +614,7 @@ end
function Explorer:focus_node_or_parent(node)
while node do
local found_node, i = utils.find_node(self.nodes, function(node_)
local found_node, i = self:find_node(function(node_)
return node_.absolute_path == node.absolute_path
end)
@ -627,6 +627,25 @@ function Explorer:focus_node_or_parent(node)
end
end
--- Get the node and index of the node from the tree that matches the predicate.
--- The explored nodes are those displayed on the view.
---@param fn fun(node: Node): boolean
---@return table|nil
---@return number
function Explorer:find_node(fn)
local node, i = Iterator.builder(self.nodes)
:matcher(fn)
:recursor(function(node)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end)
:iterate()
i = view.is_root_folder_visible() and i or i - 1
if node and node.explorer.live_filter.filter then
i = i + 1
end
return node, i
end
---Api.tree.get_nodes
---@return nvim_tree.api.Node
function Explorer:get_nodes()

View File

@ -114,26 +114,6 @@ end
M.path_separator = path_separator
--- Get the node and index of the node from the tree that matches the predicate.
--- The explored nodes are those displayed on the view.
---@param nodes Node[]
---@param fn fun(node: Node): boolean
---@return table|nil
---@return number
function M.find_node(nodes, fn)
local node, i = Iterator.builder(nodes)
:matcher(fn)
:recursor(function(node)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end)
:iterate()
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
if node and node.explorer.live_filter.filter then
i = i + 1
end
return node, i
end
---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
---@return number
function M.extmarks_length(extmarks)