@@ -9,14 +9,38 @@ local reloaders = require "nvim-tree.actions.reloaders"
9
9
10
10
local find_file = require (" nvim-tree.actions.finders.find-file" ).fn
11
11
12
- local M = {
13
- config = {},
12
+ --- @enum ACTION
13
+ local ACTION = {
14
+ copy = " copy" ,
15
+ cut = " cut" ,
14
16
}
15
17
16
- local clipboard = {
17
- cut = {},
18
- copy = {},
19
- }
18
+ --- @class Clipboard to handle all actions.fs clipboard API
19
+ --- @field config table hydrated user opts.filters
20
+ --- @field private explorer Explorer
21
+ --- @field private data table<ACTION , Node[]>
22
+ local Clipboard = {}
23
+
24
+ --- @param opts table user options
25
+ --- @param explorer Explorer
26
+ --- @return Clipboard
27
+ function Clipboard :new (opts , explorer )
28
+ local o = {
29
+ explorer = explorer ,
30
+ data = {
31
+ [ACTION .copy ] = {},
32
+ [ACTION .cut ] = {},
33
+ },
34
+ config = {
35
+ filesystem_watchers = opts .filesystem_watchers ,
36
+ actions = opts .actions ,
37
+ },
38
+ }
39
+
40
+ setmetatable (o , self )
41
+ self .__index = self
42
+ return o
43
+ end
20
44
21
45
--- @param source string
22
46
--- @param destination string
85
109
86
110
--- @param source string
87
111
--- @param dest string
88
- --- @param action_type string
112
+ --- @param action ACTION
89
113
--- @param action_fn fun ( source : string , dest : string )
90
114
--- @return boolean | nil -- success
91
115
--- @return string | nil -- error message
92
- local function do_single_paste (source , dest , action_type , action_fn )
116
+ local function do_single_paste (source , dest , action , action_fn )
93
117
local dest_stats
94
118
local success , errmsg , errcode
95
119
local notify_source = notify .render_path (source )
@@ -98,14 +122,14 @@ local function do_single_paste(source, dest, action_type, action_fn)
98
122
99
123
dest_stats , errmsg , errcode = vim .loop .fs_stat (dest )
100
124
if not dest_stats and errcode ~= " ENOENT" then
101
- notify .error (" Could not " .. action_type .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
125
+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
102
126
return false , errmsg
103
127
end
104
128
105
129
local function on_process ()
106
130
success , errmsg = action_fn (source , dest )
107
131
if not success then
108
- notify .error (" Could not " .. action_type .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
132
+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
109
133
return false , errmsg
110
134
end
111
135
@@ -123,7 +147,7 @@ local function do_single_paste(source, dest, action_type, action_fn)
123
147
vim .ui .input (input_opts , function (new_dest )
124
148
utils .clear_prompt ()
125
149
if new_dest then
126
- do_single_paste (source , new_dest , action_type , action_fn )
150
+ do_single_paste (source , new_dest , action , action_fn )
127
151
end
128
152
end )
129
153
else
@@ -137,7 +161,7 @@ local function do_single_paste(source, dest, action_type, action_fn)
137
161
vim .ui .input (input_opts , function (new_dest )
138
162
utils .clear_prompt ()
139
163
if new_dest then
140
- do_single_paste (source , new_dest , action_type , action_fn )
164
+ do_single_paste (source , new_dest , action , action_fn )
141
165
end
142
166
end )
143
167
end
@@ -165,37 +189,42 @@ local function toggle(node, clip)
165
189
notify .info (notify_node .. " added to clipboard." )
166
190
end
167
191
168
- function M .clear_clipboard ()
169
- clipboard .cut = {}
170
- clipboard .copy = {}
192
+ --- Clear copied and cut
193
+ function Clipboard :clear_clipboard ()
194
+ self .data [ACTION .copy ] = {}
195
+ self .data [ACTION .cut ] = {}
171
196
notify .info " Clipboard has been emptied."
172
197
renderer .draw ()
173
198
end
174
199
200
+ --- Copy one node
175
201
--- @param node Node
176
- function M . copy (node )
177
- utils .array_remove (clipboard . cut , node )
178
- toggle (node , clipboard . copy )
202
+ function Clipboard : copy (node )
203
+ utils .array_remove (self . data [ ACTION . cut ] , node )
204
+ toggle (node , self . data [ ACTION . copy ] )
179
205
renderer .draw ()
180
206
end
181
207
208
+ --- Cut one node
182
209
--- @param node Node
183
- function M . cut (node )
184
- utils .array_remove (clipboard . copy , node )
185
- toggle (node , clipboard . cut )
210
+ function Clipboard : cut (node )
211
+ utils .array_remove (self . data [ ACTION . copy ] , node )
212
+ toggle (node , self . data [ ACTION . cut ] )
186
213
renderer .draw ()
187
214
end
188
215
216
+ --- Paste cut or cop
217
+ --- @private
189
218
--- @param node Node
190
- --- @param action_type string
219
+ --- @param action ACTION
191
220
--- @param action_fn fun ( source : string , dest : string )
192
- local function do_paste (node , action_type , action_fn )
221
+ function Clipboard : do_paste (node , action , action_fn )
193
222
node = lib .get_last_group_node (node )
194
223
local explorer = core .get_explorer ()
195
224
if node .name == " .." and explorer then
196
225
node = explorer
197
226
end
198
- local clip = clipboard [ action_type ]
227
+ local clip = self . data [ action ]
199
228
if # clip == 0 then
200
229
return
201
230
end
@@ -204,7 +233,7 @@ local function do_paste(node, action_type, action_fn)
204
233
local stats , errmsg , errcode = vim .loop .fs_stat (destination )
205
234
if not stats and errcode ~= " ENOENT" then
206
235
log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , errmsg )
207
- notify .error (" Could not " .. action_type .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
236
+ notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
208
237
return
209
238
end
210
239
local is_dir = stats and stats .type == " directory"
@@ -214,11 +243,11 @@ local function do_paste(node, action_type, action_fn)
214
243
215
244
for _ , _node in ipairs (clip ) do
216
245
local dest = utils .path_join { destination , _node .name }
217
- do_single_paste (_node .absolute_path , dest , action_type , action_fn )
246
+ do_single_paste (_node .absolute_path , dest , action , action_fn )
218
247
end
219
248
220
- clipboard [ action_type ] = {}
221
- if not M .config .filesystem_watchers .enable then
249
+ self . data [ action ] = {}
250
+ if not self .config .filesystem_watchers .enable then
222
251
reloaders .reload_explorer ()
223
252
end
224
253
end
@@ -246,26 +275,27 @@ local function do_cut(source, destination)
246
275
return true
247
276
end
248
277
278
+ --- Paste cut (if present) or copy (if present)
249
279
--- @param node Node
250
- function M . paste (node )
251
- if clipboard . cut [1 ] ~= nil then
252
- do_paste (node , " cut" , do_cut )
253
- else
254
- do_paste (node , " copy" , do_copy )
280
+ function Clipboard : paste (node )
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 )
255
285
end
256
286
end
257
287
258
- function M . print_clipboard ()
288
+ function Clipboard : print_clipboard ()
259
289
local content = {}
260
- if # clipboard . cut > 0 then
290
+ if # self . data [ ACTION . cut ] > 0 then
261
291
table.insert (content , " Cut" )
262
- for _ , node in pairs (clipboard . cut ) do
292
+ for _ , node in pairs (self . data [ ACTION . cut ] ) do
263
293
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
264
294
end
265
295
end
266
- if # clipboard . copy > 0 then
296
+ if # self . data [ ACTION . copy ] > 0 then
267
297
table.insert (content , " Copy" )
268
- for _ , node in pairs (clipboard . copy ) do
298
+ for _ , node in pairs (self . data [ ACTION . copy ] ) do
269
299
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
270
300
end
271
301
end
@@ -274,10 +304,10 @@ function M.print_clipboard()
274
304
end
275
305
276
306
--- @param content string
277
- local function copy_to_clipboard (content )
307
+ function Clipboard : copy_to_reg (content )
278
308
local clipboard_name
279
309
local reg
280
- if M .config .actions .use_system_clipboard == true then
310
+ if self .config .actions .use_system_clipboard == true then
281
311
clipboard_name = " system"
282
312
reg = " +"
283
313
else
@@ -298,18 +328,18 @@ local function copy_to_clipboard(content)
298
328
end
299
329
300
330
--- @param node Node
301
- function M . copy_filename (node )
302
- copy_to_clipboard (node .name )
331
+ function Clipboard : copy_filename (node )
332
+ self : copy_to_reg (node .name )
303
333
end
304
334
305
335
--- @param node Node
306
- function M . copy_basename (node )
336
+ function Clipboard : copy_basename (node )
307
337
local basename = vim .fn .fnamemodify (node .name , " :r" )
308
- copy_to_clipboard (basename )
338
+ self : copy_to_reg (basename )
309
339
end
310
340
311
341
--- @param node Node
312
- function M . copy_path (node )
342
+ function Clipboard : copy_path (node )
313
343
local absolute_path = node .absolute_path
314
344
local cwd = core .get_cwd ()
315
345
if cwd == nil then
@@ -318,33 +348,28 @@ function M.copy_path(node)
318
348
319
349
local relative_path = utils .path_relative (absolute_path , cwd )
320
350
local content = node .nodes ~= nil and utils .path_add_trailing (relative_path ) or relative_path
321
- copy_to_clipboard (content )
351
+ self : copy_to_reg (content )
322
352
end
323
353
324
354
--- @param node Node
325
- function M . copy_absolute_path (node )
355
+ function Clipboard : copy_absolute_path (node )
326
356
local absolute_path = node .absolute_path
327
357
local content = node .nodes ~= nil and utils .path_add_trailing (absolute_path ) or absolute_path
328
- copy_to_clipboard (content )
358
+ self : copy_to_reg (content )
329
359
end
330
360
331
361
--- Node is cut. Will not be copied.
332
362
--- @param node Node
333
363
--- @return boolean
334
- function M . is_cut (node )
335
- return vim .tbl_contains (clipboard . cut , node )
364
+ function Clipboard : is_cut (node )
365
+ return vim .tbl_contains (self . data [ ACTION . cut ] , node )
336
366
end
337
367
338
368
--- Node is copied. Will not be cut.
339
369
--- @param node Node
340
370
--- @return boolean
341
- function M .is_copied (node )
342
- return vim .tbl_contains (clipboard .copy , node )
343
- end
344
-
345
- function M .setup (opts )
346
- M .config .filesystem_watchers = opts .filesystem_watchers
347
- M .config .actions = opts .actions
371
+ function Clipboard :is_copied (node )
372
+ return vim .tbl_contains (self .data [ACTION .copy ], node )
348
373
end
349
374
350
- return M
375
+ return Clipboard
0 commit comments