Skip to content

Commit 0a99c4a

Browse files
feat: allow cycling on git/diagnostic/opened files navigation (#2506)
* feat: allow cycling on git/diagnostic/opened files navigation * luacheck * Remove useless nil check * Cycle only if `wrapscan` is enabled --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent c763861 commit 0a99c4a

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ local M = {}
99
function M.fn(where, what)
1010
return function()
1111
local node_cur = lib.get_node_at_cursor()
12-
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
12+
local first_node_line = core.get_nodes_starting_line()
13+
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, first_node_line)
14+
local iter_start, iter_end, iter_step, cur, first, nex
1315

14-
local cur, first, prev, nex = nil, nil, nil, nil
15-
for line, node in pairs(nodes_by_line) do
16+
if where == "next" then
17+
iter_start, iter_end, iter_step = first_node_line, #nodes_by_line, 1
18+
elseif where == "prev" then
19+
iter_start, iter_end, iter_step = #nodes_by_line, first_node_line, -1
20+
end
21+
22+
for line = iter_start, iter_end, iter_step do
23+
local node = nodes_by_line[line]
1624
local valid = false
25+
1726
if what == "git" then
1827
valid = explorer_node.get_git_status(node) ~= nil
1928
elseif what == "diag" then
@@ -28,29 +37,16 @@ function M.fn(where, what)
2837

2938
if node == node_cur then
3039
cur = line
31-
elseif valid then
32-
if not cur then
33-
prev = line
34-
end
35-
if cur and not nex then
36-
nex = line
37-
break
38-
end
40+
elseif valid and cur then
41+
nex = line
42+
break
3943
end
4044
end
4145

42-
if where == "prev" then
43-
if prev then
44-
view.set_cursor { prev, 0 }
45-
end
46-
else
47-
if cur then
48-
if nex then
49-
view.set_cursor { nex, 0 }
50-
end
51-
elseif first then
52-
view.set_cursor { first, 0 }
53-
end
46+
if nex then
47+
view.set_cursor { nex, 0 }
48+
elseif vim.o.wrapscan and first then
49+
view.set_cursor { first, 0 }
5450
end
5551
end
5652
end

0 commit comments

Comments
 (0)