@@ -12,10 +12,14 @@ local DirectoryNode = require("nvim-tree.node.directory")
12
12
--- @alias ClipboardAction " copy" | " cut"
13
13
--- @alias ClipboardData table<ClipboardAction , Node[]>
14
14
15
+ --- @alias ClipboardActionFn fun ( source : string , dest : string ): boolean , string ?
16
+
15
17
--- @class Clipboard to handle all actions.fs clipboard API
16
18
--- @field config table hydrated user opts.filters
17
19
--- @field private explorer Explorer
18
20
--- @field private data ClipboardData
21
+ --- @field private clipboard_name string
22
+ --- @field private reg string
19
23
local Clipboard = {}
20
24
21
25
--- @param opts table user options
@@ -29,9 +33,10 @@ function Clipboard:new(opts, explorer)
29
33
copy = {},
30
34
cut = {},
31
35
},
36
+ clipboard_name = opts .actions .use_system_clipboard and " system" or " neovim" ,
37
+ reg = opts .actions .use_system_clipboard and " +" or " 1" ,
32
38
config = {
33
39
filesystem_watchers = opts .filesystem_watchers ,
34
- actions = opts .actions ,
35
40
},
36
41
}
37
42
45
50
--- @return boolean
46
51
--- @return string | nil
47
52
local function do_copy (source , destination )
48
- local source_stats , errmsg = vim .loop .fs_stat (source )
53
+ local source_stats , err = vim .loop .fs_stat (source )
49
54
50
55
if not source_stats then
51
- log .line (" copy_paste" , " do_copy fs_stat '%s' failed '%s'" , source , errmsg )
52
- return false , errmsg
56
+ log .line (" copy_paste" , " do_copy fs_stat '%s' failed '%s'" , source , err )
57
+ return false , err
53
58
end
54
59
55
60
log .line (" copy_paste" , " do_copy %s '%s' -> '%s'" , source_stats .type , source , destination )
@@ -61,27 +66,27 @@ local function do_copy(source, destination)
61
66
62
67
if source_stats .type == " file" then
63
68
local success
64
- success , errmsg = vim .loop .fs_copyfile (source , destination )
69
+ success , err = vim .loop .fs_copyfile (source , destination )
65
70
if not success then
66
- log .line (" copy_paste" , " do_copy fs_copyfile failed '%s'" , errmsg )
67
- return false , errmsg
71
+ log .line (" copy_paste" , " do_copy fs_copyfile failed '%s'" , err )
72
+ return false , err
68
73
end
69
74
return true
70
75
elseif source_stats .type == " directory" then
71
76
local handle
72
- handle , errmsg = vim .loop .fs_scandir (source )
77
+ handle , err = vim .loop .fs_scandir (source )
73
78
if type (handle ) == " string" then
74
79
return false , handle
75
80
elseif not handle then
76
- log .line (" copy_paste" , " do_copy fs_scandir '%s' failed '%s'" , source , errmsg )
77
- return false , errmsg
81
+ log .line (" copy_paste" , " do_copy fs_scandir '%s' failed '%s'" , source , err )
82
+ return false , err
78
83
end
79
84
80
85
local success
81
- success , errmsg = vim .loop .fs_mkdir (destination , source_stats .mode )
86
+ success , err = vim .loop .fs_mkdir (destination , source_stats .mode )
82
87
if not success then
83
- log .line (" copy_paste" , " do_copy fs_mkdir '%s' failed '%s'" , destination , errmsg )
84
- return false , errmsg
88
+ log .line (" copy_paste" , " do_copy fs_mkdir '%s' failed '%s'" , destination , err )
89
+ return false , err
85
90
end
86
91
87
92
while true do
@@ -92,15 +97,15 @@ local function do_copy(source, destination)
92
97
93
98
local new_name = utils .path_join ({ source , name })
94
99
local new_destination = utils .path_join ({ destination , name })
95
- success , errmsg = do_copy (new_name , new_destination )
100
+ success , err = do_copy (new_name , new_destination )
96
101
if not success then
97
- return false , errmsg
102
+ return false , err
98
103
end
99
104
end
100
105
else
101
- errmsg = string.format (" '%s' illegal file type '%s'" , source , source_stats .type )
102
- log .line (" copy_paste" , " do_copy %s" , errmsg )
103
- return false , errmsg
106
+ err = string.format (" '%s' illegal file type '%s'" , source , source_stats .type )
107
+ log .line (" copy_paste" , " do_copy %s" , err )
108
+ return false , err
104
109
end
105
110
106
111
return true
@@ -109,27 +114,25 @@ end
109
114
--- @param source string
110
115
--- @param dest string
111
116
--- @param action ClipboardAction
112
- --- @param action_fn fun ( source : string , dest : string )
117
+ --- @param action_fn ClipboardActionFn
113
118
--- @return boolean | nil -- success
114
119
--- @return string | nil -- error message
115
120
local function do_single_paste (source , dest , action , action_fn )
116
- local dest_stats
117
- local success , errmsg , errcode
118
121
local notify_source = notify .render_path (source )
119
122
120
123
log .line (" copy_paste" , " do_single_paste '%s' -> '%s'" , source , dest )
121
124
122
- dest_stats , errmsg , errcode = vim .loop .fs_stat (dest )
123
- if not dest_stats and errcode ~= " ENOENT" then
124
- notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
125
- return false , errmsg
125
+ local dest_stats , err , err_name = vim .loop .fs_stat (dest )
126
+ if not dest_stats and err_name ~= " ENOENT" then
127
+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (err or " ???" ))
128
+ return false , err
126
129
end
127
130
128
131
local function on_process ()
129
- success , errmsg = action_fn (source , dest )
132
+ local success , error = action_fn (source , dest )
130
133
if not success then
131
- notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
132
- return false , errmsg
134
+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (error or " ???" ))
135
+ return false , error
133
136
end
134
137
135
138
find_file (utils .path_remove_trailing (dest ))
216
219
--- @private
217
220
--- @param node Node
218
221
--- @param action ClipboardAction
219
- --- @param action_fn fun ( source : string , dest : string )
222
+ --- @param action_fn ClipboardActionFn
220
223
function Clipboard :do_paste (node , action , action_fn )
221
224
if node .name == " .." then
222
225
node = self .explorer
@@ -232,10 +235,10 @@ function Clipboard:do_paste(node, action, action_fn)
232
235
end
233
236
234
237
local destination = node .absolute_path
235
- local stats , errmsg , errcode = vim .loop .fs_stat (destination )
236
- if not stats and errcode ~= " ENOENT" then
237
- log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , errmsg )
238
- notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
238
+ local stats , err , err_name = vim .loop .fs_stat (destination )
239
+ if not stats and err_name ~= " ENOENT" then
240
+ log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , err )
241
+ notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (err or " ???" ))
239
242
return
240
243
end
241
244
local is_dir = stats and stats .type == " directory"
@@ -307,65 +310,45 @@ end
307
310
308
311
--- @param content string
309
312
function Clipboard :copy_to_reg (content )
310
- local clipboard_name
311
- local reg
312
- if self .config .actions .use_system_clipboard == true then
313
- clipboard_name = " system"
314
- reg = " +"
315
- else
316
- clipboard_name = " neovim"
317
- reg = " 1"
318
- end
319
-
320
313
-- manually firing TextYankPost does not set vim.v.event
321
314
-- workaround: create a scratch buffer with the clipboard contents and send a yank command
322
315
local temp_buf = vim .api .nvim_create_buf (false , true )
323
316
vim .api .nvim_buf_set_text (temp_buf , 0 , 0 , 0 , 0 , { content })
324
317
vim .api .nvim_buf_call (temp_buf , function ()
325
- vim .cmd (string.format (' normal! "%sy$' , reg ))
318
+ vim .cmd (string.format (' normal! "%sy$' , self . reg ))
326
319
end )
327
320
vim .api .nvim_buf_delete (temp_buf , {})
328
321
329
- notify .info (string.format (" Copied %s to %s clipboard!" , content , clipboard_name ))
322
+ notify .info (string.format (" Copied %s to %s clipboard!" , content , self . clipboard_name ))
330
323
end
331
324
332
325
--- @param node Node
333
326
function Clipboard :copy_filename (node )
334
- local content
335
-
336
327
if node .name == " .." then
337
328
-- root
338
- content = vim .fn .fnamemodify (self .explorer .absolute_path , " :t" )
329
+ self : copy_to_reg ( vim .fn .fnamemodify (self .explorer .absolute_path , " :t" ) )
339
330
else
340
331
-- node
341
- content = node .name
332
+ self : copy_to_reg ( node .name )
342
333
end
343
-
344
- self :copy_to_reg (content )
345
334
end
346
335
347
336
--- @param node Node
348
337
function Clipboard :copy_basename (node )
349
- local content
350
-
351
338
if node .name == " .." then
352
339
-- root
353
- content = vim .fn .fnamemodify (self .explorer .absolute_path , " :t:r" )
340
+ self : copy_to_reg ( vim .fn .fnamemodify (self .explorer .absolute_path , " :t:r" ) )
354
341
else
355
342
-- node
356
- content = vim .fn .fnamemodify (node .name , " :r" )
343
+ self : copy_to_reg ( vim .fn .fnamemodify (node .name , " :r" ) )
357
344
end
358
-
359
- self :copy_to_reg (content )
360
345
end
361
346
362
347
--- @param node Node
363
348
function Clipboard :copy_path (node )
364
- local content
365
-
366
349
if node .name == " .." then
367
350
-- root
368
- content = utils .path_add_trailing (" " )
351
+ self : copy_to_reg ( utils .path_add_trailing (" " ) )
369
352
else
370
353
-- node
371
354
local absolute_path = node .absolute_path
@@ -375,10 +358,12 @@ function Clipboard:copy_path(node)
375
358
end
376
359
377
360
local relative_path = utils .path_relative (absolute_path , cwd )
378
- content = node :is (DirectoryNode ) and utils .path_add_trailing (relative_path ) or relative_path
361
+ if node :is (DirectoryNode ) then
362
+ self :copy_to_reg (utils .path_add_trailing (relative_path ))
363
+ else
364
+ self :copy_to_reg (relative_path )
365
+ end
379
366
end
380
-
381
- self :copy_to_reg (content )
382
367
end
383
368
384
369
--- @param node Node
0 commit comments