Skip to content

Commit d7f4e25

Browse files
authored
Merge pull request ThePrimeagen#74 from brandoncc/confirm-deletions-again
Add confirming of deletions again, and fix force deleting
2 parents 9eeb8ea + 2902a1a commit d7f4e25

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ To bring up the telescope window listing your workspaces run the following
132132
:lua require('telescope').extensions.git_worktree.git_worktrees()
133133
-- <Enter> - switches to that worktree
134134
-- <c-d> - deletes that worktree
135-
-- <c-D> - force deletes that worktree
135+
-- <c-f> - toggles forcing of the next deletion
136136
```
137137

138138
### Create a worktree

lua/telescope/_extensions/git_worktree.lua

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ local action_state = require("telescope.actions.state")
1010
local conf = require("telescope.config").values
1111
local git_worktree = require("git-worktree")
1212

13+
local force_next_deletion = false
14+
1315
local get_worktree_path = function(prompt_bufnr)
1416
local selection = action_state.get_selected_entry(prompt_bufnr)
1517
return selection.path
@@ -23,37 +25,60 @@ local switch_worktree = function(prompt_bufnr)
2325
end
2426
end
2527

26-
local offer_forced_deletion = function()
27-
local confirmation = vim.fn.input(
28-
string.format("Deletion failed, would you like to force delete? [y/n]: ")
29-
)
28+
local toggle_forced_deletion = function()
29+
-- redraw otherwise the message is not displayed when in insert mode
30+
if force_next_deletion then
31+
print('The next deletion will not be forced')
32+
vim.fn.execute('redraw')
33+
else
34+
print('The next deletion will be forced')
35+
vim.fn.execute('redraw')
36+
force_next_deletion = true
37+
end
38+
end
3039

31-
if string.sub(string.lower(confirmation), 0, 1) == "y" then
32-
return true
33-
end
40+
local delete_success_handler = function()
41+
force_next_deletion = false
42+
end
3443

35-
return false
44+
local delete_failure_handler = function()
45+
print("Deletion failed, use <C-f> to force the next deletion")
3646
end
3747

38-
-- create_delete_failure_handler and delete_worktree need access to each other
39-
-- so delete_worktree is initialized above create_delete_failure_handler
40-
local delete_worktree
48+
local ask_to_confirm_deletion = function(forcing)
49+
if forcing then
50+
return vim.fn.input("Force deletion of worktree? [y/n]: ")
51+
end
52+
53+
return vim.fn.input("Delete worktree? [y/n]: ")
54+
end
4155

42-
-- TODO WIP
43-
local create_delete_failure_handler = function(prompt_bufnr)
44-
return function(err)
45-
if offer_forced_deletion() then
46-
delete_worktree(prompt_bufnr, true)
47-
end
56+
local confirm_deletion = function(forcing)
57+
if not git_worktree._config.confirm_telescope_deletions then
58+
return true
4859
end
60+
61+
local confirmed = ask_to_confirm_deletion(forcing)
62+
63+
if string.sub(string.lower(confirmed), 0, 1) == "y" then
64+
return true
65+
end
66+
67+
print("Didn't delete worktree")
68+
return false
4969
end
5070

51-
delete_worktree = function(prompt_bufnr, force)
71+
local delete_worktree = function(prompt_bufnr)
72+
if not confirm_deletion() then
73+
return
74+
end
75+
5276
local worktree_path = get_worktree_path(prompt_bufnr)
5377
actions.close(prompt_bufnr)
5478
if worktree_path ~= nil then
55-
git_worktree.delete_worktree(worktree_path, force, {
56-
-- on_failure = create_delete_failure_handler(prompt_bufnr),
79+
git_worktree.delete_worktree(worktree_path, force_next_deletion, {
80+
on_failure = delete_failure_handler,
81+
on_success = delete_success_handler
5782
})
5883
end
5984
end
@@ -190,12 +215,10 @@ local telescope_git_worktree = function(opts)
190215
attach_mappings = function(_, map)
191216
action_set.select:replace(switch_worktree)
192217

193-
map("i", "<c-d>", function(prompt_bufnr)
194-
delete_worktree(prompt_bufnr)
195-
end)
196-
map("n", "<c-d>", function(prompt_bufnr)
197-
delete_worktree(prompt_bufnr)
198-
end)
218+
map("i", "<c-d>", delete_worktree)
219+
map("n", "<c-d>", delete_worktree)
220+
map("i", "<c-f>", toggle_forced_deletion)
221+
map("n", "<c-f>", toggle_forced_deletion)
199222

200223
return true
201224
end

0 commit comments

Comments
 (0)