Skip to content

Commit 576d4c1

Browse files
refactor: move reload function into utils module (#2247)
* refactor: move `reload` function into `utils` module * docs: add annotations to `utils.focus_node_or_parent` --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent f5d970d commit 576d4c1

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local lib = require "nvim-tree.lib"
2-
local core = require "nvim-tree.core"
32
local utils = require "nvim-tree.utils"
43
local filters = require "nvim-tree.explorer.filters"
54
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
@@ -9,24 +8,7 @@ local M = {}
98
local function reload()
109
local node = lib.get_node_at_cursor()
1110
reloaders.reload_explorer()
12-
local explorer = core.get_explorer()
13-
14-
if explorer == nil then
15-
return
16-
end
17-
18-
while node do
19-
local found_node, _ = utils.find_node(explorer.nodes, function(node_)
20-
return node_.absolute_path == node.absolute_path
21-
end)
22-
23-
if found_node or node.parent == nil then
24-
utils.focus_file(node.absolute_path)
25-
break
26-
end
27-
28-
node = node.parent
29-
end
11+
utils.focus_node_or_parent(node)
3012
end
3113

3214
function M.custom()

lua/nvim-tree/utils.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,31 @@ function M.focus_file(path)
354354
require("nvim-tree.view").set_cursor { i + 1, 1 }
355355
end
356356

357+
---Focus node passed as parameter if visible, otherwise focus first visible parent.
358+
---If none of the parents is visible focus root.
359+
---If node is nil do nothing.
360+
---@param node table|nil node to focus
361+
function M.focus_node_or_parent(node)
362+
local explorer = require("nvim-tree.core").get_explorer()
363+
364+
if explorer == nil then
365+
return
366+
end
367+
368+
while node do
369+
local found_node, i = M.find_node(explorer.nodes, function(node_)
370+
return node_.absolute_path == node.absolute_path
371+
end)
372+
373+
if found_node or node.parent == nil then
374+
require("nvim-tree.view").set_cursor { i + 1, 1 }
375+
break
376+
end
377+
378+
node = node.parent
379+
end
380+
end
381+
357382
function M.get_win_buf_from_path(path)
358383
for _, w in pairs(vim.api.nvim_tabpage_list_wins(0)) do
359384
local b = vim.api.nvim_win_get_buf(w)

0 commit comments

Comments
 (0)