feat: add trashing (#749)

This commit is contained in:
Raafat Turki 2021-11-28 17:26:24 +03:00 committed by GitHub
parent 6488075d6a
commit e842f08884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 0 deletions

View File

@ -91,6 +91,10 @@ require'nvim-tree'.setup {
}, },
number = false, number = false,
relativenumber = false relativenumber = false
},
trash = {
cmd = "trash",
require_confirm = true
} }
} }
``` ```
@ -192,6 +196,7 @@ highlight NvimTreeFolderIcon guibg=blue
- type `gy` will copy absolute path to system clipboard - 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 `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 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 next git item
- type `[c` to go to prev git item - type `[c` to go to prev git item
- type `-` to navigate up to the parent directory of the current file/directory - type `-` to navigate up to the parent directory of the current file/directory
@ -243,6 +248,7 @@ local list = {
{ key = "R", cb = tree_cb("refresh") }, { key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") }, { key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") }, { key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") }, { key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") }, { key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") }, { key = "x", cb = tree_cb("cut") },

View File

@ -120,6 +120,10 @@ function.
filters = { filters = {
dotfiles = false, dotfiles = false,
custom = {} custom = {}
},
trash = {
cmd = "trash",
require_confirm = true,
} }
} }
< <
@ -321,6 +325,16 @@ Here is a list of the options available in the setup call:
type: `{string}` type: `{string}`
default: `{}` 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* OPTIONS *nvim-tree-options*
@ -562,6 +576,7 @@ Defaults to:
{ key = "R", cb = tree_cb("refresh") }, { key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") }, { key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") }, { key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") }, { key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") }, { key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") }, { key = "x", cb = tree_cb("cut") },

View File

@ -8,6 +8,7 @@ local renderer = require'nvim-tree.renderer'
local fs = require'nvim-tree.fs' local fs = require'nvim-tree.fs'
local view = require'nvim-tree.view' local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils' local utils = require'nvim-tree.utils'
local trash = require'nvim-tree.trash'
local _config = { local _config = {
is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1, is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1,
@ -151,6 +152,7 @@ local keypress_funcs = {
) )
luv.unref(process.handle) luv.unref(process.handle)
end, end,
trash = function(node) trash.trash_node(node, _config) end,
} }
function M.on_keypress(mode) function M.on_keypress(mode)
@ -453,6 +455,7 @@ function M.setup(conf)
_config.system_open = opts.system_open _config.system_open = opts.system_open
_config.open_on_setup = opts.open_on_setup _config.open_on_setup = opts.open_on_setup
_config.ignore_ft_on_setup = opts.ignore_ft_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 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") utils.warn("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
_config.update_to_buf_dir = { _config.update_to_buf_dir = {

72
lua/nvim-tree/trash.lua Normal file
View File

@ -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

View File

@ -61,6 +61,7 @@ M.View = {
{ key = "R", cb = M.nvim_tree_callback("refresh") }, { key = "R", cb = M.nvim_tree_callback("refresh") },
{ key = "a", cb = M.nvim_tree_callback("create") }, { key = "a", cb = M.nvim_tree_callback("create") },
{ key = "d", cb = M.nvim_tree_callback("remove") }, { 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 = "r", cb = M.nvim_tree_callback("rename") },
{ key = "<C-r>", cb = M.nvim_tree_callback("full_rename") }, { key = "<C-r>", cb = M.nvim_tree_callback("full_rename") },
{ key = "x", cb = M.nvim_tree_callback("cut") }, { key = "x", cb = M.nvim_tree_callback("cut") },