1
1
local lib = require " nvim-tree.lib"
2
+ local core = require " nvim-tree.core"
2
3
local view = require " nvim-tree.view"
3
4
local utils = require " nvim-tree.utils"
4
5
local actions = require " nvim-tree.actions"
5
6
local appearance_diagnostics = require " nvim-tree.appearance.diagnostics"
6
7
local events = require " nvim-tree.events"
7
8
local help = require " nvim-tree.help"
8
9
local live_filter = require " nvim-tree.live-filter"
9
- local marks = require " nvim-tree.marks"
10
10
local marks_navigation = require " nvim-tree.marks.navigation"
11
11
local marks_bulk_delete = require " nvim-tree.marks.bulk-delete"
12
12
local marks_bulk_trash = require " nvim-tree.marks.bulk-trash"
@@ -43,9 +43,10 @@ local Api = {
43
43
diagnostics = {},
44
44
}
45
45
46
- --- Do nothing when setup not called.
46
+ --- Print error when setup not called.
47
47
--- f function to invoke
48
48
--- @param f function
49
+ --- @return fun ( ... ) : any
49
50
local function wrap (f )
50
51
return function (...)
51
52
if vim .g .NvimTreeSetup == 1 then
@@ -56,13 +57,13 @@ local function wrap(f)
56
57
end
57
58
end
58
59
59
- --- Inject the node as the first argument if absent .
60
+ --- Inject the node as the first argument if present otherwise do nothing .
60
61
--- @param fn function function to invoke
61
62
local function wrap_node (fn )
62
63
return function (node , ...)
63
64
node = node or lib .get_node_at_cursor ()
64
65
if node then
65
- fn (node , ... )
66
+ return fn (node , ... )
66
67
end
67
68
end
68
69
end
72
73
local function wrap_node_or_nil (fn )
73
74
return function (node , ...)
74
75
node = node or lib .get_node_at_cursor ()
75
- fn (node , ... )
76
+ return fn (node , ... )
76
77
end
77
78
end
78
79
80
+ --- Inject the explorer as the first argument if present otherwise do nothing.
81
+ --- @param fn function function to invoke
82
+ --- @return fun ( ... ) : any
83
+ local function wrap_explorer (fn )
84
+ return function (...)
85
+ local explorer = core .get_explorer ()
86
+ if explorer then
87
+ return fn (explorer , ... )
88
+ end
89
+ end
90
+ end
91
+
92
+ --- Invoke a member's method on the singleton explorer.
93
+ --- Print error when setup not called.
94
+ --- @param explorer_member string explorer member name
95
+ --- @param member_method string method name to invoke on member
96
+ --- @return fun ( ... ) : any
97
+ local function wrap_explorer_member (explorer_member , member_method )
98
+ return wrap (function (...)
99
+ local explorer = core .get_explorer ()
100
+ if explorer then
101
+ return explorer [explorer_member ][member_method ](explorer [explorer_member ], ... )
102
+ end
103
+ end )
104
+ end
105
+
79
106
--- @class ApiTreeOpenOpts
80
107
--- @field path string | nil path
81
108
--- @field current_window boolean | nil default false
@@ -241,13 +268,13 @@ Api.events.Event = events.Event
241
268
Api .live_filter .start = wrap (live_filter .start_filtering )
242
269
Api .live_filter .clear = wrap (live_filter .clear_filter )
243
270
244
- Api .marks .get = wrap_node (marks . get_mark )
245
- Api .marks .list = wrap ( marks . get_marks )
246
- Api .marks .toggle = wrap_node (marks . toggle_mark )
247
- Api .marks .clear = wrap ( marks . clear_marks )
248
- Api .marks .bulk .delete = wrap (marks_bulk_delete .bulk_delete )
249
- Api .marks .bulk .trash = wrap (marks_bulk_trash .bulk_trash )
250
- Api .marks .bulk .move = wrap (marks_bulk_move .bulk_move )
271
+ Api .marks .get = wrap_node (wrap_explorer_member ( " marks" , " get_mark" ) )
272
+ Api .marks .list = wrap_explorer_member ( " marks" , " get_marks" )
273
+ Api .marks .toggle = wrap_node (wrap_explorer_member ( " marks" , " toggle_mark" ) )
274
+ Api .marks .clear = wrap_explorer_member ( " marks" , " clear_marks" )
275
+ Api .marks .bulk .delete = wrap_explorer (marks_bulk_delete .bulk_delete )
276
+ Api .marks .bulk .trash = wrap_explorer (marks_bulk_trash .bulk_trash )
277
+ Api .marks .bulk .move = wrap_explorer (marks_bulk_move .bulk_move )
251
278
Api .marks .navigate .next = wrap (marks_navigation .next )
252
279
Api .marks .navigate .prev = wrap (marks_navigation .prev )
253
280
Api .marks .navigate .select = wrap (marks_navigation .select )
0 commit comments