Skip to content

Commit 632ad2c

Browse files
committed
refactor(#2831): multi instance clipboard
1 parent 85c83cf commit 632ad2c

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

Diff for: lua/nvim-tree/actions/fs/copy-paste.lua

+32-37
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,14 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
1111

1212
---@enum ACTION
1313
local ACTION = {
14-
none = 0,
15-
copy = 1,
16-
cut = 2,
14+
copy = "copy",
15+
cut = "cut",
1716
}
1817

19-
---@class ClipboardData absolute paths
20-
---@field copy string[] copied
21-
---@field cut string[] cut
22-
2318
---@class Clipboard to handle all actions.fs clipboard API
2419
---@field config table hydrated user opts.filters
2520
---@field private explorer Explorer
26-
---@field private data ClipboardData
21+
---@field private data table<ACTION, Node[]>
2722
local Clipboard = {}
2823

2924
---@param opts table user options
@@ -33,8 +28,8 @@ function Clipboard:new(opts, explorer)
3328
local o = {
3429
explorer = explorer,
3530
data = {
36-
copy = {},
37-
cut = {},
31+
[ACTION.copy] = {},
32+
[ACTION.cut] = {},
3833
},
3934
config = {
4035
filesystem_watchers = opts.filesystem_watchers,
@@ -196,40 +191,40 @@ end
196191

197192
---Clear copied and cut
198193
function Clipboard:clear_clipboard()
199-
self.data.cut = {}
200-
self.data.copy = {}
194+
self.data[ACTION.copy] = {}
195+
self.data[ACTION.cut] = {}
201196
notify.info "Clipboard has been emptied."
202197
renderer.draw()
203198
end
204199

205200
---Copy one node
206201
---@param node Node
207202
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])
210205
renderer.draw()
211206
end
212207

213208
---Cut one node
214209
---@param node Node
215210
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])
218213
renderer.draw()
219214
end
220215

221216
---Paste cut or cop
222217
---@private
223218
---@param node Node
224-
---@param action_type string
219+
---@param action ACTION
225220
---@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)
227222
node = lib.get_last_group_node(node)
228223
local explorer = core.get_explorer()
229224
if node.name == ".." and explorer then
230225
node = explorer
231226
end
232-
local clip = self.data[action_type]
227+
local clip = self.data[action]
233228
if #clip == 0 then
234229
return
235230
end
@@ -238,7 +233,7 @@ function Clipboard:do_paste(node, action_type, action_fn)
238233
local stats, errmsg, errcode = vim.loop.fs_stat(destination)
239234
if not stats and errcode ~= "ENOENT" then
240235
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 "???"))
242237
return
243238
end
244239
local is_dir = stats and stats.type == "directory"
@@ -248,10 +243,10 @@ function Clipboard:do_paste(node, action_type, action_fn)
248243

249244
for _, _node in ipairs(clip) do
250245
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)
252247
end
253248

254-
self.data[action_type] = {}
249+
self.data[action] = {}
255250
if not self.config.filesystem_watchers.enable then
256251
reloaders.reload_explorer()
257252
end
@@ -283,24 +278,24 @@ end
283278
---Paste cut (if present) or copy (if present)
284279
---@param node Node
285280
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)
290285
end
291286
end
292287

293288
function Clipboard:print_clipboard()
294289
local content = {}
295-
if #self.data.cut > 0 then
290+
if #self.data[ACTION.cut] > 0 then
296291
table.insert(content, "Cut")
297-
for _, node in pairs(self.data.cut) do
292+
for _, node in pairs(self.data[ACTION.cut]) do
298293
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
299294
end
300295
end
301-
if #self.data.copy > 0 then
296+
if #self.data[ACTION.copy] > 0 then
302297
table.insert(content, "Copy")
303-
for _, node in pairs(self.data.copy) do
298+
for _, node in pairs(self.data[ACTION.copy]) do
304299
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
305300
end
306301
end
@@ -309,7 +304,7 @@ function Clipboard:print_clipboard()
309304
end
310305

311306
---@param content string
312-
function Clipboard:copy_to_clipboard(content)
307+
function Clipboard:copy_to_reg(content)
313308
local clipboard_name
314309
local reg
315310
if self.config.actions.use_system_clipboard == true then
@@ -334,13 +329,13 @@ end
334329

335330
---@param node Node
336331
function Clipboard:copy_filename(node)
337-
self:copy_to_clipboard(node.name)
332+
self:copy_to_reg(node.name)
338333
end
339334

340335
---@param node Node
341336
function Clipboard:copy_basename(node)
342337
local basename = vim.fn.fnamemodify(node.name, ":r")
343-
self:copy_to_clipboard(basename)
338+
self:copy_to_reg(basename)
344339
end
345340

346341
---@param node Node
@@ -353,28 +348,28 @@ function Clipboard:copy_path(node)
353348

354349
local relative_path = utils.path_relative(absolute_path, cwd)
355350
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)
357352
end
358353

359354
---@param node Node
360355
function Clipboard:copy_absolute_path(node)
361356
local absolute_path = node.absolute_path
362357
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)
364359
end
365360

366361
---Node is cut. Will not be copied.
367362
---@param node Node
368363
---@return boolean
369364
function Clipboard:is_cut(node)
370-
return vim.tbl_contains(self.data.cut, node)
365+
return vim.tbl_contains(self.data[ACTION.cut], node)
371366
end
372367

373368
---Node is copied. Will not be cut.
374369
---@param node Node
375370
---@return boolean
376371
function Clipboard:is_copied(node)
377-
return vim.tbl_contains(self.data.copy, node)
372+
return vim.tbl_contains(self.data[ACTION.copy], node)
378373
end
379374

380375
return Clipboard

0 commit comments

Comments
 (0)