Skip to content

Commit 5fb81f9

Browse files
committed
add DecoratorCopied DecoratorCut
1 parent d797d22 commit 5fb81f9

File tree

6 files changed

+88
-28
lines changed

6 files changed

+88
-28
lines changed

doc/nvim-tree-lua.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ Use nvim-tree in a floating window.
788788
5.3 OPTS: RENDERER *nvim-tree-opts-renderer*
789789

790790
Highlight precedence:
791-
clipboard > diagnostics > bookmarked > modified > opened > git
791+
cut > copied > diagnostics > bookmarked > modified > opened > git
792792

793793
*nvim-tree.renderer.add_trailing*
794794
Appends a trailing slash to folder names.

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ local notify = require "nvim-tree.notify"
77
local renderer = require "nvim-tree.renderer"
88
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
99

10-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
11-
1210
local find_file = require("nvim-tree.actions.finders.find-file").fn
1311

1412
local M = {
@@ -284,34 +282,23 @@ function M.copy_absolute_path(node)
284282
return copy_to_clipboard(content)
285283
end
286284

287-
---Clipboard text highlight group and position when highlight_clipboard.
285+
---Node is cut. May also be copied.
288286
---@param node table
289-
---@return HL_POSITION position none when clipboard empty
290-
---@return string|nil group only when node present in clipboard
291-
function M.get_highlight(node)
292-
if M.hl_pos == HL_POSITION.none then
293-
return HL_POSITION.none, nil
294-
end
295-
296-
for _, n in ipairs(clipboard.cut) do
297-
if node == n then
298-
return M.hl_pos, "NvimTreeCutHL"
299-
end
300-
end
301-
302-
for _, n in ipairs(clipboard.copy) do
303-
if node == n then
304-
return M.hl_pos, "NvimTreeCopiedHL"
305-
end
306-
end
287+
---@return boolean
288+
function M.is_cut(node)
289+
return vim.tbl_contains(clipboard.cut, node)
290+
end
307291

308-
return HL_POSITION.none, nil
292+
---Node is copied. May also be cut.
293+
---@param node table
294+
---@return boolean
295+
function M.is_copied(node)
296+
return vim.tbl_contains(clipboard.copy, node)
309297
end
310298

311299
function M.setup(opts)
312300
M.config.filesystem_watchers = opts.filesystem_watchers
313301
M.config.actions = opts.actions
314-
M.hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none
315302
end
316303

317304
return M

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
355355
end
356356

357357
function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
358-
local copy_paste = require "nvim-tree.actions.fs.copy-paste"
359-
360358
-- various components
361359
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
362360
local arrows = pad.get_arrows(node)
@@ -393,7 +391,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
393391
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
394392
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
395393
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)
396-
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
394+
self:_append_dec_highlight(node, self.decorators.copied, icon.hl, name.hl)
395+
self:_append_dec_highlight(node, self.decorators.cut, icon.hl, name.hl)
397396

398397
local line = self:_format_line(indent_markers, arrows, icon, name, git_icons, diagnostics_icon, modified_icon, bookmark_icon)
399398
self:_insert_line(self:_unwrap_highlighted_strings(line))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local copy_paste
2+
3+
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5+
6+
local Decorator = require "nvim-tree.renderer.decorator"
7+
8+
--- @class DecoratorCopied: Decorator
9+
--- @field enabled boolean
10+
--- @field icon HighlightedString|nil
11+
local DecoratorCopied = Decorator:new()
12+
13+
--- @param opts table
14+
--- @return DecoratorCopied
15+
function DecoratorCopied:new(opts)
16+
local o = Decorator.new(self, {
17+
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
18+
icon_placement = ICON_PLACEMENT.none,
19+
})
20+
---@cast o DecoratorCopied
21+
22+
-- cyclic
23+
copy_paste = copy_paste or require "nvim-tree.actions.fs.copy-paste"
24+
25+
return o
26+
end
27+
28+
--- Cut highlight: renderer.highlight_clipboard and node is copied
29+
function DecoratorCopied:get_highlight(node)
30+
if self.hl_pos ~= HL_POSITION.none and copy_paste.is_copied(node) then
31+
return "NvimTreeCopiedHL"
32+
end
33+
end
34+
35+
return DecoratorCopied
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local copy_paste
2+
3+
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5+
6+
local Decorator = require "nvim-tree.renderer.decorator"
7+
8+
--- @class DecoratorCut: Decorator
9+
--- @field enabled boolean
10+
--- @field icon HighlightedString|nil
11+
local DecoratorCut = Decorator:new()
12+
13+
--- @param opts table
14+
--- @return DecoratorCut
15+
function DecoratorCut:new(opts)
16+
local o = Decorator.new(self, {
17+
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
18+
icon_placement = ICON_PLACEMENT.none,
19+
})
20+
---@cast o DecoratorCut
21+
22+
-- cyclic
23+
copy_paste = copy_paste or require "nvim-tree.actions.fs.copy-paste"
24+
25+
return o
26+
end
27+
28+
--- Cut highlight: renderer.highlight_clipboard and node is cut
29+
function DecoratorCut:get_highlight(node)
30+
if self.hl_pos ~= HL_POSITION.none and copy_paste.is_cut(node) then
31+
return "NvimTreeCutHL"
32+
end
33+
end
34+
35+
return DecoratorCut

lua/nvim-tree/renderer/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ local Builder = require "nvim-tree.renderer.builder"
1010
local live_filter = require "nvim-tree.live-filter"
1111

1212
local DecoratorBookmarks = require "nvim-tree.renderer.decorator.bookmarks"
13+
local DecoratorCopied = require "nvim-tree.renderer.decorator.copied"
14+
local DecoratorCut = require "nvim-tree.renderer.decorator.cut"
1315
local DecoratorDiagnostics = require "nvim-tree.renderer.decorator.diagnostics"
1416
local DecoratorGit = require "nvim-tree.renderer.decorator.git"
1517
local DecoratorModified = require "nvim-tree.renderer.decorator.modified"
@@ -105,9 +107,11 @@ function M.setup(opts)
105107

106108
-- TODO change to array: precedence should follow order
107109
M.decorators = {
108-
git = DecoratorGit:new(opts),
109110
bookmarks = DecoratorBookmarks:new(opts),
111+
copied = DecoratorCopied:new(opts),
112+
cut = DecoratorCut:new(opts),
110113
diagnostics = DecoratorDiagnostics:new(opts),
114+
git = DecoratorGit:new(opts),
111115
modified = DecoratorModified:new(opts),
112116
}
113117
end

0 commit comments

Comments
 (0)