@@ -9,14 +9,43 @@ 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
+ none = 0 ,
15
+ copy = 1 ,
16
+ cut = 2 ,
14
17
}
15
18
16
- local clipboard = {
17
- cut = {},
18
- copy = {},
19
- }
19
+ --- @class ClipboardData absolute paths
20
+ --- @field copy string[] copied
21
+ --- @field cut string[] cut
22
+
23
+ --- @class Clipboard to handle all actions.fs clipboard API
24
+ --- @field config table hydrated user opts.filters
25
+ --- @field private explorer Explorer
26
+ --- @field private data ClipboardData
27
+ local Clipboard = {}
28
+
29
+ --- @param opts table user options
30
+ --- @param explorer Explorer
31
+ --- @return Clipboard
32
+ function Clipboard :new (opts , explorer )
33
+ local o = {
34
+ explorer = explorer ,
35
+ data = {
36
+ copy = {},
37
+ cut = {},
38
+ },
39
+ config = {
40
+ filesystem_watchers = opts .filesystem_watchers ,
41
+ actions = opts .actions ,
42
+ },
43
+ }
44
+
45
+ setmetatable (o , self )
46
+ self .__index = self
47
+ return o
48
+ end
20
49
21
50
--- @param source string
22
51
--- @param destination string
@@ -165,37 +194,42 @@ local function toggle(node, clip)
165
194
notify .info (notify_node .. " added to clipboard." )
166
195
end
167
196
168
- function M .clear_clipboard ()
169
- clipboard .cut = {}
170
- clipboard .copy = {}
197
+ --- Clear copied and cut
198
+ function Clipboard :clear_clipboard ()
199
+ self .data .cut = {}
200
+ self .data .copy = {}
171
201
notify .info " Clipboard has been emptied."
172
202
renderer .draw ()
173
203
end
174
204
205
+ --- Copy one node
175
206
--- @param node Node
176
- function M . copy (node )
177
- utils .array_remove (clipboard .cut , node )
178
- toggle (node , clipboard .copy )
207
+ function Clipboard : copy (node )
208
+ utils .array_remove (self . data .cut , node )
209
+ toggle (node , self . data .copy )
179
210
renderer .draw ()
180
211
end
181
212
213
+ --- Cut one node
182
214
--- @param node Node
183
- function M . cut (node )
184
- utils .array_remove (clipboard .copy , node )
185
- toggle (node , clipboard .cut )
215
+ function Clipboard : cut (node )
216
+ utils .array_remove (self . data .copy , node )
217
+ toggle (node , self . data .cut )
186
218
renderer .draw ()
187
219
end
188
220
221
+ --- Paste cut or cop
222
+ --- @private
189
223
--- @param node Node
190
224
--- @param action_type string
191
225
--- @param action_fn fun ( source : string , dest : string )
192
- local function do_paste (node , action_type , action_fn )
226
+ function Clipboard : do_paste (node , action_type , action_fn )
193
227
node = lib .get_last_group_node (node )
194
228
local explorer = core .get_explorer ()
195
229
if node .name == " .." and explorer then
196
230
node = explorer
197
231
end
198
- local clip = clipboard [action_type ]
232
+ local clip = self . data [action_type ]
199
233
if # clip == 0 then
200
234
return
201
235
end
@@ -217,8 +251,8 @@ local function do_paste(node, action_type, action_fn)
217
251
do_single_paste (_node .absolute_path , dest , action_type , action_fn )
218
252
end
219
253
220
- clipboard [action_type ] = {}
221
- if not M .config .filesystem_watchers .enable then
254
+ self . data [action_type ] = {}
255
+ if not self .config .filesystem_watchers .enable then
222
256
reloaders .reload_explorer ()
223
257
end
224
258
end
@@ -246,26 +280,27 @@ local function do_cut(source, destination)
246
280
return true
247
281
end
248
282
283
+ --- Paste cut (if present) or copy (if present)
249
284
--- @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 )
285
+ 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 )
255
290
end
256
291
end
257
292
258
- function M . print_clipboard ()
293
+ function Clipboard : print_clipboard ()
259
294
local content = {}
260
- if # clipboard .cut > 0 then
295
+ if # self . data .cut > 0 then
261
296
table.insert (content , " Cut" )
262
- for _ , node in pairs (clipboard .cut ) do
297
+ for _ , node in pairs (self . data .cut ) do
263
298
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
264
299
end
265
300
end
266
- if # clipboard .copy > 0 then
301
+ if # self . data .copy > 0 then
267
302
table.insert (content , " Copy" )
268
- for _ , node in pairs (clipboard .copy ) do
303
+ for _ , node in pairs (self . data .copy ) do
269
304
table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
270
305
end
271
306
end
@@ -274,10 +309,10 @@ function M.print_clipboard()
274
309
end
275
310
276
311
--- @param content string
277
- local function copy_to_clipboard (content )
312
+ function Clipboard : copy_to_clipboard (content )
278
313
local clipboard_name
279
314
local reg
280
- if M .config .actions .use_system_clipboard == true then
315
+ if self .config .actions .use_system_clipboard == true then
281
316
clipboard_name = " system"
282
317
reg = " +"
283
318
else
@@ -298,18 +333,18 @@ local function copy_to_clipboard(content)
298
333
end
299
334
300
335
--- @param node Node
301
- function M . copy_filename (node )
302
- copy_to_clipboard (node .name )
336
+ function Clipboard : copy_filename (node )
337
+ self : copy_to_clipboard (node .name )
303
338
end
304
339
305
340
--- @param node Node
306
- function M . copy_basename (node )
341
+ function Clipboard : copy_basename (node )
307
342
local basename = vim .fn .fnamemodify (node .name , " :r" )
308
- copy_to_clipboard (basename )
343
+ self : copy_to_clipboard (basename )
309
344
end
310
345
311
346
--- @param node Node
312
- function M . copy_path (node )
347
+ function Clipboard : copy_path (node )
313
348
local absolute_path = node .absolute_path
314
349
local cwd = core .get_cwd ()
315
350
if cwd == nil then
@@ -318,33 +353,28 @@ function M.copy_path(node)
318
353
319
354
local relative_path = utils .path_relative (absolute_path , cwd )
320
355
local content = node .nodes ~= nil and utils .path_add_trailing (relative_path ) or relative_path
321
- copy_to_clipboard (content )
356
+ self : copy_to_clipboard (content )
322
357
end
323
358
324
359
--- @param node Node
325
- function M . copy_absolute_path (node )
360
+ function Clipboard : copy_absolute_path (node )
326
361
local absolute_path = node .absolute_path
327
362
local content = node .nodes ~= nil and utils .path_add_trailing (absolute_path ) or absolute_path
328
- copy_to_clipboard (content )
363
+ self : copy_to_clipboard (content )
329
364
end
330
365
331
366
--- Node is cut. Will not be copied.
332
367
--- @param node Node
333
368
--- @return boolean
334
- function M . is_cut (node )
335
- return vim .tbl_contains (clipboard .cut , node )
369
+ function Clipboard : is_cut (node )
370
+ return vim .tbl_contains (self . data .cut , node )
336
371
end
337
372
338
373
--- Node is copied. Will not be cut.
339
374
--- @param node Node
340
375
--- @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
376
+ function Clipboard :is_copied (node )
377
+ return vim .tbl_contains (self .data .copy , node )
348
378
end
349
379
350
- return M
380
+ return Clipboard
0 commit comments