feat: asyncify pickers - except for live_grep (#709)
* something kind of works already * yayayayayayayayayayayayayayayayayayayayayayayayayayayayayayayayaya * use async for everything besides live jobs * fix: fixup autocmds previewer * fix: lints for prime * temp: Add example of how we can think about async sorters * feat: Allow picker to decide when to cancel * fix: simplify scoring logic and tests * fixup: name * fix: Move back towards more backwards compat methods * fixup: Remove results from opts * fixup: remove trailing quote * fixup: Attempt to clean up some more async items. Next is status * wip: Add todo for when bfredl implements extmarks over the EOL * wip * fixup: got em * fixup: cleaning * fixup: docs
This commit is contained in:
73
lua/telescope/finders/async_job_finder.lua
Normal file
73
lua/telescope/finders/async_job_finder.lua
Normal file
@@ -0,0 +1,73 @@
|
||||
local log = require('telescope.log')
|
||||
local Job = require('plenary.job')
|
||||
|
||||
local async_lib = require('plenary.async_lib')
|
||||
local async = async_lib.async
|
||||
-- local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
|
||||
local make_entry = require('telescope.make_entry')
|
||||
|
||||
return function(opts)
|
||||
local entry_maker = opts.entry_maker or make_entry.gen_from_string()
|
||||
local fn_command = function(prompt)
|
||||
local command_list = opts.command_generator(prompt)
|
||||
if command_list == nil then
|
||||
return nil
|
||||
end
|
||||
|
||||
local command = table.remove(command_list, 1)
|
||||
|
||||
return {
|
||||
command = command,
|
||||
args = command_list,
|
||||
}
|
||||
end
|
||||
|
||||
local job
|
||||
return setmetatable({
|
||||
close = function() end,
|
||||
}, {
|
||||
__call = void(async(function(prompt, process_result, process_complete)
|
||||
print("are we callin anything?", job)
|
||||
if job and not job.is_shutdown then
|
||||
log.debug("Shutting down old job")
|
||||
job:shutdown()
|
||||
end
|
||||
|
||||
local job_opts = fn_command(prompt)
|
||||
if not job_opts then return end
|
||||
|
||||
local writer = nil
|
||||
if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
writer = job_opts.writer
|
||||
elseif opts.writer then
|
||||
writer = Job:new(job_opts.writer)
|
||||
end
|
||||
|
||||
job = Job:new {
|
||||
command = job_opts.command,
|
||||
args = job_opts.args,
|
||||
cwd = job_opts.cwd or opts.cwd,
|
||||
maximum_results = opts.maximum_results,
|
||||
writer = writer,
|
||||
enable_recording = false,
|
||||
|
||||
on_stdout = vim.schedule_wrap(function(_, line)
|
||||
if not line or line == "" then
|
||||
return
|
||||
end
|
||||
|
||||
-- TODO: shutdown job here.
|
||||
process_result(entry_maker(line))
|
||||
end),
|
||||
|
||||
on_exit = function()
|
||||
process_complete()
|
||||
end,
|
||||
}
|
||||
|
||||
job:start()
|
||||
end)),
|
||||
})
|
||||
end
|
||||
81
lua/telescope/finders/async_oneshot_finder.lua
Normal file
81
lua/telescope/finders/async_oneshot_finder.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
local async_lib = require('plenary.async_lib')
|
||||
local async = async_lib.async
|
||||
local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
|
||||
local AWAITABLE = 1000
|
||||
|
||||
local make_entry = require('telescope.make_entry')
|
||||
|
||||
local Job = require('plenary.job')
|
||||
|
||||
return function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local entry_maker = opts.entry_maker or make_entry.from_string
|
||||
local cwd = opts.cwd
|
||||
local fn_command = assert(opts.fn_command, "Must pass `fn_command`")
|
||||
|
||||
local results = {}
|
||||
local num_results = 0
|
||||
|
||||
local job_started = false
|
||||
local job_completed = false
|
||||
return setmetatable({
|
||||
close = function() results = {}; job_started = false end,
|
||||
results = results,
|
||||
}, {
|
||||
__call = void(async(function(_, prompt, process_result, process_complete)
|
||||
if not job_started then
|
||||
local job_opts = fn_command()
|
||||
|
||||
local writer
|
||||
if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
writer = job_opts.writer
|
||||
elseif job_opts.writer then
|
||||
writer = Job:new(job_opts.writer)
|
||||
end
|
||||
|
||||
local job = Job:new {
|
||||
command = job_opts.command,
|
||||
args = job_opts.args,
|
||||
cwd = job_opts.cwd or cwd,
|
||||
maximum_results = opts.maximum_results,
|
||||
writer = writer,
|
||||
enable_recording = false,
|
||||
|
||||
on_stdout = vim.schedule_wrap(function(_, line)
|
||||
num_results = num_results + 1
|
||||
|
||||
local v = entry_maker(line)
|
||||
results[num_results] = v
|
||||
process_result(v)
|
||||
end),
|
||||
|
||||
on_exit = function()
|
||||
process_complete()
|
||||
job_completed = true
|
||||
end,
|
||||
}
|
||||
|
||||
job:start()
|
||||
job_started = true
|
||||
end
|
||||
|
||||
local current_count = num_results
|
||||
for index = 1, current_count do
|
||||
if process_result(results[index]) then
|
||||
break
|
||||
end
|
||||
|
||||
if index % AWAITABLE == 0 then
|
||||
await(async_lib.scheduler())
|
||||
end
|
||||
end
|
||||
|
||||
if job_completed then
|
||||
process_complete()
|
||||
end
|
||||
end)),
|
||||
})
|
||||
end
|
||||
41
lua/telescope/finders/async_static_finder.lua
Normal file
41
lua/telescope/finders/async_static_finder.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
local async_lib = require('plenary.async_lib')
|
||||
local async = async_lib.async
|
||||
local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
|
||||
local make_entry = require('telescope.make_entry')
|
||||
|
||||
return function(opts)
|
||||
local input_results
|
||||
if vim.tbl_islist(opts) then input_results = opts
|
||||
else input_results = opts.results end
|
||||
|
||||
local entry_maker = opts.entry_maker or make_entry.gen_from_string()
|
||||
|
||||
local results = {}
|
||||
for k, v in ipairs(input_results) do
|
||||
local entry = entry_maker(v)
|
||||
|
||||
if entry then
|
||||
entry.index = k
|
||||
table.insert(results, entry)
|
||||
end
|
||||
end
|
||||
|
||||
return setmetatable({
|
||||
results = results,
|
||||
close = function() end,
|
||||
}, {
|
||||
__call = void(async(function(_, _, process_result, process_complete)
|
||||
for i, v in ipairs(results) do
|
||||
if process_result(v) then break end
|
||||
|
||||
if i % 1000 == 0 then
|
||||
await(async_lib.scheduler())
|
||||
end
|
||||
end
|
||||
|
||||
process_complete()
|
||||
end)),
|
||||
})
|
||||
end
|
||||
Reference in New Issue
Block a user