Skip to content

Commit 9689d0d

Browse files
committed
feat(#1079): add highlight NvimTreeCopiedText and NvimTreeCutText
1 parent 94c7c81 commit 9689d0d

File tree

9 files changed

+97
-41
lines changed

9 files changed

+97
-41
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,10 @@ Standard: >
21662166
NvimTreeStatusLine StatusLine
21672167
NvimTreeStatusLineNC StatusLineNC
21682168
<
2169+
Clipboard: >
2170+
NvimTreeCopiedText
2171+
NvimTreeCutText
2172+
<
21692173
Picker: >
21702174
NvimTreeWindowPicker
21712175
<

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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"
78

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

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

155157
function M.copy(node)
156158
add_to_clipboard(node, clipboard.copy)
159+
renderer.draw()
157160
end
158161

159162
function M.cut(node)
160163
add_to_clipboard(node, clipboard.move)
164+
renderer.draw()
161165
end
162166

163167
local function do_paste(node, action_type, action_fn)
@@ -267,6 +271,23 @@ function M.copy_absolute_path(node)
267271
return copy_to_clipboard(content)
268272
end
269273

274+
---clipboard text highlight group
275+
---@param node table
276+
---@return string|nil group
277+
function M.get_highlight(node)
278+
for _, n in ipairs(clipboard.move) do
279+
if node == n then
280+
return "NvimTreeCutText"
281+
end
282+
end
283+
284+
for _, n in ipairs(clipboard.copy) do
285+
if node == n then
286+
return "NvimTreeCopiedText"
287+
end
288+
end
289+
end
290+
270291
function M.setup(opts)
271292
M.config.filesystem_watchers = opts.filesystem_watchers
272293
M.config.actions = opts.actions

lua/nvim-tree/colors.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ local function get_hl_groups()
4242
OpenedFile = { gui = "bold", fg = colors.green },
4343
ModifiedFile = { fg = colors.green },
4444

45+
CopiedText = { gui = "underline" },
46+
CutText = { gui = "strikethrough" },
47+
4548
GitDirty = { fg = colors.dark_red },
4649
GitDeleted = { fg = colors.dark_red },
4750
GitStaged = { fg = colors.green },

lua/nvim-tree/renderer/builder.lua

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114

115115
---@class HighlightedString
116116
---@field str string
117-
---@field hl string|nil
117+
---@field hl string[]
118118

119119
---@param highlighted_strings HighlightedString[]
120120
---@return string
@@ -126,7 +126,7 @@ function Builder:_unwrap_highlighted_strings(highlighted_strings)
126126
local string = ""
127127
for _, v in ipairs(highlighted_strings) do
128128
if #v.str > 0 then
129-
if v.hl then
129+
if v.hl and type(v.hl) == "table" then
130130
self:_insert_highlight(v.hl, #string, #string + #v.str)
131131
end
132132
string = string .. v.str
@@ -136,7 +136,8 @@ function Builder:_unwrap_highlighted_strings(highlighted_strings)
136136
end
137137

138138
---@param node table
139-
---@return HighlightedString icon, HighlightedString name
139+
---@return HighlightedString icon
140+
---@return HighlightedString name
140141
function Builder:_build_folder(node)
141142
local has_children = #node.nodes ~= 0 or node.has_children
142143
local icon, icon_hl = icons.get_folder_icon(node, has_children)
@@ -166,11 +167,12 @@ function Builder:_build_folder(node)
166167
foldername_hl = "NvimTreeEmptyFolderName"
167168
end
168169

169-
return { str = icon, hl = icon_hl }, { str = foldername, hl = foldername_hl }
170+
return { str = icon, hl = { icon_hl } }, { str = foldername, hl = { foldername_hl } }
170171
end
171172

172173
---@param node table
173-
---@return HighlightedString icon, HighlightedString name
174+
---@return HighlightedString icon
175+
---@return HighlightedString name
174176
function Builder:_build_symlink(node)
175177
local icon = icons.i.symlink
176178
local arrow = icons.i.symlink_arrow
@@ -180,21 +182,19 @@ function Builder:_build_symlink(node)
180182
symlink_formatted = symlink_formatted .. arrow .. link_to
181183
end
182184

183-
local link_highlight = "NvimTreeSymlink"
184-
local icon_hl = "NvimTreeSymlinkIcon"
185-
186-
return { str = icon, hl = icon_hl }, { str = symlink_formatted, hl = link_highlight }
185+
return { str = icon, hl = { "NvimTreeSymlinkIcon" } }, { str = symlink_formatted, hl = { "NvimTreeSymlink" } }
187186
end
188187

189188
---@param node table
190189
---@return HighlightedString icon
191190
function Builder:_build_file_icon(node)
192191
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
193-
return { str = icon, hl = hl_group }
192+
return { str = icon, hl = { hl_group } }
194193
end
195194

196195
---@param node table
197-
---@return HighlightedString icon, HighlightedString name
196+
---@return HighlightedString icon
197+
---@return HighlightedString name
198198
function Builder:_build_file(node)
199199
local icon = self:_build_file_icon(node)
200200

@@ -207,15 +207,15 @@ function Builder:_build_file(node)
207207
hl = "NvimTreeImageFile"
208208
end
209209

210-
return icon, { str = node.name, hl = hl }
210+
return icon, { str = node.name, hl = { hl } }
211211
end
212212

213213
---@param node table
214214
---@return HighlightedString[]|nil icon
215215
function Builder:_get_git_icons(node)
216216
local git_icons = git.get_icons(node)
217217
if git_icons and #git_icons > 0 and self.git_placement == "signcolumn" then
218-
table.insert(self.signs, { sign = git_icons[1].hl, lnum = self.index + 1, priority = 1 })
218+
table.insert(self.signs, { sign = git_icons[1].hl[1], lnum = self.index + 1, priority = 1 })
219219
git_icons = nil
220220
end
221221
return git_icons
@@ -226,7 +226,7 @@ end
226226
function Builder:_get_diagnostics_icon(node)
227227
local diagnostics_icon = diagnostics.get_icon(node)
228228
if diagnostics_icon and self.diagnostics_placement == "signcolumn" then
229-
table.insert(self.signs, { sign = diagnostics_icon.hl, lnum = self.index + 1, priority = 2 })
229+
table.insert(self.signs, { sign = diagnostics_icon.hl[1], lnum = self.index + 1, priority = 2 })
230230
diagnostics_icon = nil
231231
end
232232
return diagnostics_icon
@@ -237,14 +237,15 @@ end
237237
function Builder:_get_modified_icon(node)
238238
local modified_icon = modified.get_icon(node)
239239
if modified_icon and self.modified_placement == "signcolumn" then
240-
table.insert(self.signs, { sign = modified_icon.hl, lnum = self.index + 1, priority = 3 })
240+
table.insert(self.signs, { sign = modified_icon.hl[1], lnum = self.index + 1, priority = 3 })
241241
modified_icon = nil
242242
end
243243
return modified_icon
244244
end
245245

246246
---@param node table
247-
---@return string icon_highlight, string name_highlight
247+
---@return string|nil icon_hl
248+
---@return string|nil name_hl
248249
function Builder:_get_highlight_override(node, unloaded_bufnr)
249250
-- highlights precedence:
250251
-- original < git < opened_file < modified
@@ -290,6 +291,20 @@ function Builder:_get_highlight_override(node, unloaded_bufnr)
290291
return icon_hl, name_hl
291292
end
292293

294+
---@param node table
295+
---@return string[] icon_hl
296+
---@return string[] name_hl
297+
function Builder:_get_highlight_extra(node)
298+
local icon_hl = {}
299+
local name_hl = {}
300+
301+
-- clipboard
302+
local clipboard_highlight = require "nvim-tree.actions.fs.copy-paste".get_highlight(node)
303+
table.insert(name_hl, clipboard_highlight)
304+
305+
return icon_hl, name_hl
306+
end
307+
293308
---@param indent_markers HighlightedString[]
294309
---@param arrows HighlightedString[]|nil
295310
---@param icon HighlightedString
@@ -364,12 +379,21 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
364379
end
365380

366381
-- highlight override
367-
local icon_hl, name_hl = self:_get_highlight_override(node, unloaded_bufnr)
368-
if icon_hl then
369-
icon.hl = icon_hl
382+
local icon_hl_override, name_hl_override = self:_get_highlight_override(node, unloaded_bufnr)
383+
if icon_hl_override then
384+
icon.hl = { icon_hl_override }
385+
end
386+
if name_hl_override then
387+
name.hl = { name_hl_override }
388+
end
389+
390+
-- extra highighting
391+
local icon_hl_extra, name_hl_extra = self:_get_highlight_extra(node)
392+
for _, hl in ipairs(icon_hl_extra) do
393+
table.insert(icon.hl, hl)
370394
end
371-
if name_hl then
372-
name.hl = name_hl
395+
for _, hl in ipairs(name_hl_extra) do
396+
table.insert(name.hl, hl)
373397
end
374398

375399
local line = self:_format_line(indent_markers, arrows, icon, name, git_icons, diagnostics_icon, modified_icon)
@@ -429,16 +453,16 @@ function Builder:build_header(show_header)
429453
if show_header then
430454
local root_name = format_root_name(self.root_cwd, self.root_folder_label)
431455
self:_insert_line(root_name)
432-
self:_insert_highlight("NvimTreeRootFolder", 0, string.len(root_name))
456+
self:_insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))
433457
self.index = 1
434458
end
435459

