File tree 2 files changed +26
-19
lines changed 2 files changed +26
-19
lines changed Original file line number Diff line number Diff line change 1
1
local lib = require " nvim-tree.lib"
2
- local core = require " nvim-tree.core"
3
2
local utils = require " nvim-tree.utils"
4
3
local filters = require " nvim-tree.explorer.filters"
5
4
local reloaders = require " nvim-tree.actions.reloaders.reloaders"
@@ -9,24 +8,7 @@ local M = {}
9
8
local function reload ()
10
9
local node = lib .get_node_at_cursor ()
11
10
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 )
30
12
end
31
13
32
14
function M .custom ()
Original file line number Diff line number Diff line change @@ -354,6 +354,31 @@ function M.focus_file(path)
354
354
require (" nvim-tree.view" ).set_cursor { i + 1 , 1 }
355
355
end
356
356
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
+
357
382
function M .get_win_buf_from_path (path )
358
383
for _ , w in pairs (vim .api .nvim_tabpage_list_wins (0 )) do
359
384
local b = vim .api .nvim_win_get_buf (w )
You can’t perform that action at this time.
0 commit comments