From 87f0986b4bd4d94e13a8bb0c9ba72f090bd64c05 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Tue, 26 Oct 2021 17:20:40 +0300 Subject: [PATCH 1/9] feat: add trashing --- README.md | 12 +++++++++++- doc/nvim-tree-lua.txt | 1 + lua/nvim-tree.lua | 31 +++++++++++++++++++++++++++++++ lua/nvim-tree/view.lua | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2fba538e2b..c44868ee8dd 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,15 @@ require'nvim-tree'.setup { -- list of mappings to set on the tree manually list = {} } - } + }, + + -- trashing is currently a UNIX only feature + trash = { + -- the command used to trash files, "trash" is the default which needs trash-cli to be installed + cmd = "trash", + -- if false trashing won't require y/n confirmation + require_confirm = true + }, } ``` @@ -209,6 +217,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 @@ -260,6 +269,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 = "", cb = tree_cb("full_rename") }, { key = "x", cb = tree_cb("cut") }, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 99b2751b4e8..ccf009fb43e 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -528,6 +528,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 = "", cb = tree_cb("full_rename") }, { key = "x", cb = tree_cb("cut") }, diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 89a591f29ff..f9f308ae783 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -151,6 +151,36 @@ local keypress_funcs = { ) luv.unref(process.handle) end, + trash = function(node) + if _config.is_unix then + if _config.trash.cmd == nil then _config.trash.cmd = 'trash' end + if _config.trash.require_confirm == nil then _config.trash.require_confirm = true end + else + print('trash is currently a UNIX only feature!') + end + + local function get_user_input_char() + local c = vim.fn.getchar() + return vim.fn.nr2char(c) + end + + if (node) then + local is_confirmed = true + if _config.trash.require_confirm then + is_confirmed = false + print("Trash "..node.name.." ? y/n") + if get_user_input_char():match('^y') then is_confirmed = true end + end + + if is_confirmed then + vim.fn.jobstart(_config.trash.cmd.." "..node.absolute_path, { + detach = true, + on_exit = function(_job_id, _data, _event) lib.refresh_tree() end, + }) + end + end + + end, } function M.on_keypress(mode) @@ -446,6 +476,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.echo_warning("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir") _config.update_to_buf_dir = { diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index a5ae1e1c308..f9edb579dcf 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -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 = "", cb = M.nvim_tree_callback("full_rename") }, { key = "x", cb = M.nvim_tree_callback("cut") }, From 58dcd9d7528d9428fef326040459c74675c69b34 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sun, 31 Oct 2021 19:16:01 +0300 Subject: [PATCH 2/9] chore: relocated trash_node() into trash.lua --- lua/nvim-tree.lua | 32 ++---------------- lua/nvim-tree/trash.lua | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 lua/nvim-tree/trash.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index f9f308ae783..a46ada7a5e1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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, @@ -151,36 +152,7 @@ local keypress_funcs = { ) luv.unref(process.handle) end, - trash = function(node) - if _config.is_unix then - if _config.trash.cmd == nil then _config.trash.cmd = 'trash' end - if _config.trash.require_confirm == nil then _config.trash.require_confirm = true end - else - print('trash is currently a UNIX only feature!') - end - - local function get_user_input_char() - local c = vim.fn.getchar() - return vim.fn.nr2char(c) - end - - if (node) then - local is_confirmed = true - if _config.trash.require_confirm then - is_confirmed = false - print("Trash "..node.name.." ? y/n") - if get_user_input_char():match('^y') then is_confirmed = true end - end - - if is_confirmed then - vim.fn.jobstart(_config.trash.cmd.." "..node.absolute_path, { - detach = true, - on_exit = function(_job_id, _data, _event) lib.refresh_tree() end, - }) - end - end - - end, + trash = function(node) trash.trash_node(node, _config) end, } function M.on_keypress(mode) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua new file mode 100644 index 00000000000..d153723387e --- /dev/null +++ b/lua/nvim-tree/trash.lua @@ -0,0 +1,73 @@ +local M = {} + +local lib = require'nvim-tree.lib' +local utils = require'nvim-tree.utils' +local events = require'nvim-tree.events' +local fs = require'nvim-tree.fs' +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(_job_id, _data, _event) + events._dispatch_folder_removed(node.absolute_path) + lib.refresh_tree() + end) + else + trash_path(function(_job_id, _data, _event) + events._dispatch_file_removed(node.absolute_path) + clear_buffer(node.absolute_path) + lib.refresh_tree() + end) + end + + end +end + +return M From f573d0fe74fb0095dea9f758c12eaa3787c79017 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sat, 27 Nov 2021 18:16:33 +0300 Subject: [PATCH 3/9] chore: linting --- lua/nvim-tree/trash.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua index d153723387e..b977e3eae0e 100644 --- a/lua/nvim-tree/trash.lua +++ b/lua/nvim-tree/trash.lua @@ -3,7 +3,6 @@ local M = {} local lib = require'nvim-tree.lib' local utils = require'nvim-tree.utils' local events = require'nvim-tree.events' -local fs = require'nvim-tree.fs' local api = vim.api local function clear_buffer(absolute_path) From 991fa96f30c5849d003f41624a78ea901ae67a9e Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sat, 27 Nov 2021 18:18:58 +0300 Subject: [PATCH 4/9] fix: on_exit callback --- lua/nvim-tree/trash.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua index b977e3eae0e..d89cc1de5cf 100644 --- a/lua/nvim-tree/trash.lua +++ b/lua/nvim-tree/trash.lua @@ -36,7 +36,7 @@ function M.trash_node(node, cfg) local function trash_path(on_exit) vim.fn.jobstart(cfg.trash.cmd.." "..node.absolute_path, { detach = true, - on_exit = on_exit(), + on_exit = on_exit, }) end From 543c7ea0057117b3fd1d9fefa7757aae3e6058a4 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sat, 27 Nov 2021 18:38:54 +0300 Subject: [PATCH 5/9] docs: add example trash config in help file --- doc/nvim-tree-lua.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index a9e466d255b..a768dc5ad29 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -114,7 +114,11 @@ function. filters = { dotfiles = false, custom = {} - } + }, + trash = { + cmd = "trash", + require_confirm = true, + }, } < From 53c064a558501e7058490dd9407b4f91601a2764 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sat, 27 Nov 2021 23:16:23 +0300 Subject: [PATCH 6/9] docs: add proper trash options description --- doc/nvim-tree-lua.txt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index a768dc5ad29..bf6254089e9 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -116,8 +116,8 @@ function. custom = {} }, trash = { - cmd = "trash", - require_confirm = true, + cmd = "trash", + require_confirm = true, }, } < @@ -284,6 +284,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* From 7e14e81130d8ac0095296988b1780ada4b974c9b Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sun, 28 Nov 2021 16:20:01 +0300 Subject: [PATCH 7/9] chore: linting --- lua/nvim-tree/trash.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua index d89cc1de5cf..58a20b766aa 100644 --- a/lua/nvim-tree/trash.lua +++ b/lua/nvim-tree/trash.lua @@ -54,12 +54,12 @@ function M.trash_node(node, cfg) -- trashing if is_confirmed then if node.entries ~= nil and not node.link_to then - trash_path(function(_job_id, _data, _event) + trash_path(function(_, _, _) events._dispatch_folder_removed(node.absolute_path) lib.refresh_tree() end) else - trash_path(function(_job_id, _data, _event) + trash_path(function(_, _, _) events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) lib.refresh_tree() From 068cc0018edf7c48f239e8345b45ee04335283e6 Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sun, 28 Nov 2021 16:29:44 +0300 Subject: [PATCH 8/9] chore: linting Co-authored-by: Kiyan --- lua/nvim-tree/trash.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua index 58a20b766aa..f15b6be2084 100644 --- a/lua/nvim-tree/trash.lua +++ b/lua/nvim-tree/trash.lua @@ -59,7 +59,7 @@ function M.trash_node(node, cfg) lib.refresh_tree() end) else - trash_path(function(_, _, _) + trash_path(function() events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) lib.refresh_tree() From d2e0552e20b710a8af109ef224ac6722abd1516b Mon Sep 17 00:00:00 2001 From: Raafat Turki Date: Sun, 28 Nov 2021 16:38:22 +0300 Subject: [PATCH 9/9] chore: linting --- lua/nvim-tree/trash.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua index f15b6be2084..78c5910c320 100644 --- a/lua/nvim-tree/trash.lua +++ b/lua/nvim-tree/trash.lua @@ -54,7 +54,7 @@ function M.trash_node(node, cfg) -- trashing if is_confirmed then if node.entries ~= nil and not node.link_to then - trash_path(function(_, _, _) + trash_path(function() events._dispatch_folder_removed(node.absolute_path) lib.refresh_tree() end)