436460
if self.filter then
437461
local filter_line = self.filter_prefix .. "/" .. self.filter .. "/"
438462
self:_insert_line(filter_line)
439463
local prefix_length = string.len(self.filter_prefix)
440-
self:_insert_highlight("NvimTreeLiveFilterPrefix", 0, prefix_length)
441-
self:_insert_highlight("NvimTreeLiveFilterValue", prefix_length, string.len(filter_line))
464+
self:_insert_highlight({ "NvimTreeLiveFilterPrefix" }, 0, prefix_length)
465+
self:_insert_highlight({ "NvimTreeLiveFilterValue" }, prefix_length, string.len(filter_line))
442466
self.index = self.index + 1
443467
end
444468

lua/nvim-tree/renderer/components/diagnostics.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local ICON = {}
66

77
---diagnostics text highlight group if there is a status
88
---@param node table
9-
---@return string|nil highlight
9+
---@return string|nil group
1010
function M.get_highlight(node)
1111
if node and M.config.diagnostics.enable and M.config.renderer.highlight_diagnostics then
1212
if node.nodes then
@@ -44,24 +44,24 @@ function M.setup(opts)
4444

4545
ICON[vim.diagnostic.severity.ERROR] = {
4646
str = M.config.diagnostics.icons.error,
47-
hl = "NvimTreeLspDiagnosticsError",
47+
hl = { "NvimTreeLspDiagnosticsError" },
4848
}
4949

