Skip to content

Commit 8e9126a

Browse files
committed
refactor(#3255): add visual mode wrappers
1 parent 2b8aa02 commit 8e9126a

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

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

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,43 @@
55

66
local M = {}
77

8-
---Return a function wrapper that calls fn.
9-
---Injects node or node at cursor as first argument.
10-
---Passes other arguments verbatim.
8+
---Returns the node under the cursor.
9+
---@return Node?
10+
local function node_at_cursor()
11+
local e = require("nvim-tree.core").get_explorer()
12+
return e and e:get_node_at_cursor() or nil
13+
end
14+
15+
---Returns the visually selected nodes, if we are in visual mode.
16+
---@return Node[]?
17+
local function visual_nodes()
18+
local utils = require("nvim-tree.utils")
19+
return utils.is_visual_mode() and utils.get_visual_nodes() or nil
20+
end
21+
22+
---Injects:
23+
---- n: n or node at cursor
1124
---@param fn fun(n?: Node, ...): any
1225
---@return fun(n?: Node, ...): any
1326
local function _n(fn)
1427
return function(n, ...)
15-
if not n then
16-
local e = require("nvim-tree.core").get_explorer()
17-
n = e and e:get_node_at_cursor() or nil
18-
end
19-
return fn(n, ...)
28+
return fn(n or node_at_cursor(), ...)
29+
end
30+
end
31+
32+
---Injects:
33+
---- n: visual nodes or n or node at cursor
34+
---@param fn fun(n?: Node|Node[], ...): any
35+
---@return fun(n?: Node|Node[], ...): any
36+
local function _v(fn)
37+
return function(n, ...)
38+
return fn(visual_nodes() or n or node_at_cursor(), ...)
2039
end
2140
end
2241

23-
---Return a function wrapper that calls fn.
42+
---Injects:
43+
---- e: Explorer instance
2444
---Does nothing when no explorer instance.
25-
---Injects an explorer instance as first arg.
26-
---Passes other arguments verbatim.
2745
---@param fn fun(e: Explorer, ...): any
2846
---@return fun(e: Explorer, ...): any
2947
local function e_(fn)
@@ -35,36 +53,37 @@ local function e_(fn)
3553
end
3654
end
3755

38-
---Return a function wrapper that calls fn.
56+
---Injects:
57+
---- e: Explorer instance
58+
---- n: n or node at cursor
3959
---Does nothing when no explorer instance.
40-
---Injects an explorer instance as first arg.
41-
---Injects node or node at cursor as second argument.
42-
---Passes other arguments verbatim.
4360
---@param fn fun(e: Explorer, n?: Node, ...): any
4461
---@return fun(e: Explorer, n?: Node, ...): any
4562
local function en(fn)
4663
return function(n, ...)
4764
local e = require("nvim-tree.core").get_explorer()
4865
if e then
49-
if not n then
50-
n = e:get_node_at_cursor() or nil
51-
end
52-
return fn(e, n, ...)
66+
return fn(e, n or node_at_cursor(), ...)
5367
end
5468
end
5569
end
5670

71+
---Injects:
72+
---- e: Explorer instance
73+
---- n: visual nodes or n or node at cursor
74+
---Does nothing when no explorer instance.
75+
---@param fn fun(e: Explorer, n?: Node|Node[], ...): any
76+
---@return fun(e: Explorer, n?: Node|Node[], ...): any
5777
local function ev(fn)
58-
-- TODO following rebase
59-
end
60-
61-
local function _v(fn)
62-
-- TODO following rebase
78+
return function(n, ...)
79+
local e = require("nvim-tree.core").get_explorer()
80+
if e then
81+
return fn(e, visual_nodes() or n or node_at_cursor(), ...)
82+
end
83+
end
6384
end
6485

65-
---Return a function wrapper that calls fn.
66-
---Passes arguments verbatim.
67-
---Exists for formatting purposes only.
86+
---NOP function wrapper, exists for formatting purposes only.
6887
---@param fn fun(...): any
6988
---@return fun(...): any
7089
local function __(fn)

0 commit comments

Comments
 (0)