Skip to content

Commit

Permalink
Merge pull request #74 from brandoncc/confirm-deletions-again
Browse files Browse the repository at this point in the history
Add confirming of deletions again, and fix force deleting
  • Loading branch information
ThePrimeagen committed Dec 24, 2021
2 parents 9eeb8ea + 2902a1a commit d7f4e25
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ To bring up the telescope window listing your workspaces run the following
:lua require('telescope').extensions.git_worktree.git_worktrees()
-- <Enter> - switches to that worktree
-- <c-d> - deletes that worktree
-- <c-D> - force deletes that worktree
-- <c-f> - toggles forcing of the next deletion
```

### Create a worktree
Expand Down
75 changes: 49 additions & 26 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <C-f> 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
Expand Down Expand Up @@ -190,12 +215,10 @@ local telescope_git_worktree = function(opts)
attach_mappings = function(_, map)
action_set.select:replace(switch_worktree)
map("i", "<c-d>", function(prompt_bufnr)
delete_worktree(prompt_bufnr)
end)
map("n", "<c-d>", function(prompt_bufnr)
delete_worktree(prompt_bufnr)
end)
map("i", "<c-d>", delete_worktree)
map("n", "<c-d>", delete_worktree)
map("i", "<c-f>", toggle_forced_deletion)
map("n", "<c-f>", toggle_forced_deletion)
return true
end
Expand Down

0 comments on commit d7f4e25

Please sign in to comment.