Skip to content

Commit 2d2cbe6

Browse files
committed
refactor(actions): split movements into multiple modules
1 parent 831f115 commit 2d2cbe6

File tree

5 files changed

+159
-144
lines changed

5 files changed

+159
-144
lines changed

lua/nvim-tree/actions/dispatch.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ local Actions = {
2727
rename = require("nvim-tree.actions.fs.rename-file").fn(false),
2828

2929
-- Movements in tree
30-
close_node = require("nvim-tree.actions.moves.movements").parent_node(true),
31-
first_sibling = require("nvim-tree.actions.moves.movements").sibling(-math.huge),
32-
last_sibling = require("nvim-tree.actions.moves.movements").sibling(math.huge),
33-
next_diag_item = require("nvim-tree.actions.moves.movements").find_item("next", "diag"),
34-
next_git_item = require("nvim-tree.actions.moves.movements").find_item("next", "git"),
35-
next_sibling = require("nvim-tree.actions.moves.movements").sibling(1),
36-
parent_node = require("nvim-tree.actions.moves.movements").parent_node(false),
37-
prev_diag_item = require("nvim-tree.actions.moves.movements").find_item("prev", "diag"),
38-
prev_git_item = require("nvim-tree.actions.moves.movements").find_item("prev", "git"),
39-
prev_sibling = require("nvim-tree.actions.moves.movements").sibling(-1),
30+
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),
33+
next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"),
34+
next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"),
35+
next_sibling = require("nvim-tree.actions.moves.sibling").fn(1),
36+
parent_node = require("nvim-tree.actions.moves.parent").fn(false),
37+
prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"),
38+
prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"),
39+
prev_sibling = require("nvim-tree.actions.moves.sibling").fn(-1),
4040

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

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
local utils = require "nvim-tree.utils"
2+
local view = require "nvim-tree.view"
3+
local core = require "nvim-tree.core"
4+
local lib = require "nvim-tree.lib"
5+
6+
local M = {}
7+
8+
function M.fn(where, what)
9+
return function()
10+
local node_cur = lib.get_node_at_cursor()
11+
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
12+
13+
local cur, first, prev, nex = nil, nil, nil, nil
14+
for line, node in pairs(nodes_by_line) do
15+
local valid = false
16+
if what == "git" then
17+
valid = node.git_status ~= nil
18+
elseif what == "diag" then
19+
valid = node.diag_status ~= nil
20+
end
21+
22+
if not first and valid then
23+
first = line
24+
end
25+
26+
if node == node_cur then
27+
cur = line
28+
elseif valid then
29+
if not cur then
30+
prev = line
31+
end
32+
if cur and not nex then
33+
nex = line
34+
break
35+
end
36+
end
37+
end
38+
39+
if where == "prev" then
40+
if prev then
41+
view.set_cursor { prev, 0 }
42+
end
43+
else
44+
if cur then
45+
if nex then
46+
view.set_cursor { nex, 0 }
47+
end
48+
elseif first then
49+
view.set_cursor { first, 0 }
50+
end
51+
end
52+
end
53+
end
54+
55+
return M

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

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local renderer = require "nvim-tree.renderer"
2+
local view = require "nvim-tree.view"
3+
local utils = require "nvim-tree.utils"
4+
local core = require "nvim-tree.core"
5+
6+
local M = {}
7+
8+
function M.fn(should_close)
9+
should_close = should_close or false
10+
11+
return function(node)
12+
if should_close and node.open then
13+
node.open = false
14+
return renderer.draw()
15+
end
16+
17+
local parent = node.parent
18+
19+
if renderer.config.group_empty and parent then
20+
while parent.parent and parent.parent.group_next do
21+
parent = parent.parent
22+
end
23+
end
24+
25+
if not parent or not parent.parent then
26+
return view.set_cursor { 1, 0 }
27+
end
28+
29+
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
30+
return n.absolute_path == parent.absolute_path
31+
end)
32+
33+
view.set_cursor { line + 1, 0 }
34+
if should_close then
35+
parent.open = false
36+
renderer.draw()
37+
end
38+
end
39+
end
40+
41+
return M
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local utils = require "nvim-tree.utils"
2+
local view = require "nvim-tree.view"
3+
local core = require "nvim-tree.core"
4+
local lib = require "nvim-tree.lib"
5+
6+
local M = {}
7+
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+
24+
function M.fn(direction)
25+
return function(node)
26+
if node.name == ".." or not direction then
27+
return
28+
end
29+
30+
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
42+
end
43+
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 }
50+
end
51+
end
52+
53+
return M

0 commit comments

Comments
 (0)