Skip to content

Commit b32c883

Browse files
authored
feat(movement): allow circular movement for sibling next and prev (#1416)
1 parent 449b5bd commit b32c883

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

lua/nvim-tree/actions/dispatch.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ local Actions = {
2828

2929
-- Movements in tree
3030
close_node = require("nvim-tree.actions.moves.parent").fn(true),
31-
first_sibling = require("nvim-tree.actions.moves.sibling").fn(-math.huge),
32-
last_sibling = require("nvim-tree.actions.moves.sibling").fn(math.huge),
31+
first_sibling = require("nvim-tree.actions.moves.sibling").fn "first",
32+
last_sibling = require("nvim-tree.actions.moves.sibling").fn "last",
3333
next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"),
3434
next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"),
35-
next_sibling = require("nvim-tree.actions.moves.sibling").fn(1),
35+
next_sibling = require("nvim-tree.actions.moves.sibling").fn "next",
3636
parent_node = require("nvim-tree.actions.moves.parent").fn(false),
3737
prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"),
3838
prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"),
39-
prev_sibling = require("nvim-tree.actions.moves.sibling").fn(-1),
39+
prev_sibling = require("nvim-tree.actions.moves.sibling").fn "prev",
4040

4141
-- Other types
4242
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
11
local utils = require "nvim-tree.utils"
2-
local view = require "nvim-tree.view"
32
local core = require "nvim-tree.core"
4-
local lib = require "nvim-tree.lib"
3+
local Iterator = require "nvim-tree.iterators.node-iterator"
54

65
local M = {}
76

8-
local function get_index_of(node, nodes)
9-
local node_path = node.absolute_path
10-
local line = 1
11-
12-
for _, _node in ipairs(nodes) do
13-
if not _node.hidden then
14-
local n = lib.get_last_group_node(_node)
15-
if node_path == n.absolute_path then
16-
return line
17-
end
18-
19-
line = line + 1
20-
end
21-
end
22-
end
23-
247
function M.fn(direction)
258
return function(node)
269
if node.name == ".." or not direction then
2710
return
2811
end
2912

13+
local first, last, next, prev = nil, nil, nil, nil
14+
local found = false
3015
local parent = node.parent or core.get_explorer()
31-
local parent_nodes = vim.tbl_filter(function(n)
32-
return not n.hidden
33-
end, parent.nodes)
34-
35-
local node_index = get_index_of(node, parent_nodes)
36-
37-
local target_idx = node_index + direction
38-
if target_idx < 1 then
39-
target_idx = 1
40-
elseif target_idx > #parent_nodes then
41-
target_idx = #parent_nodes
16+
Iterator.builder(parent.nodes)
17+
:recursor(function()
18+
return nil
19+
end)
20+
:applier(function(n)
21+
first = first or n
22+
last = n
23+
if n.absolute_path == node.absolute_path then
24+
found = true
25+
return
26+
end
27+
prev = not found and n or prev
28+
if found and not next then
29+
next = n
30+
end
31+
end)
32+
:iterate()
33+
34+
local target_node
35+
if direction == "first" then
36+
target_node = first
37+
elseif direction == "last" then
38+
target_node = last
39+
elseif direction == "next" then
40+
target_node = next or first
41+
else
42+
target_node = prev or last
4243
end
4344

44-
local target_node = parent_nodes[target_idx]
45-
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
46-
return n.absolute_path == target_node.absolute_path
47-
end)
48-
49-
view.set_cursor { line + 1, 0 }
45+
if target_node then
46+
utils.focus_file(target_node.absolute_path)
47+
end
5048
end
5149
end
5250

0 commit comments

Comments
 (0)