@@ -106,23 +106,22 @@ M.combine = function(t)
106106
107107 -- First picker options set the tone
108108 local opts1 = (function ()
109- if t .pickers [1 ] then
109+ if t .pickers and t . pickers [1 ] then
110110 local ok , opts = pcall (config .normalize_opts , t , t .pickers [1 ])
111- if not ok or not opts then
112- utils .warn (" Must specify at least one valid picker" )
113- else
114- return opts
115- end
111+ return ok and opts
116112 end
117113 end )()
118- if not opts1 then return end
114+ if not opts1 then
115+ utils .warn (" Must specify at least one valid picker" )
116+ return
117+ end
119118
120119 -- Let fzf_wrap know to NOT start the coroutine
121120 opts1 ._start = false
122121
123122 local cmds , opts = (function ()
124123 local ret , opts = {}, nil
125- for _ , p in ipairs (t .pickers ) do
124+ for _ , p in ipairs (t .pickers --[[ @as table ]] ) do
126125 -- local ok, msg, cmd, o = pcall(FzfLua[p], opts1)
127126 -- if not ok or not cmd then
128127 local _ , cmd , o = FzfLua [p ](opts1 )
@@ -162,87 +161,92 @@ M.global = function(opts)
162161 opts ._start = false
163162 local pickers = {}
164163 local opts_copy = vim .deepcopy (opts )
165- for c , p in pairs (opts .pickers ) do
166- if FzfLua [p ] then
167- if # c == 0 then
164+ for _ , t in ipairs (opts .pickers ) do
165+ local name = t [1 ]
166+ if FzfLua [name ] then
167+ if not t .prefix then
168168 -- Default picker opts set the tone for this picker options
169169 -- this way convert reload / exec_silent actions will use a consistent
170170 -- opts ref in the callbacks so we can later modify internal values
171- pickers [p ] = { FzfLua [p ](opts ) }
171+ pickers [name ] = { FzfLua [name ](opts ) }
172172 -- Override opts with the return opts and store a copy of `pickers[]`
173173 -- as we patch the opts when switching a picker in the change event
174- opts = pickers [p ][3 ]
174+ opts = pickers [name ][3 ]
175175 opts ._start = nil -- remove the start suppression
176- pickers [p ][3 ] = vim .deepcopy (opts )
176+ pickers [name ][3 ] = vim .deepcopy (opts )
177177 else
178178 -- Each subsequent picker gets a fresh copy of the original opts
179179 -- (unmodified by the default picker)
180- pickers [p ] = { FzfLua [p ](opts_copy ) }
180+ pickers [name ] = { FzfLua [name ](opts_copy ) }
181181 end
182182 else
183- utils .warn (string.format (" invalid picker '%s', ignoring." , p ))
183+ utils .warn (string.format (" invalid picker '%s', ignoring." , name ))
184184 end
185185 end
186186
187+ -- Test for default/starting picker
188+ local default_picker = opts .pickers [1 ] and pickers [opts .pickers [1 ][1 ]]
189+ if not default_picker or default_picker .prefix then
190+ utils .err (" default picker not defined or has a prefix, aborting." )
191+ return
192+ end
193+
187194 --- @param q string ?
188195 --- @return table ?, integer ?
189196 local get_picker = function (q )
190- q = type (q ) == " string" and q or " "
191- if # q == 0 then
192- return pickers [opts .pickers [" " ]], 1
193- end
194- for c , p in pairs (opts .pickers ) do
195- if # c > 0 and q :match (" ^" .. utils .lua_regex_escape (c )) then
196- return pickers [p ], # c + 1
197+ if type (q ) == " string" and # q > 0 then
198+ for _ , t in ipairs (opts .pickers ) do
199+ local name = t [1 ]
200+ if t .prefix and # t .prefix > 0 and q :match (" ^" .. utils .lua_regex_escape (t .prefix )) then
201+ return pickers [name ], # t .prefix + 1
202+ end
197203 end
198204 end
205+ return default_picker , 1
199206 end
200207
201- -- Copy all keys without destroying the parent opts ref
202- -- or we will mess up the actions (especially hide)
203- local patch_opts = function (t1 , t2 )
204- for k , _ in pairs (t1 ) do
205- if k ~= " actions" then t1 [k ] = nil end
206- end
207- for k , v in pairs (t2 ) do
208- (function ()
209- if k == " actions" then return end
210- if type (v ) == " table" then
211- t1 [k ] = vim .tbl_deep_extend (" force" , t1 [k ] or {}, v )
212- else
213- t1 [k ] = v
214- end
215- end )()
216- end
217- return t1
218- end
219208
220- -- Get starting picker
221- local cur_picker , cur_sub = get_picker ()
222- if not cur_picker then
223- utils .warn (" default picker not found, aborting." )
224- return
225- end
209+ local cur_picker , cur_sub
226210
227- table.insert ( opts . _fzf_cli_args , " --bind= " .. libuv . shellescape ( " change:+transform: "
228- .. FzfLua .shell .stringify_data (function (args , _ , _ )
211+ local transform_picker = function ( start )
212+ return FzfLua .shell .stringify_data (function (args , _ , _ )
229213 local q = args [1 ]
230214 local new_picker , new_sub = get_picker (q )
215+ assert (new_picker )
231216 local reload = " "
232- if new_picker and new_picker ~= cur_picker then
217+ if start or new_picker and new_picker ~= cur_picker then
233218 -- New picker requested, reload the contents and transform
234219 -- the search string to exclude the picker prefix
235220 cur_sub = new_sub
236221 cur_picker = new_picker
237222 -- Patch the opts refs with important values for path parsing
238223 -- e.g. formatter, path_shorten, etc
239- opts = patch_opts (opts , cur_picker [3 ])
224+ -- TODO: is there a better way to override the callback opts ref?
225+ opts .__alt_opts = new_picker [3 ]
240226 reload = string.format (" reload(%s)+" , new_picker [2 ])
241227 end
242228 return reload .. string.format (" search(%s)" , q :sub (cur_sub ))
243- end , opts , " {q}" )))
229+ end , opts , " {q}" )
230+ end
231+
232+ table.insert (opts ._fzf_cli_args , " --bind="
233+ .. libuv .shellescape (" start:+transform:" .. transform_picker (true )))
234+
235+ table.insert (opts ._fzf_cli_args , " --bind="
236+ .. libuv .shellescape (" change:+transform:" .. transform_picker (false )))
237+
238+ if opts .header ~= false then
239+ local header = {}
240+ for _ , t in pairs (opts .pickers ) do
241+ table.insert (header , string.format (" <%s> %s" ,
242+ utils .ansi_from_hl (opts .hls .header_bind , t .prefix or " default" ),
243+ utils .ansi_from_hl (opts .hls .header_text , t .desc or t [1 ])))
244+ end
245+
246+ opts .header = table.concat (header , " |" )
247+ end
244248
245- return core .fzf_wrap (cur_picker [ 2 ] , opts )
249+ return core .fzf_wrap (utils . shell_nop () , opts )
246250end
247251
248252return M
0 commit comments