5050
ICON[vim.diagnostic.severity.WARN] = {
5151
str = M.config.diagnostics.icons.warning,
52-
hl = "NvimTreeLspDiagnosticsWarning",
52+
hl = { "NvimTreeLspDiagnosticsWarning" },
5353
}
5454
ICON[vim.diagnostic.severity.INFO] = {
5555
str = M.config.diagnostics.icons.info,
56-
hl = "NvimTreeLspDiagnosticsInfo",
56+
hl = { "NvimTreeLspDiagnosticsInfo" },
5757
}
5858
ICON[vim.diagnostic.severity.HINT] = {
5959
str = M.config.diagnostics.icons.hint,
60-
hl = "NvimTreeLspDiagnosticsHint",
60+
hl = { "NvimTreeLspDiagnosticsHint" },
6161
}
6262

6363
for _, i in ipairs(ICON) do
64-
vim.fn.sign_define(i.hl, { text = i.str, texthl = i.hl })
64+
vim.fn.sign_define(i.hl[1], { text = i.str, texthl = i.hl[1] })
6565
end
6666
end
6767

lua/nvim-tree/renderer/components/git.lua

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

66
local function build_icons_table(i)
77
local icons = {
8-
staged = { str = i.staged, hl = "NvimTreeGitStaged", ord = 1 },
9-
unstaged = { str = i.unstaged, hl = "NvimTreeGitDirty", ord = 2 },
10-
renamed = { str = i.renamed, hl = "NvimTreeGitRenamed", ord = 3 },
11-
deleted = { str = i.deleted, hl = "NvimTreeGitDeleted", ord = 4 },
12-
unmerged = { str = i.unmerged, hl = "NvimTreeGitMerge", ord = 5 },
13-
untracked = { str = i.untracked, hl = "NvimTreeGitNew", ord = 6 },
14-
ignored = { str = i.ignored, hl = "NvimTreeGitIgnored", ord = 7 },
8+
staged = { str = i.staged, hl = { "NvimTreeGitStaged" }, ord = 1 },
9+
unstaged = { str = i.unstaged, hl = { "NvimTreeGitDirty" }, ord = 2 },
10+
renamed = { str = i.renamed, hl = { "NvimTreeGitRenamed" }, ord = 3 },
11+
deleted = { str = i.deleted, hl = { "NvimTreeGitDeleted" }, ord = 4 },
12+
unmerged = { str = i.unmerged, hl = { "NvimTreeGitMerge" }, ord = 5 },
13+
untracked = { str = i.untracked, hl = { "NvimTreeGitNew" }, ord = 6 },
14+
ignored = { str = i.ignored, hl = { "NvimTreeGitIgnored" }, ord = 7 },
1515
}
1616
return {
1717
["M "] = { icons.staged },

lua/nvim-tree/renderer/components/modified.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function M.get_icon(node)
1212
return nil
1313
end
1414

15-
return { str = M.icon, hl = HIGHLIGHT }
15+
return { str = M.icon, hl = { HIGHLIGHT } }
1616
end
1717

1818
function M.setup_signs()

lua/nvim-tree/renderer/components/padding.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function M.get_indent_markers(depth, idx, nodes_number, node, markers)
7878
str = str .. string.rep(" ", depth * indent_width)
7979
end
8080

81-
return { str = str, hl = "NvimTreeIndentMarker" }
81+
return { str = str, hl = { "NvimTreeIndentMarker" } }
8282
end
8383

8484
---@param node table
@@ -104,7 +104,7 @@ function M.get_arrows(node)
104104
str = " "
105105
end
106106

107-
return { str = str, hl = hl }
107+
return { str = str, hl = { hl } }
108108
end
109109

110110
function M.setup(opts)

lua/nvim-tree/renderer/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ function M.render_hl(bufnr, hl)
3838
end
3939
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
4040
for _, data in ipairs(hl or M.last_highlights) do
41-
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, data[1], data[2], data[3], data[4])
41+
if type(data[1]) == "table" then
42+
for _, group in ipairs(data[1]) do
43+
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
44+
end
45+
end
4246
end
4347
end
4448

0 commit comments

Comments
 (0)