@@ -12,6 +12,8 @@ local git_worktree = require("git-worktree")
1212
1313local force_next_deletion = false
1414
15+ local wt_actions = {}
16+
1517local get_worktree_path = function (prompt_bufnr )
1618 local selection = action_state .get_selected_entry (prompt_bufnr )
1719 return selection .path
@@ -25,7 +27,7 @@ local switch_worktree = function(prompt_bufnr)
2527 end
2628end
2729
28- local toggle_forced_deletion = function ()
30+ wt_actions . toggle_forced_deletion = function ()
2931 -- redraw otherwise the message is not displayed when in insert mode
3032 if force_next_deletion then
3133 print (' The next deletion will not be forced' )
@@ -68,7 +70,7 @@ local confirm_deletion = function(forcing)
6870 return false
6971end
7072
71- local delete_worktree = function (prompt_bufnr )
73+ wt_actions . delete_worktree = function (prompt_bufnr )
7274 if not confirm_deletion () then
7375 return
7476 end
@@ -109,8 +111,58 @@ local create_input_prompt = function(cb)
109111 cb (subtree )
110112end
111113
114+ local pconf = {
115+ mappings = {
116+ [" i" ] = {
117+ [" <C-d>" ] = wt_actions .delete_worktree ,
118+ [" <C-f>" ] = wt_actions .toggle_forced_deletion ,
119+ },
120+ [" n" ] = {
121+ [" <C-d>" ] = wt_actions .delete_worktree ,
122+ [" <C-f>" ] = wt_actions .toggle_forced_deletion ,
123+ },
124+ },
125+ attach_mappings = function (_ , _ )
126+ action_set .select :replace (switch_worktree )
127+ return true
128+ end
129+ }
130+
131+ local get_default_opts = function (opts )
132+ opts = opts or {}
133+ local defaults = (function ()
134+ if pconf .theme then
135+ return require (" telescope.themes" )[" get_" .. pconf .theme ](pconf )
136+ end
137+ return vim .deepcopy (pconf )
138+ end )()
139+
140+ if pconf .mappings then
141+ defaults .attach_mappings = function (prompt_bufnr , map )
142+ if pconf .attach_mappings then
143+ pconf .attach_mappings (prompt_bufnr , map )
144+ end
145+ for mode , tbl in pairs (pconf .mappings ) do
146+ for key , action in pairs (tbl ) do
147+ map (mode , key , action )
148+ end
149+ end
150+ return true
151+ end
152+ end
153+
154+ if opts .attach_mappings then
155+ local opts_attach = opts .attach_mappings
156+ opts .attach_mappings = function (prompt_bufnr , map )
157+ defaults .attach_mappings (prompt_bufnr , map )
158+ return opts_attach (prompt_bufnr , map )
159+ end
160+ end
161+ return vim .tbl_deep_extend (" force" , defaults , opts )
162+ end
163+
112164local create_worktree = function (opts )
113- opts = opts or {}
165+ opts = get_default_opts ( opts or {})
114166 opts .attach_mappings = function ()
115167 actions .select_default :replace (
116168 function (prompt_bufnr , _ )
@@ -142,14 +194,16 @@ local create_worktree = function(opts)
142194end
143195
144196local telescope_git_worktree = function (opts )
145- opts = opts or {}
197+ opts = get_default_opts ( opts or {})
146198 local output = utils .get_os_command_output ({" git" , " worktree" , " list" })
147199 local results = {}
148- local widths = {
149- path = 0 ,
150- sha = 0 ,
151- branch = 0
152- }
200+
201+ local items = vim .F .if_nil (opts .items , {
202+ { " branch" , 0 },
203+ { " path" , 0 },
204+ { " sha" , 0 },
205+ })
206+ local displayer_items = {}
153207
154208 local parse_line = function (line )
155209 local fields = vim .split (string.gsub (line , " %s+" , " " ), " " )
@@ -161,14 +215,17 @@ local telescope_git_worktree = function(opts)
161215
162216 if entry .sha ~= " (bare)" then
163217 local index = # results + 1
164- 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- widths [key ] = math.max (val , path_len )
169- else
170- widths [key ] = math.max (val , strings .strdisplaywidth (entry [key ] or " " ))
218+ for key , item in ipairs (items ) do
219+ if not opts .items then
220+ if item [1 ] == ' path' then
221+ local new_path = utils .transform_path (opts , entry [item [1 ]])
222+ local path_len = strings .strdisplaywidth (new_path or " " )
223+ item [2 ] = math.max (item [2 ], path_len )
224+ else
225+ item [2 ] = math.max (item [2 ], strings .strdisplaywidth (entry [item [1 ]] or " " ))
226+ end
171227 end
228+ displayer_items [key ] = { width = item [2 ] }
172229 end
173230
174231 table.insert (results , index , entry )
@@ -180,28 +237,33 @@ local telescope_git_worktree = function(opts)
180237 end
181238
182239 if # results == 0 then
240+ error (" No git branches found" )
183241 return
184242 end
185243
186244 local displayer = require (" telescope.pickers.entry_display" ).create {
187245 separator = " " ,
188- items = {
189- { width = widths .branch },
190- { width = widths .path },
191- { width = widths .sha },
192- },
246+ items = displayer_items ,
193247 }
194248
195249 local make_display = function (entry )
196- return displayer {
197- { entry .branch , " TelescopeResultsIdentifier" },
198- { utils .transform_path (opts , entry .path ) },
199- { entry .sha },
200- }
250+ local foo = {}
251+ for _ , item in ipairs (items ) do
252+ if item [1 ] == " branch" then
253+ table.insert (foo , { entry [item [1 ]], " TelescopeResultsIdentifier" })
254+ elseif item [1 ] == " path" then
255+ table.insert (foo , { utils .transform_path (opts , entry [item [1 ]]) })
256+ elseif item [1 ] == " sha" then
257+ table.insert (foo , { entry [item [1 ]] })
258+ else
259+ error (" Invalid git-worktree entry item: " .. tostring (item [1 ]))
260+ end
261+ end
262+ return displayer (foo )
201263 end
202264
203265 pickers .new (opts or {}, {
204- prompt_title = " Git Worktrees" ,
266+ prompt_title = opts . prompt_title or " Git Worktrees" ,
205267 finder = finders .new_table {
206268 results = results ,
207269 entry_maker = function (entry )
@@ -212,23 +274,20 @@ local telescope_git_worktree = function(opts)
212274 end ,
213275 },
214276 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
225277 }):find ()
226278end
227279
228- return require (" telescope" ).register_extension (
229- {
230- exports = {
231- git_worktrees = telescope_git_worktree ,
232- create_git_worktree = create_worktree
233- }
234- })
280+ local git_worktree_setup = function (opts )
281+ pconf .mappings = vim .tbl_deep_extend (" force" , pconf .mappings , require (" telescope.config" ).values .mappings )
282+ pconf = vim .tbl_deep_extend (" force" , pconf , opts )
283+ end
284+
285+
286+ return require (" telescope" ).register_extension ({
287+ setup = git_worktree_setup ,
288+ exports = {
289+ git_worktrees = telescope_git_worktree ,
290+ create_git_worktree = create_worktree ,
291+ actions = wt_actions ,
292+ },
293+ })
0 commit comments