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 0e795b1..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,37 +25,60 @@ 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]: ") - ) +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 - if string.sub(string.lower(confirmation), 0, 1) == "y" then - return true - end +local delete_success_handler = function() + force_next_deletion = false +end - return false +local delete_failure_handler = function() + print("Deletion failed, use to force the next deletion") 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 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 --- 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 confirm_deletion = function(forcing) + if not git_worktree._config.confirm_telescope_deletions then + return true end + + local confirmed = ask_to_confirm_deletion(forcing) + + if string.sub(string.lower(confirmed), 0, 1) == "y" then + return true + end + + print("Didn't delete worktree") + return false end -delete_worktree = function(prompt_bufnr, force) +local delete_worktree = function(prompt_bufnr) + if not confirm_deletion() then + return + end + 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 @@ -190,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