Skip to content

Require deletion prompt #1908

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
Jan 9, 2023
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
39 changes: 34 additions & 5 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ Subsequent calls to setup will replace the previous configuration.
},
trash = {
cmd = "gio trash",
require_confirm = true,
},
live_filter = {
prefix = "[FILTER]: ",
Expand All @@ -390,6 +389,12 @@ Subsequent calls to setup will replace the previous configuration.
notify = {
threshold = vim.log.levels.INFO,
},
ui = {
confirm = {
remove = true,
trash = true,
},
},
log = {
enable = false,
truncate = false,
Expand Down Expand Up @@ -993,10 +998,6 @@ Configuration options for trashing.
The default is shipped with glib2 which is a common linux package.
Type: `string`, Default: `"gio trash"`

*nvim-tree.trash.require_confirm*
Show a prompt before trashing takes place.
Type: `boolean`, Default: `true`

*nvim-tree.actions*
Configuration for various actions.

Expand Down Expand Up @@ -1146,6 +1147,20 @@ Configuration for notification.
`INFO:` information only e.g. file copy path confirmation.
`DEBUG:` not used.

*nvim-tree.ui*
General UI configuration.

*nvim-tree.ui.confirm*
Confirmation prompts.

*nvim-tree.ui.confirm.remove*
Prompt before removing.
Type: `boolean`, Default: `true`

*nvim-tree.ui.confirm.trash* (previously `trash.require_confirm`)
Prompt before trashing.
Type: `boolean`, Default: `true`

*nvim-tree.log*
Configuration for diagnostic logging.

Expand Down Expand Up @@ -1335,6 +1350,20 @@ exists.
- navigate.prev
- navigate.select

api.config.mappings.active() *nvim-tree.api.config.mappings.active()*
Retrieve a clone of the currently active mappings:
|nvim-tree-default-mappings| with |nvim-tree.view.mappings| applied.
Changing the active mappings will require a call to |nvim-tree-setup|

Return: ~
(table) as per |nvim-tree.view.mappings.list|

api.config.mappings.default() *nvim-tree.api.config.mappings.default()*
Retrieve a clone of the default mappings: |nvim-tree-default-mappings|

Return: ~
(table) as per |nvim-tree.view.mappings.list|

==============================================================================
6. MAPPINGS *nvim-tree-mappings*

Expand Down
7 changes: 6 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
trash = {
cmd = "gio trash",
require_confirm = true,
},
live_filter = {
prefix = "[FILTER]: ",
Expand All @@ -714,6 +713,12 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
notify = {
threshold = vim.log.levels.INFO,
},
ui = {
confirm = {
remove = true,
trash = true,
},
},
log = {
enable = false,
truncate = false,
Expand Down
55 changes: 33 additions & 22 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,45 @@ function M.fn(node)
if node.name == ".." then
return
end
local prompt_select = "Remove " .. node.name .. " ?"
local prompt_input = prompt_select .. " y/n: "
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
utils.clear_prompt()
if item_short == "y" then
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
end
events._dispatch_folder_removed(node.absolute_path)
else
local success = vim.loop.fs_unlink(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)

local function do_remove()
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
end
notify.info(node.absolute_path .. " was properly removed.")
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
events._dispatch_folder_removed(node.absolute_path)
else
local success = vim.loop.fs_unlink(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
end)
notify.info(node.absolute_path .. " was properly removed.")
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end

if M.config.ui.confirm.remove then
local prompt_select = "Remove " .. node.name .. " ?"
local prompt_input = prompt_select .. " y/n: "
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
utils.clear_prompt()
if item_short == "y" then
do_remove()
end
end)
else
do_remove()
end
end

function M.setup(opts)
M.config = {}
M.config.ui = opts.ui or {}
M.enable_reload = not opts.filesystem_watchers.enable
M.close_window = opts.actions.remove_file.close_window
end
Expand Down
7 changes: 4 additions & 3 deletions lua/nvim-tree/actions/fs/trash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function M.fn(node)
if M.config.trash.cmd == nil then
M.config.trash.cmd = "trash"
end
if M.config.trash.require_confirm == nil then
M.config.trash.require_confirm = true
if M.config.ui.confirm.trash == nil then
M.config.ui.confirm.trash = true
end
else
notify.warn "Trash is currently a UNIX only feature!"
Expand Down Expand Up @@ -87,7 +87,7 @@ function M.fn(node)
end
end

if M.config.trash.require_confirm then
if M.config.ui.confirm.trash then
local prompt_select = "Trash " .. node.name .. " ?"
local prompt_input = prompt_select .. " y/n: "
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
Expand All @@ -103,6 +103,7 @@ end

function M.setup(opts)
M.config = {}
M.config.ui = opts.ui or {}
M.config.trash = opts.trash or {}
M.enable_reload = not opts.filesystem_watchers.enable
end
Expand Down
12 changes: 12 additions & 0 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ local DEFAULT_MAPPING_CONFIG = {
list = {},
}

--- clone default for the user
--- @return table
function M.default_mappings_clone()
return vim.deepcopy(DEFAULT_MAPPINGS)
end

--- clone active for the user
--- @return table
function M.active_mappings_clone()
return vim.deepcopy(M.mappings)
end

function M.setup(opts)
require("nvim-tree.actions.fs.trash").setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
Expand Down
4 changes: 4 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Api = {
fs = { copy = {} },
git = {},
live_filter = {},
config = { mappings = {} },
}

local function inject_node(f)
Expand Down Expand Up @@ -127,4 +128,7 @@ Api.marks.navigate.next = require("nvim-tree.marks.navigation").next
Api.marks.navigate.prev = require("nvim-tree.marks.navigation").prev
Api.marks.navigate.select = require("nvim-tree.marks.navigation").select

Api.config.mappings.active = require("nvim-tree.actions").active_mappings_clone
Api.config.mappings.default = require("nvim-tree.actions").default_mappings_clone

return Api
3 changes: 3 additions & 0 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ local function refactored(opts)

-- 2023/01/01
utils.move_missing_val(opts, "update_focused_file", "debounce_delay", opts, "view", "debounce_delay", true)

-- 2023/01/08
utils.move_missing_val(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true)
end

local function removed(opts)
Expand Down
14 changes: 10 additions & 4 deletions lua/nvim-tree/modified.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ function M.reload()
local bufs = vim.fn.getbufinfo { bufmodified = true, buflisted = true }
for _, buf in pairs(bufs) do
local path = buf.name
M._record[path] = true
while path ~= vim.fn.getcwd() and path ~= "/" do
path = vim.fn.fnamemodify(path, ":h")
M._record[path] = true
if path ~= "" then -- not a [No Name] buffer
-- mark all the parent as modified as well
while
M._record[path] ~= true
-- no need to keep going if already recorded
-- This also prevents an infinite loop
do
M._record[path] = true
path = vim.fn.fnamemodify(path, ":h")
end
end
end
end
Expand Down