Skip to content

Commit f030ea6

Browse files
committed
refactor(#2942): multi instance: move focus_node_or_parent to Explorer
1 parent a20c817 commit f030ea6

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,26 @@ function Explorer:focus_file(path)
607607
view.set_cursor({ i + 1, 1 })
608608
end
609609

610+
---Focus node passed as parameter if visible, otherwise focus first visible parent.
611+
---If none of the parents is visible focus root.
612+
---If node is nil do nothing.
613+
---@param node Node? node to focus
614+
function Explorer:focus_node_or_parent(node)
615+
616+
while node do
617+
local found_node, i = utils.find_node(self.nodes, function(node_)
618+
return node_.absolute_path == node.absolute_path
619+
end)
620+
621+
if found_node or node.parent == nil then
622+
require("nvim-tree.view").set_cursor({ i + 1, 1 })
623+
break
624+
end
625+
626+
node = node.parent
627+
end
628+
end
629+
610630
---Api.tree.get_nodes
611631
---@return nvim_tree.api.Node
612632
function Explorer:get_nodes()

lua/nvim-tree/marks/init.lua

Lines changed: 2 additions & 2 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

lua/nvim-tree/utils.lua

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -447,31 +447,6 @@ function M.debounce(context, timeout, callback)
447447
end)
448448
end
449449

450-
---Focus node passed as parameter if visible, otherwise focus first visible parent.
451-
---If none of the parents is visible focus root.
452-
---If node is nil do nothing.
453-
---@param node Node? node to focus
454-
function M.focus_node_or_parent(node)
455-
local explorer = require("nvim-tree.core").get_explorer()
456-
457-
if explorer == nil then
458-
return
459-
end
460-
461-
while node do
462-
local found_node, i = M.find_node(explorer.nodes, function(node_)
463-
return node_.absolute_path == node.absolute_path
464-
end)
465-
466-
if found_node or node.parent == nil then
467-
require("nvim-tree.view").set_cursor({ i + 1, 1 })
468-
break
469-
end
470-
471-
node = node.parent
472-
end
473-
end
474-
475450
---@param path string
476451
---@return integer|nil
477452
---@return integer|nil

0 commit comments

Comments
 (0)