Skip to content

Commit a3aa3b4

Browse files
authored
feat(#1079): add renderer.highlight_clipboard default name, defaults to undercurls (#2410)
* feat(#1079): add highlight NvimTreeCopiedText and NvimTreeCutText * feat(#1079): add highlight NvimTreeCopiedText and NvimTreeCutText * feat(#1079): node may not be present in copy and cut * feat(#1079): add renderer.highlight_clipboard * feat(#1079): renderer.highlight_clipboard takes options, style cut/copy HL * feat(#1079): renderer.highlight_clipboard takes options, style cut/copy HL * feat(#1079): use an enum for highlight position * feat(#1079): diagnostics uses _append_highlight
1 parent f742b86 commit a3aa3b4

File tree

12 files changed

+205
-91
lines changed

12 files changed

+205
-91
lines changed

doc/nvim-tree-lua.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
388388
highlight_diagnostics = false,
389389
highlight_opened_files = "none",
390390
highlight_modified = "none",
391+
highlight_clipboard = "name",
391392
indent_markers = {
392393
enable = false,
393394
inline_arrows = true,
@@ -843,6 +844,12 @@ Value can be `"none"`, `"icon"`, `"name"` or `"all"`
843844
This can be used with or without the icons.
844845
Type: `string`, Default `"none"`
845846

847+
*nvim-tree.renderer.highlight_clipboard*
848+
Enable highlight for clipboard items using the `NvimTreeCutHL` and
849+
`NvimTreeCopiedHL` groups.
850+
Value can be `"none"`, `"icon"`, `"name"` or `"all"`.
851+
Type: `string`, Default: `"name"`
852+
846853
*nvim-tree.renderer.indent_markers*
847854
Configuration options for tree indent markers.
848855

@@ -2171,6 +2178,10 @@ Standard: >
21712178
NvimTreeStatusLine StatusLine
21722179
NvimTreeStatusLineNC StatusLineNC
21732180
<
2181+
Clipboard: >
2182+
NvimTreeCopiedHL SpellRare
2183+
NvimTreeCutHL SpellBad
2184+
<
21742185
Picker: >
21752186
NvimTreeWindowPicker
21762187
<

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
417417
highlight_diagnostics = false,
418418
highlight_opened_files = "none",
419419
highlight_modified = "none",
420+
highlight_clipboard = "name",
420421
indent_markers = {
421422
enable = false,
422423
inline_arrows = true,

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

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local utils = require "nvim-tree.utils"
44
local core = require "nvim-tree.core"
55
local events = require "nvim-tree.events"
66
local notify = require "nvim-tree.notify"
7+
local renderer = require "nvim-tree.renderer"
8+
9+
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
710

811
local find_file = require("nvim-tree.actions.finders.find-file").fn
912

@@ -12,7 +15,7 @@ local M = {
1215
}
1316

1417
local clipboard = {
15-
move = {},
18+
cut = {},
1619
copy = {},
1720
}
1821

@@ -130,34 +133,37 @@ local function do_single_paste(source, dest, action_type, action_fn)
130133
end
131134
end
132135

133-
local function add_to_clipboard(node, clip)
136+
local function toggle(node, clip)
134137
if node.name == ".." then
135138
return
136139
end
137140
local notify_node = notify.render_path(node.absolute_path)
138141

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.")
144144
end
145+
145146
table.insert(clip, node)
146147
notify.info(notify_node .. " added to clipboard.")
147148
end
148149

149150
function M.clear_clipboard()
150-
clipboard.move = {}
151+
clipboard.cut = {}
151152
clipboard.copy = {}
152153
notify.info "Clipboard has been emptied."
154+
renderer.draw()
153155
end
154156

155157
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()
157161
end
158162

159163
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()
161167
end
162168

163169
local function do_paste(node, action_type, action_fn)
@@ -213,25 +219,25 @@ local function do_cut(source, destination)
213219
end
214220

215221
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)
218224
end
219225

220226
return do_paste(node, "copy", do_copy)
221227
end
222228

223229
function M.print_clipboard()
224230
local content = {}
225-
if #clipboard.move > 0 then
231+
if #clipboard.cut > 0 then
226232
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)))
229235
end
230236
end
231237
if #clipboard.copy > 0 then
232238
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)))
235241
end
236242
end
237243

@@ -267,9 +273,34 @@ function M.copy_absolute_path(node)
267273
return copy_to_clipboard(content)
268274
end
269275

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+
270300
function M.setup(opts)
271301
M.config.filesystem_watchers = opts.filesystem_watchers
272302
M.config.actions = opts.actions
303+
M.hl_pos = HL_POSITION[opts.renderer.highlight_clipboard]
273304
end
274305

275306
return M

lua/nvim-tree/colors.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ local function get_links()
106106
StatusLine = "StatusLine",
107107
StatusLineNC = "StatusLineNC",
108108
SignColumn = "NvimTreeNormal",
109+
CutHL = "SpellBad",
110+
CopiedHL = "SpellRare",
109111
}
110112
end
111113

lua/nvim-tree/enum.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local M = {}
2+
3+
---Setup options for "highlight_*"
4+
---@enum HL_POSITION
5+
M.HL_POSITION = {
6+
none = 0,
7+
icon = 1,
8+
name = 2,
9+
all = 4,
10+
}
11+
12+
---Setup options for "*_placement"
13+
---@enum ICON_PLACEMENT
14+
M.ICON_PLACEMENT = {
15+
signcolumn = 0,
16+
before = 1,
17+
after = 2,
18+
}
19+
20+
return M

0 commit comments

Comments
 (0)