Skip to content

Commit 71ec2c2

Browse files
committed
feat(siblings): allow circular siblings
1 parent 3946730 commit 71ec2c2

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
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: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
33
local core = require "nvim-tree.core"
44
local lib = require "nvim-tree.lib"
5+
local Iterator = require "nvim-tree.iterators.node-iterator"
56

67
local M = {}
78

@@ -27,26 +28,41 @@ function M.fn(direction)
2728
return
2829
end
2930

31+
local first, last, next, prev = nil, nil, nil, nil
32+
local found = false
3033
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+
Iterator.builder(parent.nodes)
35+
:recursor(function()
36+
return nil
37+
end)
38+
:applier(function(n)
39+
first = first or n
40+
last = n
41+
if n.absolute_path == node.absolute_path then
42+
found = true
43+
return
44+
end
45+
prev = not found and n or prev
46+
if found and not next then
47+
next = n
48+
end
49+
end)
50+
:iterate()
3451

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
52+
local target_node
53+
if direction == "first" then
54+
target_node = first
55+
elseif direction == "last" then
56+
target_node = last
57+
elseif direction == "next" then
58+
target_node = next or first
59+
else
60+
target_node = prev or last
4261
end
4362

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 }
63+
if target_node then
64+
utils.focus_file(target_node.absolute_path)
65+
end
5066
end
5167
end
5268

0 commit comments

Comments
 (0)