Skip to content

feat: add trashing #749

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 11 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ require'nvim-tree'.setup {
},
number = false,
relativenumber = false
},
trash = {
cmd = "trash",
require_confirm = true
}
}
```
Expand Down Expand Up @@ -192,6 +196,7 @@ highlight NvimTreeFolderIcon guibg=blue
- type `gy` will copy absolute path to system clipboard
- type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation)
- type `d` to delete a file (will prompt for confirmation)
- type `D` to trash a file (configured in setup())
- type `]c` to go to next git item
- type `[c` to go to prev git item
- type `-` to navigate up to the parent directory of the current file/directory
Expand Down Expand Up @@ -243,6 +248,7 @@ local list = {
{ key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") },
Expand Down
15 changes: 15 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ function.
filters = {
dotfiles = false,
custom = {}
},
trash = {
cmd = "trash",
require_confirm = true,
}
}
<
Expand Down Expand Up @@ -321,6 +325,16 @@ Here is a list of the options available in the setup call:
type: `{string}`
default: `{}`

*nvim-tree.trash*
|trash|: configuration options for trashing

- |trash.cmd|: the command used to trash items (must be installed on your system)
type: `string`
default: `"trash"`

- |trash.require_confirm|: show a prompt before trashing takes place.
type: `boolean`
default: `true`

==============================================================================
OPTIONS *nvim-tree-options*
Expand Down Expand Up @@ -562,6 +576,7 @@ Defaults to:
{ key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") },
Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local renderer = require'nvim-tree.renderer'
local fs = require'nvim-tree.fs'
local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils'
local trash = require'nvim-tree.trash'

local _config = {
is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1,
Expand Down Expand Up @@ -151,6 +152,7 @@ local keypress_funcs = {
)
luv.unref(process.handle)
end,
trash = function(node) trash.trash_node(node, _config) end,
}

function M.on_keypress(mode)
Expand Down Expand Up @@ -453,6 +455,7 @@ function M.setup(conf)
_config.system_open = opts.system_open
_config.open_on_setup = opts.open_on_setup
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
_config.trash = opts.trash or {}
if type(opts.update_to_buf_dir) == "boolean" then
utils.warn("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
_config.update_to_buf_dir = {
Expand Down
72 changes: 72 additions & 0 deletions lua/nvim-tree/trash.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local M = {}

local lib = require'nvim-tree.lib'
local utils = require'nvim-tree.utils'
local events = require'nvim-tree.events'
local api = vim.api

local function clear_buffer(absolute_path)
local bufs = vim.fn.getbufinfo({bufloaded = 1, buflisted = 1})
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
if buf.hidden == 0 and #bufs > 1 then
local winnr = api.nvim_get_current_win()
api.nvim_set_current_win(buf.windows[1])
vim.cmd(':bn')
api.nvim_set_current_win(winnr)
end
vim.api.nvim_buf_delete(buf.bufnr, {})
return
end
end
end

function M.trash_node(node, cfg)
if node.name == '..' then return end

-- configs
if cfg.is_unix then
if cfg.trash.cmd == nil then cfg.trash.cmd = 'trash' end
if cfg.trash.require_confirm == nil then cfg.trash.require_confirm = true end
else
print('trash is currently a UNIX only feature!')
end

-- trashes a path (file or folder)
local function trash_path(on_exit)
vim.fn.jobstart(cfg.trash.cmd.." "..node.absolute_path, {
detach = true,
on_exit = on_exit,
})
end

local is_confirmed = true

-- confirmation prompt
if cfg.trash.require_confirm then
is_confirmed = false
print("Trash " ..node.name.. " ? y/n")
local ans = utils.get_user_input_char()
if ans:match('^y') then is_confirmed = true end
utils.clear_prompt()
end

-- trashing
if is_confirmed then
if node.entries ~= nil and not node.link_to then
trash_path(function()
events._dispatch_folder_removed(node.absolute_path)
lib.refresh_tree()
end)
else
trash_path(function()
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
lib.refresh_tree()
end)
end

end
end

return M
1 change: 1 addition & 0 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ M.View = {
{ key = "R", cb = M.nvim_tree_callback("refresh") },
{ key = "a", cb = M.nvim_tree_callback("create") },
{ key = "d", cb = M.nvim_tree_callback("remove") },
{ key = "D", cb = M.nvim_tree_callback("trash") },
{ key = "r", cb = M.nvim_tree_callback("rename") },
{ key = "<C-r>", cb = M.nvim_tree_callback("full_rename") },
{ key = "x", cb = M.nvim_tree_callback("cut") },
Expand Down