@@ -9,27 +9,25 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
99
1010local DirectoryNode = require (" nvim-tree.node.directory" )
1111
12- --- @enum ACTION
13- local ACTION = {
14- copy = " copy" ,
15- cut = " cut" ,
16- }
12+ --- @alias ClipboardAction " copy" | " cut"
13+ --- @alias ClipboardData table<ClipboardAction , Node[]>
1714
1815--- @class Clipboard to handle all actions.fs clipboard API
1916--- @field config table hydrated user opts.filters
2017--- @field private explorer Explorer
21- --- @field private data table<ACTION , Node[]>
18+ --- @field private data ClipboardData
2219local Clipboard = {}
2320
2421--- @param opts table user options
2522--- @param explorer Explorer
2623--- @return Clipboard
2724function Clipboard :new (opts , explorer )
25+ --- @type Clipboard
2826 local o = {
2927 explorer = explorer ,
3028 data = {
31- [ ACTION . copy ] = {},
32- [ ACTION . cut ] = {},
29+ copy = {},
30+ cut = {},
3331 },
3432 config = {
3533 filesystem_watchers = opts .filesystem_watchers ,
109107
110108--- @param source string
111109--- @param dest string
112- --- @param action ACTION
110+ --- @param action ClipboardAction
113111--- @param action_fn fun ( source : string , dest : string )
114112--- @return boolean | nil -- success
115113--- @return string | nil -- error message
@@ -173,7 +171,7 @@ local function do_single_paste(source, dest, action, action_fn)
173171end
174172
175173--- @param node Node
176- --- @param clip table
174+ --- @param clip ClipboardData
177175local function toggle (node , clip )
178176 if node .name == " .." then
179177 return
@@ -191,32 +189,32 @@ end
191189
192190--- Clear copied and cut
193191function Clipboard :clear_clipboard ()
194- self .data [ ACTION .copy ] = {}
195- self .data [ ACTION .cut ] = {}
192+ self .data .copy = {}
193+ self .data .cut = {}
196194 notify .info (" Clipboard has been emptied." )
197195 self .explorer .renderer :draw ()
198196end
199197
200198--- Copy one node
201199--- @param node Node
202200function 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 )
205203 self .explorer .renderer :draw ()
206204end
207205
208206--- Cut one node
209207--- @param node Node
210208function 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 )
213211 self .explorer .renderer :draw ()
214212end
215213
216214--- Paste cut or cop
217215--- @private
218216--- @param node Node
219- --- @param action ACTION
217+ --- @param action ClipboardAction
220218--- @param action_fn fun ( source : string , dest : string )
221219function Clipboard :do_paste (node , action , action_fn )
222220 if node .name == " .." then
@@ -281,24 +279,24 @@ end
281279--- Paste cut (if present) or copy (if present)
282280--- @param node Node
283281function 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 )
288286 end
289287end
290288
291289function Clipboard :print_clipboard ()
292290 local content = {}
293- if # self .data [ ACTION .cut ] > 0 then
291+ if # self .data .cut > 0 then
294292 table.insert (content , " Cut" )
295- for _ , node in pairs (self .data [ ACTION .cut ] ) do
293+ for _ , node in pairs (self .data .cut ) do
296294 table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
297295 end
298296 end
299- if # self .data [ ACTION .copy ] > 0 then
297+ if # self .data .copy > 0 then
300298 table.insert (content , " Copy" )
301- for _ , node in pairs (self .data [ ACTION .copy ] ) do
299+ for _ , node in pairs (self .data .copy ) do
302300 table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
303301 end
304302 end
@@ -397,14 +395,14 @@ end
397395--- @param node Node
398396--- @return boolean
399397function Clipboard :is_cut (node )
400- return vim .tbl_contains (self .data [ ACTION .cut ] , node )
398+ return vim .tbl_contains (self .data .cut , node )
401399end
402400
403401--- Node is copied. Will not be cut.
404402--- @param node Node
405403--- @return boolean
406404function Clipboard :is_copied (node )
407- return vim .tbl_contains (self .data [ ACTION .copy ] , node )
405+ return vim .tbl_contains (self .data .copy , node )
408406end
409407
410408return Clipboard
0 commit comments