Skip to content

Commit e179ad2

Browse files
authored
refactor(#2942): multi instance: move utils functions to Explorer methods (#3200)
* refactor(#2942): multi instance: move find_node_line to Explorer * refactor(#2942): multi instance: move get_node_from_path to Explorer * refactor(#2942): multi instance: move focus_file to Explorer * refactor(#2942): multi instance: move focus_node_or_parent to Explorer * refactor(#2942): multi instance: move get_node_from_path to Explorer * refactor(#2942): multi instance: move find_node to Explorer * refactor(#2942): multi instance: move get_nodes_by_line to Explorer * refactor(#2942): multi instance: remove unnecessary focus_file * refactor(#2942): style
1 parent f92cc3a commit e179ad2

File tree

11 files changed

+135
-153
lines changed

11 files changed

+135
-153
lines changed

lua/nvim-tree/actions/finders/find-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function M.fn(path)
3232
local profile = log.profile_start("find file %s", path_real)
3333

3434
-- refresh the contents of all parents, expanding groups as needed
35-
if utils.get_node_from_path(path_real) == nil then
35+
if explorer:get_node_from_path(path_real) == nil then
3636
explorer:refresh_parent_nodes_for_path(vim.fn.fnamemodify(path_real, ":h"))
3737
end
3838

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local utils = require("nvim-tree.utils")
21
local view = require("nvim-tree.view")
32
local core = require("nvim-tree.core")
43
local diagnostics = require("nvim-tree.diagnostics")
@@ -36,7 +35,7 @@ end
3635
---@param skip_gitignored boolean? default false
3736
local function move(explorer, where, what, skip_gitignored)
3837
local first_node_line = core.get_nodes_starting_line()
39-
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
38+
local nodes_by_line = explorer:get_nodes_by_line(first_node_line)
4039
local iter_start, iter_end, iter_step, cur, first, nex
4140

4241
local cursor = explorer:get_cursor_position()
@@ -191,7 +190,7 @@ local function move_prev_recursive(explorer, what, skip_gitignored)
191190
if node_init.name == ".." then -- root node
192191
view.set_cursor({ 1, 0 }) -- move to root node (position 1)
193192
else
194-
local node_init_line = utils.find_node_line(node_init)
193+
local node_init_line = explorer:find_node_line(node_init)
195194
if node_init_line < 0 then
196195
return
197196
end

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/actions/moves/sibling.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local utils = require("nvim-tree.utils")
21
local core = require("nvim-tree.core")
32
local Iterator = require("nvim-tree.iterators.node-iterator")
43

@@ -12,9 +11,14 @@ function M.fn(direction)
1211
return
1312
end
1413

14+
local explorer = core.get_explorer()
15+
if not explorer then
16+
return
17+
end
18+
1519
local first, last, next, prev = nil, nil, nil, nil
1620
local found = false
17-
local parent = node.parent or core.get_explorer()
21+
local parent = node.parent or explorer
1822
Iterator.builder(parent and parent.nodes or {})
1923
:recursor(function()
2024
return nil
@@ -45,7 +49,7 @@ function M.fn(direction)
4549
end
4650

4751
if target_node then
48-
utils.focus_file(target_node.absolute_path)
52+
explorer:focus_node_or_parent(target_node)
4953
end
5054
end
5155
end

lua/nvim-tree/actions/tree/modifiers/collapse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ local function collapse(node, opts)
5656
:iterate()
5757

5858
explorer.renderer:draw()
59-
utils.focus_node_or_parent(node_at_cursor)
59+
explorer:focus_node_or_parent(node_at_cursor)
6060
end
6161

6262

lua/nvim-tree/explorer/filters.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function Filters:toggle(type)
280280
local node = self.explorer:get_node_at_cursor()
281281
self.explorer:reload_explorer()
282282
if node then
283-
utils.focus_node_or_parent(node)
283+
self.explorer:focus_node_or_parent(node)
284284
end
285285
end
286286

lua/nvim-tree/explorer/init.lua

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ function Explorer:get_node_at_cursor()
527527
return self
528528
end
529529

530-
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]]
530+
return self:get_nodes_by_line(core.get_nodes_starting_line())[cursor[1]]
531531
end
532532

533533
function Explorer:place_cursor_on_node()
@@ -551,6 +551,114 @@ function Explorer:place_cursor_on_node()
551551
end
552552
end
553553

554+
-- Find the line number of a node.
555+
---@param node Node?
556+
---@return integer -1 not found
557+
function Explorer:find_node_line(node)
558+
if not node then
559+
return -1
560+
end
561+
562+
local first_node_line = core.get_nodes_starting_line()
563+
local nodes_by_line = self:get_nodes_by_line(first_node_line)
564+
local iter_start, iter_end = first_node_line, #nodes_by_line
565+
566+
for line = iter_start, iter_end, 1 do
567+
if nodes_by_line[line] == node then
568+
return line
569+
end
570+
end
571+
572+
return -1
573+
end
574+
575+
-- get the node in the tree state depending on the absolute path of the node
576+
-- (grouped or hidden too)
577+
---@param path string
578+
---@return Node|nil
579+
---@return number|nil
580+
function Explorer:get_node_from_path(path)
581+
if self.absolute_path == path then
582+
return self
583+
end
584+
585+
return Iterator.builder(self.nodes)
586+
:hidden()
587+
:matcher(function(node)
588+
return node.absolute_path == path or node.link_to == path
589+
end)
590+
:recursor(function(node)
591+
if node.group_next then
592+
return { node.group_next }
593+
end
594+
if node.nodes then
595+
return node.nodes
596+
end
597+
end)
598+
:iterate()
599+
end
600+
601+
---Focus node passed as parameter if visible, otherwise focus first visible parent.
602+
---If none of the parents is visible focus root.
603+
---If node is nil do nothing.
604+
---@param node Node? node to focus
605+
function Explorer:focus_node_or_parent(node)
606+
while node do
607+
local found_node, i = self:find_node(function(node_)
608+
return node_.absolute_path == node.absolute_path
609+
end)
610+
611+
if found_node or node.parent == nil then
612+
view.set_cursor({ i + 1, 1 })
613+
break
614+
end
615+
616+
node = node.parent
617+
end
618+
end
619+
620+
--- Get the node and index of the node from the tree that matches the predicate.
621+
--- The explored nodes are those displayed on the view.
622+
---@param fn fun(node: Node): boolean
623+
---@return table|nil
624+
---@return number
625+
function Explorer:find_node(fn)
626+
local node, i = Iterator.builder(self.nodes)
627+
:matcher(fn)
628+
:recursor(function(node)
629+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
630+
end)
631+
:iterate()
632+
i = view.is_root_folder_visible() and i or i - 1
633+
if node and node.explorer.live_filter.filter then
634+
i = i + 1
635+
end
636+
return node, i
637+
end
638+
639+
--- Return visible nodes indexed by line
640+
---@param line_start number
641+
---@return table
642+
function Explorer:get_nodes_by_line(line_start)
643+
local nodes_by_line = {}
644+
local line = line_start
645+
646+
Iterator.builder(self.nodes)
647+
:applier(function(node)
648+
if node.group_next then
649+
return
650+
end
651+
nodes_by_line[line] = node
652+
line = line + 1
653+
end)
654+
:recursor(function(node)
655+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
656+
end)
657+
:iterate()
658+
659+
return nodes_by_line
660+
end
661+
554662
---Api.tree.get_nodes
555663
---@return nvim_tree.api.Node
556664
function Explorer:get_nodes()

lua/nvim-tree/explorer/live-filter.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ function LiveFilter:clear_filter()
220220
self.explorer.renderer:draw()
221221

222222
if node then
223-
utils.focus_file(node.absolute_path)
223+
self.explorer:focus_node_or_parent(node)
224224
elseif last_node then
225-
utils.focus_file(last_node.absolute_path)
225+
self.explorer:focus_node_or_parent(last_node)
226226
end
227227
end
228228

lua/nvim-tree/git/init.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ local function reload_tree_at(toplevel)
233233
end
234234

235235
log.line("watcher", "git event executing '%s'", toplevel)
236-
local root_node = utils.get_node_from_path(toplevel)
236+
237+
local explorer = require("nvim-tree.core").get_explorer()
238+
if not explorer then
239+
return nil
240+
end
241+
242+
local root_node = explorer:get_node_from_path(toplevel)
237243
if not root_node then
238244
return
239245
end
@@ -252,7 +258,7 @@ local function reload_tree_at(toplevel)
252258
end)
253259
:iterate()
254260

255-
root_node.explorer.renderer:draw()
261+
explorer.renderer:draw()
256262
end)
257263
end
258264

lua/nvim-tree/marks/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ function Marks:navigate(up)
227227
end
228228

229229
if up then
230-
utils.focus_node_or_parent(prev or last)
230+
self.explorer:focus_node_or_parent(prev or last)
231231
else
232-
utils.focus_node_or_parent(next or first)
232+
self.explorer:focus_node_or_parent(next or first)
233233
end
234234
end
235235

@@ -263,7 +263,7 @@ function Marks:navigate_select()
263263
if node and not node:is(DirectoryNode) and not utils.get_win_buf_from_path(node.absolute_path) then
264264
open_file.fn("edit", node.absolute_path)
265265
elseif node then
266-
utils.focus_file(node.absolute_path)
266+
self.explorer:focus_node_or_parent(node)
267267
end
268268
end)
269269
end

0 commit comments

Comments
 (0)