Skip to content

Commit 2f51557

Browse files
committed
feat: adapt to changes in telescope api
1 parent f247308 commit 2f51557

File tree

1 file changed

+92
-88
lines changed

1 file changed

+92
-88
lines changed
Lines changed: 92 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local Path = require("plenary.path")
2-
local Window = require("plenary.window.float")
31
local strings = require("plenary.strings")
42
local pickers = require("telescope.pickers")
53
local finders = require("telescope.finders")
@@ -12,11 +10,17 @@ local git_worktree = require("git-worktree")
1210

1311
local force_next_deletion = false
1412

13+
-- Get the path of the selected worktree
14+
-- @param prompt_bufnr number: the prompt buffer number
15+
-- @return string: the path of the selected worktree
1516
local get_worktree_path = function(prompt_bufnr)
1617
local selection = action_state.get_selected_entry(prompt_bufnr)
1718
return selection.path
1819
end
1920

21+
-- Switch to the selected worktree
22+
-- @param prompt_bufnr number: the prompt buffer number
23+
-- @return nil
2024
local switch_worktree = function(prompt_bufnr)
2125
local worktree_path = get_worktree_path(prompt_bufnr)
2226
actions.close(prompt_bufnr)
@@ -25,26 +29,35 @@ local switch_worktree = function(prompt_bufnr)
2529
end
2630
end
2731

32+
-- Toggle the forced deletion of the next worktree
33+
-- @return nil
2834
local toggle_forced_deletion = function()
2935
-- redraw otherwise the message is not displayed when in insert mode
3036
if force_next_deletion then
31-
print('The next deletion will not be forced')
32-
vim.fn.execute('redraw')
37+
vim.print("The next deletion will not be forced")
38+
vim.fn.execute("redraw")
3339
else
34-
print('The next deletion will be forced')
35-
vim.fn.execute('redraw')
40+
vim.print("The next deletion will be forced")
41+
vim.fn.execute("redraw")
3642
force_next_deletion = true
3743
end
3844
end
3945

46+
-- Handler for successful deletion
47+
-- @return nil
4048
local delete_success_handler = function()
4149
force_next_deletion = false
4250
end
4351

52+
-- Handler for failed deletion
53+
-- @return nil
4454
local delete_failure_handler = function()
4555
print("Deletion failed, use <C-f> to force the next deletion")
4656
end
4757

58+
-- Ask the user to confirm the deletion of a worktree
59+
-- @param forcing boolean: whether the deletion is forced
60+
-- @return boolean: whether the deletion is confirmed
4861
local ask_to_confirm_deletion = function(forcing)
4962
if forcing then
5063
return vim.fn.input("Force deletion of worktree? [y/n]: ")
@@ -53,6 +66,9 @@ local ask_to_confirm_deletion = function(forcing)
5366
return vim.fn.input("Delete worktree? [y/n]: ")
5467
end
5568

69+
-- Confirm the deletion of a worktree
70+
-- @param forcing boolean: whether the deletion is forced
71+
-- @return boolean: whether the deletion is confirmed
5672
local confirm_deletion = function(forcing)
5773
if not git_worktree._config.confirm_telescope_deletions then
5874
return true
@@ -68,6 +84,9 @@ local confirm_deletion = function(forcing)
6884
return false
6985
end
7086

87+
-- Delete the selected worktree
88+
-- @param prompt_bufnr number: the prompt buffer number
89+
-- @return nil
7190
local delete_worktree = function(prompt_bufnr)
7291
if not confirm_deletion() then
7392
return
@@ -76,79 +95,63 @@ local delete_worktree = function(prompt_bufnr)
7695
local worktree_path = get_worktree_path(prompt_bufnr)
7796
actions.close(prompt_bufnr)
7897
if worktree_path ~= nil then
79-
git_worktree.delete_worktree(worktree_path, force_next_deletion, {
80-
on_failure = delete_failure_handler,
81-
on_success = delete_success_handler
82-
})
98+
git_worktree.delete_worktree(worktree_path, force_next_deletion, {
99+
on_failure = delete_failure_handler,
100+
on_success = delete_success_handler,
101+
})
83102
end
84103
end
85104

105+
-- Create a prompt to get the path of the new worktree
106+
-- @param cb function: the callback to call with the path
107+
-- @return nil
86108
local create_input_prompt = function(cb)
87-
88-
--[[
89-
local window = Window.centered({
90-
width = 30,
91-
height = 1
92-
})
93-
vim.api.nvim_buf_set_option(window.bufnr, "buftype", "prompt")
94-
vim.fn.prompt_setprompt(window.bufnr, "Worktree Location: ")
95-
vim.fn.prompt_setcallback(window.bufnr, function(text)
96-
vim.api.nvim_win_close(window.win_id, true)
97-
vim.api.nvim_buf_delete(window.bufnr, {force = true})
98-
cb(text)
99-
end)
100-
101-
vim.api.nvim_set_current_win(window.win_id)
102-
vim.fn.schedule(function()
103-
vim.nvim_command("startinsert")
104-
end)
105-
--]]
106-
--
107-
108109
local subtree = vim.fn.input("Path to subtree > ")
109110
cb(subtree)
110111
end
111112

