@@ -3,6 +3,10 @@ local Job = require('plenary.job')
33local make_entry = require (' telescope.make_entry' )
44local log = require (' telescope.log' )
55
6+ local async_static_finder = require (' telescope.finders.async_static_finder' )
7+ local async_oneshot_finder = require (' telescope.finders.async_oneshot_finder' )
8+ -- local async_job_finder = require('telescope.finders.async_job_finder')
9+
610local finders = {}
711
812local _callable_obj = function ()
@@ -104,179 +108,24 @@ function JobFinder:_find(prompt, process_result, process_complete)
104108 self .job :start ()
105109end
106110
107- local OneshotJobFinder = _callable_obj ()
108-
109- function OneshotJobFinder :new (opts )
110- opts = opts or {}
111-
112- assert (not opts .results , " `results` should be used with finder.new_table" )
113- assert (not opts .static , " `static` should be used with finder.new_oneshot_job" )
114-
115- local obj = setmetatable ({
116- fn_command = opts .fn_command ,
117- entry_maker = opts .entry_maker or make_entry .from_string ,
118-
119- cwd = opts .cwd ,
120- writer = opts .writer ,
121-
122- maximum_results = opts .maximum_results ,
123-
124- _started = false ,
125- }, self )
126-
127- obj ._find = coroutine.wrap (function (finder , _ , process_result , process_complete )
128- local num_execution = 1
129- local num_results = 0
130-
131- local results = setmetatable ({}, {
132- __newindex = function (t , k , v )
133- rawset (t , k , v )
134- process_result (v )
135- end
136- })
137-
138- local job_opts = finder :fn_command (_ )
139- if not job_opts then
140- error (debug.traceback (" expected `job_opts` from fn_command" ))
141- end
142-
143- local writer = nil
144- if job_opts .writer and Job .is_job (job_opts .writer ) then
145- writer = job_opts .writer
146- elseif job_opts .writer then
147- writer = Job :new (job_opts .writer )
148- end
149-
150- local on_output = function (_ , line )
151- -- This will call the metamethod, process_result
152- num_results = num_results + 1
153- results [num_results ] = finder .entry_maker (line )
154- end
155-
156- local completed = false
157- local job = Job :new {
158- command = job_opts .command ,
159- args = job_opts .args ,
160- cwd = job_opts .cwd or finder .cwd ,
161-
162- maximum_results = finder .maximum_results ,
163-
164- writer = writer ,
165-
166- enable_recording = false ,
167-
168- on_stdout = on_output ,
169- on_stderr = on_output ,
170-
171- on_exit = function ()
172- process_complete ()
173- completed = true
174- end ,
175- }
176-
177- job :start ()
178-
179- while true do
180- finder , _ , process_result , process_complete = coroutine.yield ()
181- num_execution = num_execution + 1
182-
183- local current_count = num_results
184- for index = 1 , current_count do
185- process_result (results [index ])
186- end
187-
188- if completed then
189- process_complete ()
190- end
191- end
192- end )
193-
194- return obj
195- end
196-
197- function OneshotJobFinder :old_find (_ , process_result , process_complete )
198- local first_run = false
199-
200- if not self ._started then
201- first_run = true
202-
203- self ._started = true
204-
205- end
206-
207- -- First time we get called, start on up that job.
208- -- Every time after that, just use the results LUL
209- if not first_run then
210- return
211- end
212- end
213-
214-
215-
216- --[[ =============================================================
217- Static Finders
218-
219- A static finder has results that never change.
220- They are passed in directly as a result.
221- -- ============================================================= ]]
222- local StaticFinder = _callable_obj ()
223-
224- function StaticFinder :new (opts )
225- assert (opts , " Options are required. See documentation for usage" )
226-
227- local input_results
228- if vim .tbl_islist (opts ) then
229- input_results = opts
230- else
231- input_results = opts .results
232- end
233-
234- local entry_maker = opts .entry_maker or make_entry .gen_from_string ()
235-
236- assert (input_results )
237- assert (input_results , " Results are required for static finder" )
238- assert (type (input_results ) == ' table' , " self.results must be a table" )
239-
240- local results = {}
241- for k , v in ipairs (input_results ) do
242- local entry = entry_maker (v )
243-
244- if entry then
245- entry .index = k
246- table.insert (results , entry )
247- end
248- end
249-
250- return setmetatable ({ results = results }, self )
251- end
252-
253- function StaticFinder :_find (_ , process_result , process_complete )
254- for _ , v in ipairs (self .results ) do
255- process_result (v )
256- end
257-
258- process_complete ()
259- end
260-
261-
262- -- local
263-
264-
265111--- Return a new Finder
266112--
267113-- Use at your own risk.
268114-- This opts dictionary is likely to change, but you are welcome to use it right now.
269115-- I will try not to change it needlessly, but I will change it sometimes and I won't feel bad.
270116finders ._new = function (opts )
271- if opts .results then
272- print (" finder.new is deprecated with `results`. You should use `finder.new_table`" )
273- return StaticFinder :new (opts )
274- end
275-
117+ assert (not opts .results , " finder.new is deprecated with `results`. You should use `finder.new_table`" )
276118 return JobFinder :new (opts )
277119end
278120
279121finders .new_job = function (command_generator , entry_maker , maximum_results , cwd )
122+ -- return async_job_finder {
123+ -- command_generator = command_generator,
124+ -- entry_maker = entry_maker,
125+ -- maximum_results = maximum_results,
126+ -- cwd = cwd,
127+ -- }
128+
280129 return JobFinder :new {
281130 fn_command = function (_ , prompt )
282131 local command_list = command_generator (prompt )
@@ -298,18 +147,20 @@ finders.new_job = function(command_generator, entry_maker, maximum_results, cwd)
298147 }
299148end
300149
301- --- @param command_list string[] Command list to execute.
302- --- @param opts table
150+ --- One shot job
151+ --- @param command_list string[] : Command list to execute.
152+ --- @param opts table : stuff
303153--- @key entry_maker function Optional: function(line: string) => table
304154--- @key cwd string
305155finders .new_oneshot_job = function (command_list , opts )
306156 opts = opts or {}
307157
308- command_list = vim . deepcopy ( command_list )
158+ assert ( not opts . results , " `results` should be used with finder.new_table " )
309159
160+ command_list = vim .deepcopy (command_list )
310161 local command = table.remove (command_list , 1 )
311162
312- return OneshotJobFinder : new {
163+ return async_oneshot_finder {
313164 entry_maker = opts .entry_maker or make_entry .gen_from_string (),
314165
315166 cwd = opts .cwd ,
331182-- results table, the results to run on
332183-- entry_maker function, the function to convert results to entries.
333184finders .new_table = function (t )
334- return StaticFinder : new (t )
185+ return async_static_finder (t )
335186end
336187
337188return finders
0 commit comments