@@ -4,6 +4,9 @@ local utils = require "nvim-tree.utils"
4
4
local core = require " nvim-tree.core"
5
5
local events = require " nvim-tree.events"
6
6
local notify = require " nvim-tree.notify"
7
+ local renderer = require " nvim-tree.renderer"
8
+
9
+ local HL_POSITION = require (" nvim-tree.enum" ).HL_POSITION
7
10
8
11
local find_file = require (" nvim-tree.actions.finders.find-file" ).fn
9
12
@@ -12,7 +15,7 @@ local M = {
12
15
}
13
16
14
17
local clipboard = {
15
- move = {},
18
+ cut = {},
16
19
copy = {},
17
20
}
18
21
@@ -130,34 +133,37 @@ local function do_single_paste(source, dest, action_type, action_fn)
130
133
end
131
134
end
132
135
133
- local function add_to_clipboard (node , clip )
136
+ local function toggle (node , clip )
134
137
if node .name == " .." then
135
138
return
136
139
end
137
140
local notify_node = notify .render_path (node .absolute_path )
138
141
139
- for idx , _node in ipairs (clip ) do
140
- if _node .absolute_path == node .absolute_path then
141
- table.remove (clip , idx )
142
- return notify .info (notify_node .. " removed from clipboard." )
143
- end
142
+ if utils .array_remove (clip , node ) then
143
+ return notify .info (notify_node .. " removed from clipboard." )
144
144
end
145
+
145
146
table.insert (clip , node )
146
147
notify .info (notify_node .. " added to clipboard." )
147
148
end
148
149
149
150
function M .clear_clipboard ()
150
- clipboard .move = {}
151
+ clipboard .cut = {}
151
152
clipboard .copy = {}
152
153
notify .info " Clipboard has been emptied."
154
+ renderer .draw ()
153
155
end
154
156
155
157
function M .copy (node )
156
- add_to_clipboard (node , clipboard .copy )
158
+ utils .array_remove (clipboard .cut , node )
159
+ toggle (node , clipboard .copy )
160
+ renderer .draw ()
157
161
end
158
162
159
163
function M .cut (node )
160
- add_to_clipboard (node , clipboard .move )
164
+ utils .array_remove (clipboard .copy , node )
165
+ toggle (node , clipboard .cut )
166
+ renderer .draw ()
161
167
end
162
168
163
169
local function do_paste (node , action_type , action_fn )
@@ -213,25 +219,25 @@ local function do_cut(source, destination)
213
219
end
214
220
215
221
function M .paste (node )
216
- if clipboard .move [1 ] ~= nil then
217
- return do_paste (node , " move " , do_cut )
222
+ if clipboard .cut [1 ] ~= nil then
223
+ return do_paste (node , " cut " , do_cut )
218
224
end
219
225
220
226
return do_paste (node , " copy" , do_copy )
221
227
end
222
228
223
229
function M .print_clipboard ()
224
230
local content = {}
225
- if # clipboard .move > 0 then
231
+ if # clipboard .cut > 0 then
226
232
table.insert (content , " Cut" )
227
- for _ , item in pairs (clipboard .move ) do
228
- table.insert (content , " * " .. (notify .render_path (item .absolute_path )))
233
+ for _ , node in pairs (clipboard .cut ) do
234
+ table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
229
235
end
230
236
end
231
237
if # clipboard .copy > 0 then
232
238
table.insert (content , " Copy" )
233
- for _ , item in pairs (clipboard .copy ) do
234
- table.insert (content , " * " .. (notify .render_path (item .absolute_path )))
239
+ for _ , node in pairs (clipboard .copy ) do
240
+ table.insert (content , " * " .. (notify .render_path (node .absolute_path )))
235
241
end
236
242
end
237
243
@@ -267,9 +273,34 @@ function M.copy_absolute_path(node)
267
273
return copy_to_clipboard (content )
268
274
end
269
275
276
+ --- Clipboard text highlight group and position when highlight_clipboard.
277
+ --- @param node table
278
+ --- @return HL_POSITION position none when clipboard empty
279
+ --- @return string | nil group only when node present in clipboard
280
+ function M .get_highlight (node )
281
+ if M .hl_pos == HL_POSITION .none then
282
+ return HL_POSITION .none , nil
283
+ end
284
+
285
+ for _ , n in ipairs (clipboard .cut ) do
286
+ if node == n then
287
+ return M .hl_pos , " NvimTreeCutHL"
288
+ end
289
+ end
290
+
291
+ for _ , n in ipairs (clipboard .copy ) do
292
+ if node == n then
293
+ return M .hl_pos , " NvimTreeCopiedHL"
294
+ end
295
+ end
296
+
297
+ return HL_POSITION .none , nil
298
+ end
299
+
270
300
function M .setup (opts )
271
301
M .config .filesystem_watchers = opts .filesystem_watchers
272
302
M .config .actions = opts .actions
303
+ M .hl_pos = HL_POSITION [opts .renderer .highlight_clipboard ]
273
304
end
274
305
275
306
return M
0 commit comments