Skip to content

Commit 4a7d849

Browse files
committed
add DecoratorCopied DecoratorCut
1 parent 3f128e3 commit 4a7d849

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
@@ -791,7 +791,7 @@ Use nvim-tree in a floating window.
791791
5.3 OPTS: RENDERER *nvim-tree-opts-renderer*
792792

793793
Highlight precedence:
794-
clipboard > diagnostics > bookmarked > modified > opened > git
794+
cut > copied > diagnostics > bookmarked > modified > opened > git
795795

796796
*nvim-tree.renderer.add_trailing*
797797
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"
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 = {
@@ -317,34 +315,23 @@ function M.copy_absolute_path(node)
317315
copy_to_clipboard(content)
318316
end
319317

320-
--- Clipboard text highlight group and position when highlight_clipboard.
318+
---Node is cut. May also be copied.
321319
---@param node Node
322-
---@return HL_POSITION position none when clipboard empty
323-
---@return string|nil group only when node present in clipboard
324-
function M.get_highlight(node)
325-
if M.hl_pos == HL_POSITION.none then
326-
return HL_POSITION.none, nil
327-
end
328-
329-
for _, n in ipairs(clipboard.cut) do
330-
if node == n then
331-
return M.hl_pos, "NvimTreeCutHL"
332-
end
333-
end
334-
335-
for _, n in ipairs(clipboard.copy) do
336-
if node == n then
337-
return M.hl_pos, "NvimTreeCopiedHL"
338-
end
339-
end
320+
---@return boolean
321+
function M.is_cut(node)
322+
return vim.tbl_contains(clipboard.cut, node)
323+
end
340324

341-
return HL_POSITION.none, nil
325+
---Node is copied. May also be cut.
326+
---@param node Node
327+
---@return boolean
328+
function M.is_copied(node)
329+
return vim.tbl_contains(clipboard.copy, node)
342330
end
343331

344332
function M.setup(opts)
345333
M.config.filesystem_watchers = opts.filesystem_watchers
346334
M.config.actions = opts.actions
347-
M.hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none
348335
end
349336

350337
return M

lua/nvim-tree/renderer/builder.lua

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

375375
function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
376-
local copy_paste = require "nvim-tree.actions.fs.copy-paste"
377-
378376
-- various components
379377
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
380378
local arrows = pad.get_arrows(node)
@@ -411,7 +409,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
411409
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
412410
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
413411
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)
414-
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
412+
self:_append_dec_highlight(node, self.decorators.copied, icon.hl, name.hl)
413+
self:_append_dec_highlight(node, self.decorators.cut, icon.hl, name.hl)
415414

416415
local line = self:_format_line(indent_markers, arrows, icon, name, git_icons, diagnostics_icon, modified_icon, bookmark_icon)
417416
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"
@@ -106,9 +108,11 @@ function M.setup(opts)
106108

107109
-- TODO change to array: precedence should follow order
108110
M.decorators = {
109-
git = DecoratorGit:new(opts),
110111
bookmarks = DecoratorBookmarks:new(opts),
112+
copied = DecoratorCopied:new(opts),
113+
cut = DecoratorCut:new(opts),
111114
diagnostics = DecoratorDiagnostics:new(opts),
115+
git = DecoratorGit:new(opts),
112116
modified = DecoratorModified:new(opts),
113117
}
114118
end

0 commit comments

Comments
 (0)