Skip to content

Commit 6d5bf5f

Browse files
committed
refactor(#3255): wire node.navigate.sibling API impl directly to functions
1 parent 9197f3e commit 6d5bf5f

2 files changed

Lines changed: 70 additions & 52 deletions

File tree

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

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,73 @@ local Iterator = require("nvim-tree.iterators.node-iterator")
33

44
local 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")
5573
end
5674

5775
return M

lua/nvim-tree/api/impl/post.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ function M.hydrate(api)
227227
api.node.run.cmd = wrap_node(actions.node.run_command.run_file_command)
228228
api.node.run.system = wrap_node(actions.node.system_open.fn)
229229

230-
api.node.navigate.sibling.next = wrap_node(actions.moves.sibling.fn("next"))
231-
api.node.navigate.sibling.prev = wrap_node(actions.moves.sibling.fn("prev"))
232-
api.node.navigate.sibling.first = wrap_node(actions.moves.sibling.fn("first"))
233-
api.node.navigate.sibling.last = wrap_node(actions.moves.sibling.fn("last"))
230+
api.node.navigate.sibling.next = wrap_node(actions.moves.sibling.next)
231+
api.node.navigate.sibling.prev = wrap_node(actions.moves.sibling.prev)
232+
api.node.navigate.sibling.first = wrap_node(actions.moves.sibling.first)
233+
api.node.navigate.sibling.last = wrap_node(actions.moves.sibling.last)
234234
api.node.navigate.parent = wrap_node(actions.moves.parent.fn(false))
235235
api.node.navigate.parent_close = wrap_node(actions.moves.parent.fn(true))
236236
api.node.navigate.git.next = wrap_node(actions.moves.item.fn({ where = "next", what = "git" }))

0 commit comments

Comments
 (0)