@@ -11,19 +11,14 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
11
11
12
12
--- @enum ACTION
13
13
local ACTION = {
14
- none = 0 ,
15
- copy = 1 ,
16
- cut = 2 ,
14
+ copy = " copy" ,
15
+ cut = " cut" ,
17
16
}
18
17
19
- --- @class ClipboardData absolute paths
20
- --- @field copy string[] copied
21
- --- @field cut string[] cut
22
-
23
18
--- @class Clipboard to handle all actions.fs clipboard API
24
19
--- @field config table hydrated user opts.filters
25
20
--- @field private explorer Explorer
26
- --- @field private data ClipboardData
21
+ --- @field private data table<ACTION , Node[]>
27
22
local Clipboard = {}
28
23
29
24
--- @param opts table user options
@@ -33,8 +28,8 @@ function Clipboard:new(opts, explorer)
33
28
local o = {
34
29
explorer = explorer ,
35
30
data = {
36
- copy = {},
37
- cut = {},
31
+ [ ACTION . copy ] = {},
32
+ [ ACTION . cut ] = {},
38
33
},
39
34
config = {
40
35
filesystem_watchers = opts .filesystem_watchers ,
@@ -196,40 +191,40 @@ end
196
191
197
192
--- Clear copied and cut
198
193
function Clipboard :clear_clipboard ()
199
- self .data . cut = {}
200
- self .data . copy = {}
194
+ self .data [ ACTION . copy ] = {}
195
+ self .data [ ACTION . cut ] = {}
201
196
notify .info " Clipboard has been emptied."
202
197
renderer .draw ()
203
198
end
204
199
205
200
--- Copy one node
206
201
--- @param node Node
207
202
function Clipboard :copy (node )
208
- utils .array_remove (self .data .cut , node )
209
- toggle (node , self .data .copy )
203
+ utils .array_remove (self .data [ ACTION .cut ] , node )
204
+ toggle (node , self .data [ ACTION .copy ] )
210
205
renderer .draw ()
211
206
end
212
207
213
208
--- Cut one node
214
209
--- @param node Node
215
210
function Clipboard :cut (node )
216
- utils .array_remove (self .data .copy , node )
217
- toggle (node , self .data .cut )
211
+ utils .array_remove (self .data [ ACTION .copy ] , node )
212
+ toggle (node , self .data [ ACTION .cut ] )
218
213
renderer .draw ()
219
214
end
220
215
221
216
--- Paste cut or cop
222
217
--- @private
223
218
--- @param node Node
224
- --- @param action_type string
219
+ --- @param action ACTION
225
220
--- @param action_fn fun ( source : string , dest : string )
226
- function Clipboard :do_paste (node , action_type , action_fn )
221
+ function Clipboard :do_paste (node , action , action_fn )
227
222
node = lib .get_last_group_node (node )
228
223
local explorer = core .get_explorer ()
229
224
if node .name == " .." and explorer then
230
225
node = explorer
231
226
end
232
- local clip = self .data [action_type ]
227
+ local clip = self .data [action ]
233
228
if # clip == 0 then
234
229
return
235
230
end
@@ -238,7 +233,7 @@ function Clipboard:do_paste(node, action_type, action_fn)
238
233
local stats , errmsg , errcode = vim .loop .fs_stat (destination )
239
234
if not stats and errcode ~= " ENOENT" then
240
235
log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , errmsg )
241
- notify .error (" Could not " .. action_type .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
236
+ notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
242
237
return
243
238
end
244
239
local is_dir = stats and stats .type == " directory"
@@ -248,10 +243,10 @@ function Clipboard:do_paste(node, action_type, action_fn)
248
243
249
244
for _ , _node in ipairs (clip ) do
250
245
local dest = utils .path_join { destination , _node .name }
251
- do_single_paste (_node .absolute_path , dest , action_type , action_fn )
246
+ do_single_paste (_node .absolute_path , dest , action , action_fn )
252
247
end
253
248
254
- self .data [action_type ] = {}
249
+ self .data [action ] = {}
255
250
if not self .config .filesystem_watchers .enable then
256
251
reloaders .reload_explorer ()
257
252
end
@@ -283,24 +278,24 @@ end
283
278
--- Paste cut (if present) or copy (if present)
284
279
--- @param node Node
285
280
function Clipboard :paste (node )
286
- if self .data .cut [1 ] ~= nil then
287
- self :do_paste (node , " cut" , do_cut )
288
- elseif self .data .copy [1 ] ~= nil then
289
- self :do_paste (node , " cop " , do_copy )
281
+ if self .data [ ACTION .cut ] [1 ] ~= nil then
282
+ self :do_paste (node , ACTION . cut , do_cut )
283
+ elseif self .data [ ACTION .copy ] [1 ] ~= nil then
284
+ self :do_paste (node , ACTION . copy , do_copy )
290
285
end
291
286
end
292
287
293
288
function Clipboard :print_clipboard ()
294
289
local content = {}
295
- if # self .data .cut > 0 then
290
+ if # self .data [ ACTION .cut ] > 0 then
296
291
table.insert (content , " Cut" )
297
- for _ , node in pairs (self .data .cut ) do
292
+ for _ , node in pairs (self .data [ ACTION .cut ] ) do
298
293
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
299
294
end
300
295
end
301
- if # self .data .copy > 0 then
296
+ if # self .data [ ACTION .copy ] > 0 then
302
297
table.insert (content , " Copy" )
303
- for _ , node in pairs (self .data .copy ) do
298
+ for _ , node in pairs (self .data [ ACTION .copy ] ) do
304
299
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
305
300
end
306
301
end
@@ -309,7 +304,7 @@ function Clipboard:print_clipboard()
309
304
end
310
305
311
306
--- @param content string
312
- function Clipboard :copy_to_clipboard (content )
307
+ function Clipboard :copy_to_reg (content )
313
308
local clipboard_name
314
309
local reg
315
310
if self .config .actions .use_system_clipboard == true then
@@ -334,13 +329,13 @@ end
334
329
335
330
--- @param node Node
336
331
function Clipboard :copy_filename (node )
337
- self :copy_to_clipboard (node .name )
332
+ self :copy_to_reg (node .name )
338
333
end
339
334
340
335
--- @param node Node
341
336
function Clipboard :copy_basename (node )
342
337
local basename = vim .fn .fnamemodify (node .name , " :r" )
343
- self :copy_to_clipboard (basename )
338
+ self :copy_to_reg (basename )
344
339
end
345
340
346
341
--- @param node Node
@@ -353,28 +348,28 @@ function Clipboard:copy_path(node)
353
348
354
349
local relative_path = utils .path_relative (absolute_path , cwd )
355
350
local content = node .nodes ~= nil and utils .path_add_trailing (relative_path ) or relative_path
356
- self :copy_to_clipboard (content )
351
+ self :copy_to_reg (content )
357
352
end
358
353
359
354
--- @param node Node
360
355
function Clipboard :copy_absolute_path (node )
361
356
local absolute_path = node .absolute_path
362
357
local content = node .nodes ~= nil and utils .path_add_trailing (absolute_path ) or absolute_path
363
- self :copy_to_clipboard (content )
358
+ self :copy_to_reg (content )
364
359
end
365
360
366
361
--- Node is cut. Will not be copied.
367
362
--- @param node Node
368
363
--- @return boolean
369
364
function Clipboard :is_cut (node )
370
- return vim .tbl_contains (self .data .cut , node )
365
+ return vim .tbl_contains (self .data [ ACTION .cut ] , node )
371
366
end
372
367
373
368
--- Node is copied. Will not be cut.
374
369
--- @param node Node
375
370
--- @return boolean
376
371
function Clipboard :is_copied (node )
377
- return vim .tbl_contains (self .data .copy , node )
372
+ return vim .tbl_contains (self .data [ ACTION .copy ] , node )
378
373
end
379
374
380
375
return Clipboard
0 commit comments