Skip to content

Commit 3a3565e

Browse files
committed
refactor(#2942): multi instance: move find_node to Explorer
1 parent 570b905 commit 3a3565e

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

lua/nvim-tree/actions/moves/parent.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
local view = require("nvim-tree.view")
2-
local utils = require("nvim-tree.utils")
3-
42
local DirectoryNode = require("nvim-tree.node.directory")
53

64
local M = {}
@@ -29,7 +27,7 @@ function M.fn(should_close)
2927
return
3028
end
3129

32-
local _, line = utils.find_node(parent.explorer.nodes, function(n)
30+
local _, line = parent.explorer:find_node(function(n)
3331
return n.absolute_path == parent.absolute_path
3432
end)
3533

lua/nvim-tree/explorer/init.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ end
601601

602602
---@param path string
603603
function Explorer:focus_file(path)
604-
local _, i = utils.find_node(self.nodes, function(node)
604+
local _, i = self:find_node(function(node)
605605
return node.absolute_path == path
606606
end)
607607
view.set_cursor({ i + 1, 1 })
@@ -614,7 +614,7 @@ end
614614
function Explorer:focus_node_or_parent(node)
615615

616616
while node do
617-
local found_node, i = utils.find_node(self.nodes, function(node_)
617+
local found_node, i = self:find_node(function(node_)
618618
return node_.absolute_path == node.absolute_path
619619
end)
620620

@@ -627,6 +627,25 @@ function Explorer:focus_node_or_parent(node)
627627
end
628628
end
629629

630+
--- Get the node and index of the node from the tree that matches the predicate.
631+
--- The explored nodes are those displayed on the view.
632+
---@param fn fun(node: Node): boolean
633+
---@return table|nil
634+
---@return number
635+
function Explorer:find_node(fn)
636+
local node, i = Iterator.builder(self.nodes)
637+
:matcher(fn)
638+
:recursor(function(node)
639+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
640+
end)
641+
:iterate()
642+
i = view.is_root_folder_visible() and i or i - 1
643+
if node and node.explorer.live_filter.filter then
644+
i = i + 1
645+
end
646+
return node, i
647+
end
648+
630649
---Api.tree.get_nodes
631650
---@return nvim_tree.api.Node
632651
function Explorer:get_nodes()

lua/nvim-tree/utils.lua

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,6 @@ end
114114

115115
M.path_separator = path_separator
116116

117-
--- Get the node and index of the node from the tree that matches the predicate.
118-
--- The explored nodes are those displayed on the view.
119-
---@param nodes Node[]
120-
---@param fn fun(node: Node): boolean
121-
---@return table|nil
122-
---@return number
123-
function M.find_node(nodes, fn)
124-
local node, i = Iterator.builder(nodes)
125-
:matcher(fn)
126-
:recursor(function(node)
127-
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
128-
end)
129-
:iterate()
130-
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
131-
if node and node.explorer.live_filter.filter then
132-
i = i + 1
133-
end
134-
return node, i
135-
end
136-
137117
---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
138118
---@return number
139119
function M.extmarks_length(extmarks)

0 commit comments

Comments
 (0)