@@ -109,8 +109,54 @@ local create_input_prompt = function(cb)
109109 cb (subtree )
110110end
111111
112+ local pconf = {
113+ mappings = {
114+ [" i" ] = {
115+ [" <C-d>" ] = delete_worktree ,
116+ [" <C-f>" ] = toggle_forced_deletion ,
117+ },
118+ [" n" ] = {
119+ [" <C-d>" ] = delete_worktree ,
120+ [" <C-f>" ] = toggle_forced_deletion ,
121+ },
122+ },
123+ }
124+
125+ local get_default_opts = function (opts )
126+ opts = opts or {}
127+ local defaults = (function ()
128+ if pconf .theme then
129+ return require (" telescope.themes" )[" get_" .. pconf .theme ](pconf )
130+ end
131+ return vim .deepcopy (pconf )
132+ end )()
133+
134+ if pconf .mappings then
135+ defaults .attach_mappings = function (prompt_bufnr , map )
136+ if pconf .attach_mappings then
137+ pconf .attach_mappings (prompt_bufnr , map )
138+ end
139+ for mode , tbl in pairs (pconf .mappings ) do
140+ for key , action in pairs (tbl ) do
141+ map (mode , key , action )
142+ end
143+ end
144+ return true
145+ end
146+ end
147+
148+ if opts .attach_mappings then
149+ local opts_attach = opts .attach_mappings
150+ opts .attach_mappings = function (prompt_bufnr , map )
151+ defaults .attach_mappings (prompt_bufnr , map )
152+ return opts_attach (prompt_bufnr , map )
153+ end
154+ end
155+ return vim .tbl_deep_extend (" force" , defaults , opts )
156+ end
157+
112158local create_worktree = function (opts )
113- opts = opts or {}
159+ opts = get_default_opts ( opts or {})
114160 opts .attach_mappings = function ()
115161 actions .select_default :replace (
116162 function (prompt_bufnr , _ )
@@ -142,14 +188,16 @@ local create_worktree = function(opts)
142188end
143189
144190local telescope_git_worktree = function (opts )
145- opts = opts or {}
191+ opts = get_default_opts ( opts or {})
146192 local output = utils .get_os_command_output ({" git" , " worktree" , " list" })
147193 local results = {}
148- local widths = {
149- path = 0 ,
150- sha = 0 ,
151- branch = 0
152- }
194+
195+ local items = vim .F .if_nil (opts .items , {
196+ { " branch" , 0 },
197+ { " path" , 0 },
198+ { " sha" , 0 },
199+ })
200+ local displayer_items = {}
153201
154202 local parse_line = function (line )
155203 local fields = vim .split (string.gsub (line , " %s+" , " " ), " " )
@@ -161,14 +209,17 @@ local telescope_git_worktree = function(opts)
161209
162210 if entry .sha ~= " (bare)" then
163211 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 " " ))
212+ for key , item in ipairs (items ) do
213+ if not opts .items then
214+ if item [1 ] == ' path' then
215+ local new_path = utils .transform_path (opts , entry [item [1 ]])
216+ local path_len = strings .strdisplaywidth (new_path or " " )
217+ item [2 ] = math.max (item [2 ], path_len )
218+ else
219+ item [2 ] = math.max (item [2 ], strings .strdisplaywidth (entry [item [1 ]] or " " ))
220+ end
171221 end
222+ displayer_items [key ] = { width = item [2 ] }
172223 end
173224
174225 table.insert (results , index , entry )
@@ -180,28 +231,33 @@ local telescope_git_worktree = function(opts)
180231 end
181232
182233 if # results == 0 then
234+ error (" No git branches found" )
183235 return
184236 end
185237
186238 local displayer = require (" telescope.pickers.entry_display" ).create {
187239 separator = " " ,
188- items = {
189- { width = widths .branch },
190- { width = widths .path },
191- { width = widths .sha },
192- },
240+ items = displayer_items ,
193241 }
194242
195243 local make_display = function (entry )
196- return displayer {
197- { entry .branch , " TelescopeResultsIdentifier" },
198- { utils .transform_path (opts , entry .path ) },
199- { entry .sha },
200- }
244+ local foo = {}
245+ for _ , item in ipairs (items ) do
246+ if item [1 ] == " branch" then
247+ table.insert (foo , { entry [item [1 ]], " TelescopeResultsIdentifier" })
248+ elseif item [1 ] == " path" then
249+ table.insert (foo , { utils .transform_path (opts , entry [item [1 ]]) })
250+ elseif item [1 ] == " sha" then
251+ table.insert (foo , { entry [item [1 ]] })
252+ else
253+ error (" Invalid git-worktree entry item: " .. tostring (item [1 ]))
254+ end
255+ end
256+ return displayer (foo )
201257 end
202258
203259 pickers .new (opts or {}, {
204- prompt_title = " Git Worktrees" ,
260+ prompt_title = opts . prompt_title or " Git Worktrees" ,
205261 finder = finders .new_table {
206262 results = results ,
207263 entry_maker = function (entry )
@@ -212,23 +268,19 @@ local telescope_git_worktree = function(opts)
212268 end ,
213269 },
214270 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
225271 }):find ()
226272end
227273
228- return require (" telescope" ).register_extension (
229- {
230- exports = {
231- git_worktrees = telescope_git_worktree ,
232- create_git_worktree = create_worktree
233- }
234- })
274+ local git_worktree_setup = function (opts )
275+ pconf .mappings = vim .tbl_deep_extend (" force" , pconf .mappings , require (" telescope.config" ).values .mappings )
276+ pconf = vim .tbl_deep_extend (" force" , pconf , opts )
277+ end
278+
279+
280+ return require (" telescope" ).register_extension ({
281+ setup = git_worktree_setup ,
282+ exports = {
283+ git_worktrees = telescope_git_worktree ,
284+ create_git_worktree = create_worktree ,
285+ },
286+ })
0 commit comments