@@ -3,55 +3,73 @@ local Iterator = require("nvim-tree.iterators.node-iterator")
33
44local M = {}
55
6- --- @param direction string
7- --- @return fun ( node : Node ): nil
8- function M .fn (direction )
9- return function (node )
10- if node .name == " .." or not direction then
11- return
12- end
13-
14- local explorer = core .get_explorer ()
15- if not explorer then
16- return
17- end
18-
19- local first , last , next , prev = nil , nil , nil , nil
20- local found = false
21- local parent = node .parent or explorer
22- Iterator .builder (parent and parent .nodes or {})
23- :recursor (function ()
24- return nil
25- end )
26- :applier (function (n )
27- first = first or n
28- last = n
29- if n .absolute_path == node .absolute_path then
30- found = true
31- return
32- end
33- prev = not found and n or prev
34- if found and not next then
35- next = n
36- end
37- end )
38- :iterate ()
39-
40- local target_node
41- if direction == " first" then
42- target_node = first
43- elseif direction == " last" then
44- target_node = last
45- elseif direction == " next" then
46- target_node = next or first
47- else
48- target_node = prev or last
49- end
50-
51- if target_node then
52- explorer :focus_node_or_parent (target_node )
53- end
6+ --- @param node Node
7+ --- @param direction " next" | " prev" | " first" | " last"
8+ local function move (node , direction )
9+ if node .name == " .." or not direction then
10+ return
5411 end
12+
13+ local explorer = core .get_explorer ()
14+ if not explorer then
15+ return
16+ end
17+
18+ local first , last , next , prev = nil , nil , nil , nil
19+ local found = false
20+ local parent = node .parent or explorer
21+ Iterator .builder (parent and parent .nodes or {})
22+ :recursor (function ()
23+ return nil
24+ end )
25+ :applier (function (n )
26+ first = first or n
27+ last = n
28+ if n .absolute_path == node .absolute_path then
29+ found = true
30+ return
31+ end
32+ prev = not found and n or prev
33+ if found and not next then
34+ next = n
35+ end
36+ end )
37+ :iterate ()
38+
39+ local target_node
40+ if direction == " first" then
41+ target_node = first
42+ elseif direction == " last" then
43+ target_node = last
44+ elseif direction == " next" then
45+ target_node = next or first
46+ else
47+ target_node = prev or last
48+ end
49+
50+ if target_node then
51+ explorer :focus_node_or_parent (target_node )
52+ end
53+ end
54+
55+ --- @param node Node
56+ function M .next (node )
57+ move (node , " next" )
58+ end
59+
60+ --- @param node Node
61+ function M .prev (node )
62+ move (node , " prev" )
63+ end
64+
65+ --- @param node Node
66+ function M .first (node )
67+ move (node , " first" )
68+ end
69+
70+ --- @param node Node
71+ function M .last (node )
72+ move (node , " last" )
5573end
5674
5775return M
0 commit comments