Skip to content

Commit 03ff368

Browse files
parent 9a97982
author Brian Shu <[email protected]> 1611447930 -0500 committer Brian Shu <[email protected]> 1611453550 -0500 started coroutine async pickers more stuff removed accidental menu stuff added wip cancel stray character
1 parent 099910d commit 03ff368

File tree

2 files changed

+207
-11
lines changed

2 files changed

+207
-11
lines changed

lua/telescope/finders.lua

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local Job = require('plenary.job')
22

3+
local co = coroutine
34
local make_entry = require('telescope.make_entry')
5+
local Executor = require('telescope.utils').Executor
46
local log = require('telescope.log')
57

68
local finders = {}
@@ -33,7 +35,7 @@ local JobFinder = _callable_obj()
3335
function JobFinder:new(opts)
3436
opts = opts or {}
3537

36-
> assert(not opts.results, "`results` should be used with finder.new_table")
38+
assert(not opts.results, "`results` should be used with finder.new_table")
3739
assert(not opts.static, "`static` should be used with finder.new_oneshot_job")
3840

3941
local obj = setmetatable({
@@ -179,13 +181,26 @@ function OneshotJobFinder:new(opts)
179181
num_execution = num_execution + 1
180182

181183
local current_count = num_results
182-
for index = 1, current_count do
183-
process_result(results[index])
184-
end
185-
186-
if completed then
187-
process_complete()
188-
end
184+
local exe = Executor.new {}
185+
exe:add(co.create(function(results)
186+
for index = 1, current_count do
187+
process_result(results[index])
188+
co.yield()
189+
end
190+
191+
if completed then
192+
process_complete()
193+
end
194+
end), results)
195+
exe:run()
196+
197+
-- for index = 1, current_count do
198+
-- process_result(results[index])
199+
-- end
200+
201+
-- if completed then
202+
-- process_complete()
203+
-- end
189204
end
190205
end)
191206

@@ -248,12 +263,43 @@ function StaticFinder:new(opts)
248263
return setmetatable({ results = results }, self)
249264
end
250265

251-
function StaticFinder:_find(_, process_result, process_complete)
252-
for _, v in ipairs(self.results) do
253-
process_result(v)
266+
local static_find = successive_async(function(results, process_result, process_complete)
267+
local exe = Executor.new {}
268+
exe:add(co.create(function(results)
269+
for _, v in ipairs(results) do
270+
process_result(v)
271+
co.yield()
272+
end
273+
process_complete()
274+
end), results)
275+
276+
exe:run()
277+
end)
278+
279+
local function exe_cancel_wrap(fn)
280+
local exe = Executor.new {}
281+
282+
local function wrapped(...)
283+
exe:close()
284+
exe:add(fn, ...)
285+
exe:run()
254286
end
255287

288+
return wrapped
289+
end
290+
291+
-- WIP, cancel previous task when we start a new one
292+
local static_find_cancel = exe_cancel_wrap(co.create(function(results, process_result, process_complete)
293+
for _, v in ipairs(results) do
294+
process_result(v)
295+
co.yield()
296+
end
256297
process_complete()
298+
end))
299+
300+
function StaticFinder:_find(_, process_result, process_complete)
301+
static_find(self.results, process_result, process_complete)
302+
-- static_find_cancel(self.results, process_result, process_complete)
257303
end
258304

259305

lua/telescope/utils.lua

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,154 @@ function utils.get_os_command_output(cmd)
202202
return Job:new({ command = command, args = cmd }):sync()
203203
end
204204

205+
local uv = vim.loop
206+
local co = coroutine
207+
208+
local Executor = {}
209+
Executor.__index = Executor
210+
211+
function Executor.new(opts)
212+
opts = opts or {}
213+
214+
local self = setmetatable({}, Executor)
215+
216+
self.tasks = opts.tasks or {}
217+
self.mode = opts.mode or "next"
218+
self.index = opts.start_idx or 1
219+
self.idle = uv.new_idle()
220+
221+
return self
222+
end
223+
224+
function Executor:run()
225+
self.idle:start(vim.schedule_wrap(function()
226+
if #self.tasks == 0 then
227+
self.idle:stop()
228+
return
229+
end
230+
231+
if self.mode == "finish" then
232+
self:step_finish()
233+
else
234+
self:step()
235+
end
236+
end))
237+
end
238+
239+
function Executor:close()
240+
self.idle:stop()
241+
self.tasks = {}
242+
end
243+
244+
function Executor:step_finish()
245+
if #self.tasks == 0 then return end
246+
local curr_task = self.tasks[self.index]
247+
if curr_task == nil then
248+
self.index = 1
249+
curr_task = self.tasks[self.index]
250+
end
251+
252+
local _, _ = co.resume(curr_task)
253+
if co.status(curr_task) == "dead" then
254+
table.remove(self.tasks, self.index)
255+
256+
self.index = self.index + 1
257+
end
258+
end
259+
260+
function Executor:step()
261+
if #self.tasks == 0 then return end
262+
local curr_task = self.tasks[self.index]
263+
if curr_task == nil then
264+
self.index = 1
265+
curr_task = self.tasks[self.index]
266+
end
267+
268+
local _, _ = co.resume(curr_task[1], unpack(curr_task[2]))
269+
if co.status(curr_task[1]) == "dead" then
270+
table.remove(self.tasks, self.index)
271+
end
272+
273+
self.index = self.index + 1
274+
end
275+
276+
function Executor:get_current_task()
277+
return self.tasks[self.index]
278+
end
279+
280+
function Executor:remove_task(idx)
281+
table.remove(self.tasks, idx)
282+
end
283+
284+
function Executor:add(task, ...)
285+
table.insert(self.tasks, {task, {...}})
286+
end
287+
288+
utils.Executor = Executor
289+
290+
List = {}
291+
292+
function List.new()
293+
return {first = 0, last = -1}
294+
end
295+
296+
function List.pushleft(list, value)
297+
local first = list.first - 1
298+
list.first = first
299+
list[first] = value
300+
end
301+
302+
function List.pushright (list, value)
303+
local last = list.last + 1
304+
list.last = last
305+
list[last] = value
306+
end
307+
308+
function List.popleft (list)
309+
local first = list.first
310+
if first > list.last then return nil end
311+
local value = list[first]
312+
list[first] = nil -- to allow garbage collection
313+
list.first = first + 1
314+
return value
315+
end
316+
317+
function List.is_empty(list)
318+
return list.first > list.last
319+
end
320+
321+
function List.popright (list)
322+
local last = list.last
323+
if list.first > last then return nil end
324+
local value = list[last]
325+
list[last] = nil -- to allow garbage collection
326+
list.last = last - 1
327+
return value
328+
end
329+
330+
function List.len(list)
331+
return list.last - list.first
332+
end
333+
334+
utils.List = List
335+
336+
function successive_async(f)
337+
local list = List.new()
338+
339+
local function wrapped(...)
340+
if list == nil then return end
341+
-- if List.is_empty(list) or list == nil then return end
342+
343+
List.pushleft(list, {...})
344+
local curr = List.popright(list)
345+
f(unpack(curr))
346+
end
347+
348+
local function finish()
349+
list = nil
350+
end
351+
352+
return wrapped
353+
end
354+
205355
return utils

0 commit comments

Comments
 (0)