From 8350756f8398a5213fd9e6a039183786ccfe93f8 Mon Sep 17 00:00:00 2001 From: Brandon Conway Date: Fri, 24 Dec 2021 10:55:27 -0800 Subject: [PATCH 1/2] Add deletion confirmation in telescope extension --- lua/telescope/_extensions/git_worktree.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index 0e795b1..9e2ee6f 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -48,7 +48,28 @@ local create_delete_failure_handler = function(prompt_bufnr) end end +local confirm_deletion = function() + if not git_worktree._config.confirm_telescope_deletions then + return true + end + + local confirmation = vim.fn.input( + string.format("Delete worktree? [y/n]: ") + ) + + if string.sub(string.lower(confirmation), 0, 1) == "y" then + return true + end + + print("Didn't delete worktree") + return false +end + delete_worktree = function(prompt_bufnr, force) + if not confirm_deletion() then + return + end + local worktree_path = get_worktree_path(prompt_bufnr) actions.close(prompt_bufnr) if worktree_path ~= nil then From 2902a1a002967b55da6e8e4abc29645d02745abd Mon Sep 17 00:00:00 2001 From: Brandon Conway Date: Fri, 24 Dec 2021 11:53:36 -0800 Subject: [PATCH 2/2] Allow user to force the next deletion --- README.md | 2 +- lua/telescope/_extensions/git_worktree.lua | 66 +++++++++++----------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 803117b..a1b8ed2 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ To bring up the telescope window listing your workspaces run the following :lua require('telescope').extensions.git_worktree.git_worktrees() -- - switches to that worktree -- - deletes that worktree --- - force deletes that worktree +-- - toggles forcing of the next deletion ``` ### Create a worktree diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index 9e2ee6f..905a358 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -10,6 +10,8 @@ local action_state = require("telescope.actions.state") local conf = require("telescope.config").values local git_worktree = require("git-worktree") +local force_next_deletion = false + local get_worktree_path = function(prompt_bufnr) local selection = action_state.get_selected_entry(prompt_bufnr) return selection.path @@ -23,41 +25,42 @@ local switch_worktree = function(prompt_bufnr) end end -local offer_forced_deletion = function() - local confirmation = vim.fn.input( - string.format("Deletion failed, would you like to force delete? [y/n]: ") - ) - - if string.sub(string.lower(confirmation), 0, 1) == "y" then - return true - end +local toggle_forced_deletion = function() + -- redraw otherwise the message is not displayed when in insert mode + if force_next_deletion then + print('The next deletion will not be forced') + vim.fn.execute('redraw') + else + print('The next deletion will be forced') + vim.fn.execute('redraw') + force_next_deletion = true + end +end - return false +local delete_success_handler = function() + force_next_deletion = false end --- create_delete_failure_handler and delete_worktree need access to each other --- so delete_worktree is initialized above create_delete_failure_handler -local delete_worktree +local delete_failure_handler = function() + print("Deletion failed, use to force the next deletion") +end --- TODO WIP -local create_delete_failure_handler = function(prompt_bufnr) - return function(err) - if offer_forced_deletion() then - delete_worktree(prompt_bufnr, true) - end +local ask_to_confirm_deletion = function(forcing) + if forcing then + return vim.fn.input("Force deletion of worktree? [y/n]: ") end + + return vim.fn.input("Delete worktree? [y/n]: ") end -local confirm_deletion = function() +local confirm_deletion = function(forcing) if not git_worktree._config.confirm_telescope_deletions then return true end - local confirmation = vim.fn.input( - string.format("Delete worktree? [y/n]: ") - ) + local confirmed = ask_to_confirm_deletion(forcing) - if string.sub(string.lower(confirmation), 0, 1) == "y" then + if string.sub(string.lower(confirmed), 0, 1) == "y" then return true end @@ -65,7 +68,7 @@ local confirm_deletion = function() return false end -delete_worktree = function(prompt_bufnr, force) +local delete_worktree = function(prompt_bufnr) if not confirm_deletion() then return end @@ -73,8 +76,9 @@ delete_worktree = function(prompt_bufnr, force) local worktree_path = get_worktree_path(prompt_bufnr) actions.close(prompt_bufnr) if worktree_path ~= nil then - git_worktree.delete_worktree(worktree_path, force, { - -- on_failure = create_delete_failure_handler(prompt_bufnr), + git_worktree.delete_worktree(worktree_path, force_next_deletion, { + on_failure = delete_failure_handler, + on_success = delete_success_handler }) end end @@ -211,12 +215,10 @@ local telescope_git_worktree = function(opts) attach_mappings = function(_, map) action_set.select:replace(switch_worktree) - map("i", "", function(prompt_bufnr) - delete_worktree(prompt_bufnr) - end) - map("n", "", function(prompt_bufnr) - delete_worktree(prompt_bufnr) - end) + map("i", "", delete_worktree) + map("n", "", delete_worktree) + map("i", "", toggle_forced_deletion) + map("n", "", toggle_forced_deletion) return true end