Skip to content

Commit 4e161cb

Browse files
committed
feat(#1079): node may not be present in copy and cut
1 parent c9ed718 commit 4e161cb

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

lua/nvim-tree/actions/fs/copy-paste.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local M = {
1313
}
1414

1515
local clipboard = {
16-
move = {},
16+
cut = {},
1717
copy = {},
1818
}
1919

@@ -131,36 +131,36 @@ local function do_single_paste(source, dest, action_type, action_fn)
131131
end
132132
end
133133

134-
local function add_to_clipboard(node, clip)
134+
local function toggle(node, clip)
135135
if node.name == ".." then
136136
return
137137
end
138138
local notify_node = notify.render_path(node.absolute_path)
139139

140-
for idx, _node in ipairs(clip) do
141-
if _node.absolute_path == node.absolute_path then
142-
table.remove(clip, idx)
143-
return notify.info(notify_node .. " removed from clipboard.")
144-
end
140+
if utils.array_remove(clip, node) then
141+
return notify.info(notify_node .. " removed from clipboard.")
145142
end
143+
146144
table.insert(clip, node)
147145
notify.info(notify_node .. " added to clipboard.")
148146
end
149147

150148
function M.clear_clipboard()
151-
clipboard.move = {}
149+
clipboard.cut = {}
152150
clipboard.copy = {}
153151
notify.info "Clipboard has been emptied."
154152
renderer.draw()
155153
end
156154

157155
function M.copy(node)
158-
add_to_clipboard(node, clipboard.copy)
156+
utils.array_remove(clipboard.cut, node)
157+
toggle(node, clipboard.copy)
159158
renderer.draw()
160159
end
161160

162161
function M.cut(node)
163-
add_to_clipboard(node, clipboard.move)
162+
utils.array_remove(clipboard.copy, node)
163+
toggle(node, clipboard.cut)
164164
renderer.draw()
165165
end
166166

@@ -217,25 +217,25 @@ local function do_cut(source, destination)
217217
end
218218

219219
function M.paste(node)
220-
if clipboard.move[1] ~= nil then
221-
return do_paste(node, "move", do_cut)
220+
if clipboard.cut[1] ~= nil then
221+
return do_paste(node, "cut", do_cut)
222222
end
223223

224224
return do_paste(node, "copy", do_copy)
225225
end
226226

227227
function M.print_clipboard()
228228
local content = {}
229-
if #clipboard.move > 0 then
229+
if #clipboard.cut > 0 then
230230
table.insert(content, "Cut")
231-
for _, item in pairs(clipboard.move) do
232-
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
231+
for _, node in pairs(clipboard.cut) do
232+
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
233233
end
234234
end
235235
if #clipboard.copy > 0 then
236236
table.insert(content, "Copy")
237-
for _, item in pairs(clipboard.copy) do
238-
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
237+
for _, node in pairs(clipboard.copy) do
238+
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
239239
end
240240
end
241241

@@ -275,7 +275,7 @@ end
275275
---@param node table
276276
---@return string|nil group
277277
function M.get_highlight(node)
278-
for _, n in ipairs(clipboard.move) do
278+
for _, n in ipairs(clipboard.cut) do
279279
if node == n then
280280
return "NvimTreeCutText"
281281
end

lua/nvim-tree/utils.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,18 @@ function M.array_shallow_clone(array)
425425
return to
426426
end
427427

428-
-- remove item from array if it exists
428+
--- Remove and return item from array if present.
429+
--- @param array table
430+
--- @param item any
431+
--- @return any|nil removed
429432
function M.array_remove(array, item)
433+
if not array then
434+
return nil
435+
end
430436
for i, v in ipairs(array) do
431437
if v == item then
432438
table.remove(array, i)
433-
break
439+
return v
434440
end
435441
end
436442
end

0 commit comments

Comments
 (0)