Skip to content

Commit 6f1b818

Browse files
committed
feat(clipboard): always copy/cut to register and make p pick local data or register data
1 parent 356c083 commit 6f1b818

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

lua/nvim-tree/actions/fs/clipboard.lua

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ function Clipboard:new(args)
4444
self.reg = self.explorer.opts.actions.use_system_clipboard and "+" or "1"
4545
end
4646

47-
---@class PasteOptions
48-
---@field use_register? boolean
49-
---@field cut? boolean
50-
5147
---@param source string
5248
---@param destination string
5349
---@return boolean
@@ -201,6 +197,7 @@ function Clipboard:copy(node_or_nodes)
201197
else
202198
self:bulk_clipboard(utils.filter_descendant_nodes(node_or_nodes), self.data.cut, self.data.copy, "added to")
203199
end
200+
self:copy_node_attribute(node_or_nodes, "absolute_path", { notify = false })
204201
end
205202

206203
---Cut one or more nodes
@@ -213,6 +210,7 @@ function Clipboard:cut(node_or_nodes)
213210
else
214211
self:bulk_clipboard(utils.filter_descendant_nodes(node_or_nodes), self.data.copy, self.data.cut, "cut to")
215212
end
213+
self:copy_node_attribute(node_or_nodes, "absolute_path", { notify = false })
216214
end
217215

218216
---Clear clipboard for action and reload to reflect filesystem changes from paste.
@@ -325,7 +323,7 @@ function Clipboard:get_nodes_from_reg()
325323
local content = vim.fn.getreg(self.reg)
326324

327325
if #content == 0 then
328-
return
326+
return {}
329327
end
330328

331329
local nodes = {}
@@ -348,8 +346,7 @@ end
348346
---@param node Node
349347
---@param action ClipboardAction
350348
---@param action_fn ClipboardActionFn
351-
---@param opts? PasteOptions
352-
function Clipboard:do_paste(node, action, action_fn, opts)
349+
function Clipboard:do_paste(node, action, action_fn)
353350
if node.name == ".." then
354351
node = self.explorer
355352
else
@@ -358,7 +355,8 @@ function Clipboard:do_paste(node, action, action_fn, opts)
358355
node = dir:last_group_node()
359356
end
360357
end
361-
local clip = opts and opts.use_register and self:get_nodes_from_reg() or self.data[action]
358+
local clip = #self.data[action] > 0 and self.data[action] or self:get_nodes_from_reg()
359+
362360
if #clip == 0 then
363361
return
364362
end
@@ -426,12 +424,13 @@ end
426424

427425
---Paste cut (if present) or copy (if present)
428426
---@param node Node
429-
---@param opts? PasteOptions
427+
---@param opts? { cut?: boolean }
430428
function Clipboard:paste(node, opts)
431-
if self.data.cut[1] ~= nil or opts and opts.use_register and opts.cut then
432-
self:do_paste(node, "cut", do_cut, opts)
433-
elseif self.data.copy[1] ~= nil or opts and opts.use_register then
434-
self:do_paste(node, "copy", do_copy, opts)
429+
opts = opts and opts or {}
430+
if self.data.cut[1] ~= nil or opts.cut == true then
431+
self:do_paste(node, "cut", do_cut)
432+
else
433+
self:do_paste(node, "copy", do_copy)
435434
end
436435
end
437436

@@ -454,11 +453,15 @@ function Clipboard:print_clipboard()
454453
end
455454

456455
---@param content string
457-
---@param msg? string
458-
function Clipboard:copy_to_reg(content, msg)
456+
---@param message? string
457+
---@param opts? { notify?: boolean }
458+
function Clipboard:copy_to_reg(content, message, opts)
459+
opts = opts and opts or {}
459460
vim.fn.setreg(self.reg, type(content) == "table" and content or { content }, "v")
460461

461-
notify.info(msg or string.format("Copied %s to %s clipboard!", content, self.clipboard_name))
462+
if opts.notify ~= false then
463+
notify.info(message or string.format("Copied %s to %s clipboard!", content, self.clipboard_name))
464+
end
462465
end
463466

464467
---@param node Node
@@ -475,7 +478,9 @@ end
475478

476479
---@param node_or_nodes Node|Node[]
477480
---@param attribute "absolute_path" | "basename" | "filename" | "relative_path"
478-
function Clipboard:copy_node_attribute(node_or_nodes, attribute)
481+
---@param opts? { notify?: boolean }
482+
function Clipboard:copy_node_attribute(node_or_nodes, attribute, opts)
483+
opts = opts and opts or {}
479484
local content
480485
local node_attribute_getters = {
481486
basename = function(n) return self:get_node_basename(n) end,
@@ -505,7 +510,7 @@ function Clipboard:copy_node_attribute(node_or_nodes, attribute)
505510
if not is_single then
506511
message = string.format("%s %s copied to register", #content, attribute:gsub("_", " ") .. "s")
507512
end
508-
self:copy_to_reg(content, message)
513+
self:copy_to_reg(content, message, opts)
509514
end
510515
end
511516

lua/nvim-tree/api/impl.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ function M.hydrate_post_setup(api)
163163
api.fs.create = _n(function(n) require("nvim-tree.actions.fs.create-file").fn(n) end)
164164
api.fs.cut = ev(function(e, n) e.clipboard:cut(n) end)
165165
api.fs.paste = en(function(e, n) e.clipboard:paste(n) end)
166-
api.fs.paste_from_register_copying = en(function(e, n) e.clipboard:paste(n, { use_register = true }) end)
167-
api.fs.paste_from_register_cutting = en(function(e, n) e.clipboard:paste(n, { use_register = true, cut = true }) end)
166+
api.fs.paste_while_cutting = en(function(e, n) e.clipboard:paste(n, { cut = true }) end)
168167
api.fs.print_clipboard = e_(function(e) e.clipboard:print_clipboard() end)
169168
api.fs.remove = _v(function(n) require("nvim-tree.actions.fs.remove-file").fn(n) end)
170169
api.fs.rename = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_node(n) end)

0 commit comments

Comments
 (0)