Skip to content

Commit e842f08

Browse files
authored
feat: add trashing (#749)
1 parent 6488075 commit e842f08

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ require'nvim-tree'.setup {
9191
},
9292
number = false,
9393
relativenumber = false
94+
},
95+
trash = {
96+
cmd = "trash",
97+
require_confirm = true
9498
}
9599
}
96100
```
@@ -192,6 +196,7 @@ highlight NvimTreeFolderIcon guibg=blue
192196
- type `gy` will copy absolute path to system clipboard
193197
- type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation)
194198
- type `d` to delete a file (will prompt for confirmation)
199+
- type `D` to trash a file (configured in setup())
195200
- type `]c` to go to next git item
196201
- type `[c` to go to prev git item
197202
- type `-` to navigate up to the parent directory of the current file/directory
@@ -243,6 +248,7 @@ local list = {
243248
{ key = "R", cb = tree_cb("refresh") },
244249
{ key = "a", cb = tree_cb("create") },
245250
{ key = "d", cb = tree_cb("remove") },
251+
{ key = "D", cb = tree_cb("trash") },
246252
{ key = "r", cb = tree_cb("rename") },
247253
{ key = "<C-r>", cb = tree_cb("full_rename") },
248254
{ key = "x", cb = tree_cb("cut") },

doc/nvim-tree-lua.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ function.
120120
filters = {
121121
dotfiles = false,
122122
custom = {}
123+
},
124+
trash = {
125+
cmd = "trash",
126+
require_confirm = true,
123127
}
124128
}
125129
<
@@ -321,6 +325,16 @@ Here is a list of the options available in the setup call:
321325
type: `{string}`
322326
default: `{}`
323327

328+
*nvim-tree.trash*
329+
|trash|: configuration options for trashing
330+
331+
- |trash.cmd|: the command used to trash items (must be installed on your system)
332+
type: `string`
333+
default: `"trash"`
334+
335+
- |trash.require_confirm|: show a prompt before trashing takes place.
336+
type: `boolean`
337+
default: `true`
324338

325339
==============================================================================
326340
OPTIONS *nvim-tree-options*
@@ -562,6 +576,7 @@ Defaults to:
562576
{ key = "R", cb = tree_cb("refresh") },
563577
{ key = "a", cb = tree_cb("create") },
564578
{ key = "d", cb = tree_cb("remove") },
579+
{ key = "D", cb = tree_cb("trash") },
565580
{ key = "r", cb = tree_cb("rename") },
566581
{ key = "<C-r>", cb = tree_cb("full_rename") },
567582
{ key = "x", cb = tree_cb("cut") },

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local renderer = require'nvim-tree.renderer'
88
local fs = require'nvim-tree.fs'
99
local view = require'nvim-tree.view'
1010
local utils = require'nvim-tree.utils'
11+
local trash = require'nvim-tree.trash'
1112

1213
local _config = {
1314
is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1,
@@ -151,6 +152,7 @@ local keypress_funcs = {
151152
)
152153
luv.unref(process.handle)
153154
end,
155+
trash = function(node) trash.trash_node(node, _config) end,
154156
}
155157

156158
function M.on_keypress(mode)
@@ -453,6 +455,7 @@ function M.setup(conf)
453455
_config.system_open = opts.system_open
454456
_config.open_on_setup = opts.open_on_setup
455457
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
458+
_config.trash = opts.trash or {}
456459
if type(opts.update_to_buf_dir) == "boolean" then
457460
utils.warn("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
458461
_config.update_to_buf_dir = {

lua/nvim-tree/trash.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local M = {}
2+
3+
local lib = require'nvim-tree.lib'
4+
local utils = require'nvim-tree.utils'
5+
local events = require'nvim-tree.events'
6+
local api = vim.api
7+
8+
local function clear_buffer(absolute_path)
9+
local bufs = vim.fn.getbufinfo({bufloaded = 1, buflisted = 1})
10+
for _, buf in pairs(bufs) do
11+
if buf.name == absolute_path then
12+
if buf.hidden == 0 and #bufs > 1 then
13+
local winnr = api.nvim_get_current_win()
14+
api.nvim_set_current_win(buf.windows[1])
15+
vim.cmd(':bn')
16+
api.nvim_set_current_win(winnr)
17+
end
18+
vim.api.nvim_buf_delete(buf.bufnr, {})
19+
return
20+
end
21+
end
22+
end
23+
24+
function M.trash_node(node, cfg)
25+
if node.name == '..' then return end
26+
27+
-- configs
28+
if cfg.is_unix then
29+
if cfg.trash.cmd == nil then cfg.trash.cmd = 'trash' end
30+
if cfg.trash.require_confirm == nil then cfg.trash.require_confirm = true end
31+
else
32+
print('trash is currently a UNIX only feature!')
33+
end
34+
35+
-- trashes a path (file or folder)
36+
local function trash_path(on_exit)
37+
vim.fn.jobstart(cfg.trash.cmd.." "..node.absolute_path, {
38+
detach = true,
39+
on_exit = on_exit,
40+
})
41+
end
42+
43+
local is_confirmed = true
44+
45+
-- confirmation prompt
46+
if cfg.trash.require_confirm then
47+
is_confirmed = false
48+
print("Trash " ..node.name.. " ? y/n")
49+
local ans = utils.get_user_input_char()
50+
if ans:match('^y') then is_confirmed = true end
51+
utils.clear_prompt()
52+
end
53+
54+
-- trashing
55+
if is_confirmed then
56+
if node.entries ~= nil and not node.link_to then
57+
trash_path(function()
58+
events._dispatch_folder_removed(node.absolute_path)
59+
lib.refresh_tree()
60+
end)
61+
else
62+
trash_path(function()
63+
events._dispatch_file_removed(node.absolute_path)
64+
clear_buffer(node.absolute_path)
65+
lib.refresh_tree()
66+
end)
67+
end
68+
69+
end
70+
end
71+
72+
return M

lua/nvim-tree/view.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ M.View = {
6161
{ key = "R", cb = M.nvim_tree_callback("refresh") },
6262
{ key = "a", cb = M.nvim_tree_callback("create") },
6363
{ key = "d", cb = M.nvim_tree_callback("remove") },
64+
{ key = "D", cb = M.nvim_tree_callback("trash") },
6465
{ key = "r", cb = M.nvim_tree_callback("rename") },
6566
{ key = "<C-r>", cb = M.nvim_tree_callback("full_rename") },
6667
{ key = "x", cb = M.nvim_tree_callback("cut") },

0 commit comments

Comments
 (0)