Skip to content

Commit 2902a1a

Browse files
committed
Allow user to force the next deletion
1 parent 8350756 commit 2902a1a

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
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: 34 additions & 32 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,58 +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-
)
30-
31-
if string.sub(string.lower(confirmation), 0, 1) == "y" then
32-
return true
33-
end
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
3439

35-
return false
40+
local delete_success_handler = function()
41+
force_next_deletion = false
3642
end
3743

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
44+
local delete_failure_handler = function()
45+
print("Deletion failed, use <C-f> to force the next deletion")
46+
end
4147

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
48+
local ask_to_confirm_deletion = function(forcing)
49+
if forcing then
50+
return vim.fn.input("Force deletion of worktree? [y/n]: ")
4851
end
52+
53+
return vim.fn.input("Delete worktree? [y/n]: ")
4954
end
5055

51-
local confirm_deletion = function()
56+
local confirm_deletion = function(forcing)
5257
if not git_worktree._config.confirm_telescope_deletions then
5358
return true
5459
end
5560

56-
local confirmation = vim.fn.input(
57-
string.format("Delete worktree? [y/n]: ")
58-
)
61+
local confirmed = ask_to_confirm_deletion(forcing)
5962

60-
if string.sub(string.lower(confirmation), 0, 1) == "y" then
63+
if string.sub(string.lower(confirmed), 0, 1) == "y" then
6164
return true
6265
end
6366

6467
print("Didn't delete worktree")
6568
return false
6669
end
6770

68-
delete_worktree = function(prompt_bufnr, force)
71+
local delete_worktree = function(prompt_bufnr)
6972
if not confirm_deletion() then
7073
return
7174
end
7275

7376
local worktree_path = get_worktree_path(prompt_bufnr)
7477
actions.close(prompt_bufnr)
7578
if worktree_path ~= nil then
76-
git_worktree.delete_worktree(worktree_path, force, {
77-
-- 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
7882
})
7983
end
8084
end
@@ -211,12 +215,10 @@ local telescope_git_worktree = function(opts)
211215
attach_mappings = function(_, map)
212216
action_set.select:replace(switch_worktree)
213217

214-
map("i", "<c-d>", function(prompt_bufnr)
215-
delete_worktree(prompt_bufnr)
216-
end)
217-
map("n", "<c-d>", function(prompt_bufnr)
218-
delete_worktree(prompt_bufnr)
219-
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)
220222

221223
return true
222224
end

0 commit comments

Comments
 (0)