Skip to content

feat(#1079): add renderer.highlight_clipboard default name, default undercurls #2410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 17, 2023
11 changes: 11 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
highlight_diagnostics = false,
highlight_opened_files = "none",
highlight_modified = "none",
highlight_clipboard = "name",
indent_markers = {
enable = false,
inline_arrows = true,
Expand Down Expand Up @@ -843,6 +844,12 @@ Value can be `"none"`, `"icon"`, `"name"` or `"all"`
This can be used with or without the icons.
Type: `string`, Default `"none"`

*nvim-tree.renderer.highlight_clipboard*
Enable highlight for clipboard items using the `NvimTreeCutHL` and
`NvimTreeCopiedHL` groups.
Value can be `"none"`, `"icon"`, `"name"` or `"all"`.
Type: `string`, Default: `"name"`

*nvim-tree.renderer.indent_markers*
Configuration options for tree indent markers.

Expand Down Expand Up @@ -2171,6 +2178,10 @@ Standard: >
NvimTreeStatusLine StatusLine
NvimTreeStatusLineNC StatusLineNC
<
Clipboard: >
NvimTreeCopiedHL SpellRare
NvimTreeCutHL SpellBad
<
Picker: >
NvimTreeWindowPicker
<
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
highlight_diagnostics = false,
highlight_opened_files = "none",
highlight_modified = "none",
highlight_clipboard = "name",
indent_markers = {
enable = false,
inline_arrows = true,
Expand Down
65 changes: 48 additions & 17 deletions lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"
local renderer = require "nvim-tree.renderer"

local HL_POSITION = require("nvim-tree.enum").HL_POSITION

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

Expand All @@ -12,7 +15,7 @@ local M = {
}

local clipboard = {
move = {},
cut = {},
copy = {},
}

Expand Down Expand Up @@ -130,34 +133,37 @@ local function do_single_paste(source, dest, action_type, action_fn)
end
end

local function add_to_clipboard(node, clip)
local function toggle(node, clip)
if node.name == ".." then
return
end
local notify_node = notify.render_path(node.absolute_path)

for idx, _node in ipairs(clip) do
if _node.absolute_path == node.absolute_path then
table.remove(clip, idx)
return notify.info(notify_node .. " removed from clipboard.")
end
if utils.array_remove(clip, node) then
return notify.info(notify_node .. " removed from clipboard.")
end

table.insert(clip, node)
notify.info(notify_node .. " added to clipboard.")
end

function M.clear_clipboard()
clipboard.move = {}
clipboard.cut = {}
clipboard.copy = {}
notify.info "Clipboard has been emptied."
renderer.draw()
end

function M.copy(node)
add_to_clipboard(node, clipboard.copy)
utils.array_remove(clipboard.cut, node)
toggle(node, clipboard.copy)
renderer.draw()
end

function M.cut(node)
add_to_clipboard(node, clipboard.move)
utils.array_remove(clipboard.copy, node)
toggle(node, clipboard.cut)
renderer.draw()
end

local function do_paste(node, action_type, action_fn)
Expand Down Expand Up @@ -213,25 +219,25 @@ local function do_cut(source, destination)
end

function M.paste(node)
if clipboard.move[1] ~= nil then
return do_paste(node, "move", do_cut)
if clipboard.cut[1] ~= nil then
return do_paste(node, "cut", do_cut)
end

return do_paste(node, "copy", do_copy)
end

function M.print_clipboard()
local content = {}
if #clipboard.move > 0 then
if #clipboard.cut > 0 then
table.insert(content, "Cut")
for _, item in pairs(clipboard.move) do
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
for _, node in pairs(clipboard.cut) do
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
end
end
if #clipboard.copy > 0 then
table.insert(content, "Copy")
for _, item in pairs(clipboard.copy) do
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
for _, node in pairs(clipboard.copy) do
table.insert(content, " * " .. (notify.render_path(node.absolute_path)))
end
end

Expand Down Expand Up @@ -267,9 +273,34 @@ function M.copy_absolute_path(node)
return copy_to_clipboard(content)
end

---Clipboard text highlight group and position when highlight_clipboard.
---@param node table
---@return HL_POSITION position none when clipboard empty
---@return string|nil group only when node present in clipboard
function M.get_highlight(node)
if M.hl_pos == HL_POSITION.none then
return HL_POSITION.none, nil
end

for _, n in ipairs(clipboard.cut) do
if node == n then
return M.hl_pos, "NvimTreeCutHL"
end
end

for _, n in ipairs(clipboard.copy) do
if node == n then
return M.hl_pos, "NvimTreeCopiedHL"
end
end

return HL_POSITION.none, nil
end

function M.setup(opts)
M.config.filesystem_watchers = opts.filesystem_watchers
M.config.actions = opts.actions
M.hl_pos = HL_POSITION[opts.renderer.highlight_clipboard]
end

return M
2 changes: 2 additions & 0 deletions lua/nvim-tree/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ local function get_links()
StatusLine = "StatusLine",
StatusLineNC = "StatusLineNC",
SignColumn = "NvimTreeNormal",
CutHL = "SpellBad",
CopiedHL = "SpellRare",
}
end

Expand Down
20 changes: 20 additions & 0 deletions lua/nvim-tree/enum.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

---Setup options for "highlight_*"
---@enum HL_POSITION
M.HL_POSITION = {
none = 0,
icon = 1,
name = 2,
all = 4,
}

---Setup options for "*_placement"
---@enum ICON_PLACEMENT
M.ICON_PLACEMENT = {
signcolumn = 0,
before = 1,
after = 2,
}

return M
Loading