113+
-- Create a worktree
114+
-- @param opts table: the options for the telescope picker (optional)
115+
-- @return nil
112116
local create_worktree = function(opts)
113117
opts = opts or {}
114118
opts.attach_mappings = function()
115-
actions.select_default:replace(
116-
function(prompt_bufnr, _)
117-
local selected_entry = action_state.get_selected_entry()
118-
local current_line = action_state.get_current_line()
119+
actions.select_default:replace(function(prompt_bufnr, _)
120+
local selected_entry = action_state.get_selected_entry()
121+
local current_line = action_state.get_current_line()
119122

120-
actions.close(prompt_bufnr)
123+
actions.close(prompt_bufnr)
121124

122-
local branch = selected_entry ~= nil and
123-
selected_entry.value or current_line
125+
local branch = selected_entry ~= nil and selected_entry.value or current_line
124126

125-
if branch == nil then
126-
return
127-
end
127+
if branch == nil then
128+
return
129+
end
128130

129-
create_input_prompt(function(name)
130-
if name == "" then
131-
name = branch
132-
end
133-
git_worktree.create_worktree(name, branch)
134-
end)
131+
create_input_prompt(function(name)
132+
if name == "" then
133+
name = branch
134+
end
135+
git_worktree.create_worktree(name, branch)
135136
end)
136-
137-
-- do we need to replace other default maps?
137+
end)
138138

139139
return true
140140
end
141141
require("telescope.builtin").git_branches(opts)
142142
end
143143

144+
-- List the git worktrees
145+
-- @param opts table: the options for the telescope picker (optional)
146+
-- @return nil
144147
local telescope_git_worktree = function(opts)
145148
opts = opts or {}
146-
local output = utils.get_os_command_output({"git", "worktree", "list"})
149+
local output = utils.get_os_command_output({ "git", "worktree", "list" })
147150
local results = {}
148151
local widths = {
149152
path = 0,
150153
sha = 0,
151-
branch = 0
154+
branch = 0,
152155
}
153156

154157
local parse_line = function(line)
@@ -162,9 +165,8 @@ local telescope_git_worktree = function(opts)
162165
if entry.sha ~= "(bare)" then
163166
local index = #results + 1
164167
for key, val in pairs(widths) do
165-
if key == 'path' then
166-
local new_path = utils.transform_path(opts, entry[key])
167-
local path_len = strings.strdisplaywidth(new_path or "")
168+
if key == "path" then
169+
local path_len = strings.strdisplaywidth(entry[key] or "")
168170
widths[key] = math.max(val, path_len)
169171
else
170172
widths[key] = math.max(val, strings.strdisplaywidth(entry[key] or ""))
@@ -183,53 +185,55 @@ local telescope_git_worktree = function(opts)
183185
return
184186
end
185187

186-
local displayer = require("telescope.pickers.entry_display").create {
188+
local displayer = require("telescope.pickers.entry_display").create({
187189
separator = " ",
188190
items = {
189191
{ width = widths.branch },
190192
{ width = widths.path },
191193
{ width = widths.sha },
192194
},
193-
}
195+
})
194196

195197
local make_display = function(entry)
196-
return displayer {
198+
return displayer({
197199
{ entry.branch, "TelescopeResultsIdentifier" },
198200
{ utils.transform_path(opts, entry.path) },
199201
{ entry.sha },
200-
}
202+
})
201203
end
202204

203-
pickers.new(opts or {}, {
204-
prompt_title = "Git Worktrees",
205-
finder = finders.new_table {
206-
results = results,
207-
entry_maker = function(entry)
208-
entry.value = entry.branch
209-
entry.ordinal = entry.branch
210-
entry.display = make_display
211-
return entry
205+
pickers
206+
.new(opts or {}, {
207+
prompt_title = "Git Worktrees",
208+
finder = finders.new_table({
209+
results = results,
210+
entry_maker = function(entry)
211+
entry.value = entry.branch
212+
entry.ordinal = entry.branch
213+
entry.display = make_display
214+
return entry
215+
end,
216+
}),
217+
sorter = conf.generic_sorter(opts),
218+
attach_mappings = function(_, map)
219+
action_set.select:replace(switch_worktree)
220+
221+
map("i", "<c-d>", delete_worktree)
222+
map("n", "<c-d>", delete_worktree)
223+
map("i", "<c-f>", toggle_forced_deletion)
224+
map("n", "<c-f>", toggle_forced_deletion)
225+
226+
return true
212227
end,
213-
},
214-
sorter = conf.generic_sorter(opts),
215-
attach_mappings = function(_, map)
216-
action_set.select:replace(switch_worktree)
217-
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)
222-
223-
return true
224-
end
225-
}):find()
228+
})
229+
:find()
226230
end
227231

228-
return require("telescope").register_extension(
229-
{
230-
exports = {
231-
git_worktree = telescope_git_worktree,
232-
git_worktrees = telescope_git_worktree,
233-
create_git_worktree = create_worktree
234-
}
235-
})
232+
-- Register the extension
233+
-- @return table: the extension
234+
return require("telescope").register_extension({
235+
exports = {
236+
git_worktrees = telescope_git_worktree,
237+
create_git_worktree = create_worktree,
238+
},
239+
})

0 commit comments

Comments
 (0)