@@ -9,27 +9,25 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
9
9
10
10
local DirectoryNode = require (" nvim-tree.node.directory" )
11
11
12
- --- @enum ACTION
13
- local ACTION = {
14
- copy = " copy" ,
15
- cut = " cut" ,
16
- }
12
+ --- @alias ClipboardAction " copy" | " cut"
13
+ --- @alias ClipboardData table<ClipboardAction , Node[]>
17
14
18
15
--- @class Clipboard to handle all actions.fs clipboard API
19
16
--- @field config table hydrated user opts.filters
20
17
--- @field private explorer Explorer
21
- --- @field private data table<ACTION , Node[]>
18
+ --- @field private data ClipboardData
22
19
local Clipboard = {}
23
20
24
21
--- @param opts table user options
25
22
--- @param explorer Explorer
26
23
--- @return Clipboard
27
24
function Clipboard :new (opts , explorer )
25
+ --- @type Clipboard
28
26
local o = {
29
27
explorer = explorer ,
30
28
data = {
31
- [ ACTION . copy ] = {},
32
- [ ACTION . cut ] = {},
29
+ copy = {},
30
+ cut = {},
33
31
},
34
32
config = {
35
33
filesystem_watchers = opts .filesystem_watchers ,
109
107
110
108
--- @param source string
111
109
--- @param dest string
112
- --- @param action ACTION
110
+ --- @param action ClipboardAction
113
111
--- @param action_fn fun ( source : string , dest : string )
114
112
--- @return boolean | nil -- success
115
113
--- @return string | nil -- error message
@@ -173,7 +171,7 @@ local function do_single_paste(source, dest, action, action_fn)
173
171
end
174
172
175
173
--- @param node Node
176
- --- @param clip table
174
+ --- @param clip ClipboardData
177
175
local function toggle (node , clip )
178
176
if node .name == " .." then
179
177
return
@@ -191,32 +189,32 @@ end
191
189
192
190
--- Clear copied and cut
193
191
function Clipboard :clear_clipboard ()
194
- self .data [ ACTION .copy ] = {}
195
- self .data [ ACTION .cut ] = {}
192
+ self .data .copy = {}
193
+ self .data .cut = {}
196
194
notify .info (" Clipboard has been emptied." )
197
195
self .explorer .renderer :draw ()
198
196
end
199
197
200
198
--- Copy one node
201
199
--- @param node Node
202
200
function Clipboard :copy (node )
203
- utils .array_remove (self .data [ ACTION .cut ] , node )
204
- toggle (node , self .data [ ACTION .copy ] )
201
+ utils .array_remove (self .data .cut , node )
202
+ toggle (node , self .data .copy )
205
203
self .explorer .renderer :draw ()
206
204
end
207
205
208
206
--- Cut one node
209
207
--- @param node Node
210
208
function Clipboard :cut (node )
211
- utils .array_remove (self .data [ ACTION .copy ] , node )
212
- toggle (node , self .data [ ACTION .cut ] )
209
+ utils .array_remove (self .data .copy , node )
210
+ toggle (node , self .data .cut )
213
211
self .explorer .renderer :draw ()
214
212
end
215
213
216
214
--- Paste cut or cop
217
215
--- @private
218
216
--- @param node Node
219
- --- @param action ACTION
217
+ --- @param action ClipboardAction
220
218
--- @param action_fn fun ( source : string , dest : string )
221
219
function Clipboard :do_paste (node , action , action_fn )
222
220
if node .name == " .." then
@@ -281,24 +279,24 @@ end
281
279
--- Paste cut (if present) or copy (if present)
282
280
--- @param node Node
283
281
function Clipboard :paste (node )
284
- if self .data [ ACTION .cut ] [1 ] ~= nil then
285
- self :do_paste (node , ACTION . cut , do_cut )
286
- elseif self .data [ ACTION .copy ] [1 ] ~= nil then
287
- self :do_paste (node , ACTION . copy , do_copy )
282
+ if self .data .cut [1 ] ~= nil then
283
+ self :do_paste (node , " cut" , do_cut )
284
+ elseif self .data .copy [1 ] ~= nil then
285
+ self :do_paste (node , " copy" , do_copy )
288
286
end
289
287
end
290
288
291
289
function Clipboard :print_clipboard ()
292
290
local content = {}
293
- if # self .data [ ACTION .cut ] > 0 then
291
+ if # self .data .cut > 0 then
294
292
table.insert (content , " Cut" )
295
- for _ , node in pairs (self .data [ ACTION .cut ] ) do
293
+ for _ , node in pairs (self .data .cut ) do
296
294
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
297
295
end
298
296
end
299
- if # self .data [ ACTION .copy ] > 0 then
297
+ if # self .data .copy > 0 then
300
298
table.insert (content , " Copy" )
301
- for _ , node in pairs (self .data [ ACTION .copy ] ) do
299
+ for _ , node in pairs (self .data .copy ) do
302
300
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
303
301
end
304
302
end
@@ -397,14 +395,14 @@ end
397
395
--- @param node Node
398
396
--- @return boolean
399
397
function Clipboard :is_cut (node )
400
- return vim .tbl_contains (self .data [ ACTION .cut ] , node )
398
+ return vim .tbl_contains (self .data .cut , node )
401
399
end
402
400
403
401
--- Node is copied. Will not be cut.
404
402
--- @param node Node
405
403
--- @return boolean
406
404
function Clipboard :is_copied (node )
407
- return vim .tbl_contains (self .data [ ACTION .copy ] , node )
405
+ return vim .tbl_contains (self .data .copy , node )
408
406
end
409
407
410
408
return Clipboard
0 commit comments