Skip to content

Commit 64e5906

Browse files
authored
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
1 parent e5fbe6f commit 64e5906

14 files changed

Lines changed: 414 additions & 353 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build/
22
doc/tags
3+
4+
.luacheckcache

lua/telescope/builtin/files.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ end
211211
files.file_browser = function(opts)
212212
opts = opts or {}
213213

214+
opts.depth = opts.depth or 1
214215
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
215-
216-
local gen_new_finder = function(path)
216+
opts.new_finder = opts.new_finder or function(path)
217217
opts.cwd = path
218218
local data = {}
219219

220220
scan.scan_dir(path, {
221221
hidden = opts.hidden or false,
222222
add_dirs = true,
223-
depth = 1,
223+
depth = opts.depth,
224224
on_insert = function(entry, typ)
225225
table.insert(data, typ == 'directory' and (entry .. os_sep) or entry)
226226
end
@@ -242,8 +242,8 @@ files.file_browser = function(opts)
242242
end
243243

244244
pickers.new(opts, {
245-
prompt_title = 'Find Files',
246-
finder = gen_new_finder(opts.cwd),
245+
prompt_title = 'File Browser',
246+
finder = opts.new_finder(opts.cwd),
247247
previewer = conf.file_previewer(opts),
248248
sorter = conf.file_sorter(opts),
249249
attach_mappings = function(prompt_bufnr, map)
@@ -253,7 +253,7 @@ files.file_browser = function(opts)
253253
local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2))
254254
local current_picker = action_state.get_current_picker(prompt_bufnr)
255255
current_picker.cwd = new_cwd
256-
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
256+
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
257257
end)
258258

259259
local create_new_file = function()
@@ -276,7 +276,7 @@ files.file_browser = function(opts)
276276
Path:new(fpath:sub(1, -2)):mkdir({ parents = true })
277277
local new_cwd = vim.fn.expand(fpath)
278278
current_picker.cwd = new_cwd
279-
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
279+
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
280280
end
281281
end
282282

lua/telescope/builtin/internal.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,6 @@ internal.autocommands = function(opts)
815815
inner_loop(line)
816816
end
817817

818-
-- print(vim.inspect(autocmd_table))
819-
820818
pickers.new(opts,{
821819
prompt_title = 'autocommands',
822820
finder = finders.new_table {

lua/telescope/entry_manager.lua

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if past loop of must have scores,
2626
local EntryManager = {}
2727
EntryManager.__index = EntryManager
2828

29-
function EntryManager:new(max_results, set_entry, info, id)
29+
function EntryManager:new(max_results, set_entry, info)
3030
log.trace("Creating entry_manager...")
3131

3232
info = info or {}
@@ -40,7 +40,6 @@ function EntryManager:new(max_results, set_entry, info, id)
4040
set_entry = set_entry or function() end
4141

4242
return setmetatable({
43-
id = id,
4443
linked_states = LinkedList:new { track_at = max_results },
4544
info = info,
4645
max_results = max_results,
@@ -128,13 +127,6 @@ function EntryManager:_append_container(picker, new_container, should_update)
128127
end
129128

130129
function EntryManager:add_entry(picker, score, entry)
131-
if picker and picker.id then
132-
if picker.request_number ~= self.id then
133-
error("ADDING ENTRY TOO LATE!")
134-
return
135-
end
136-
end
137-
138130
score = score or 0
139131

140132
local max_res = self.max_results

lua/telescope/finders.lua

Lines changed: 19 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ local Job = require('plenary.job')
33
local make_entry = require('telescope.make_entry')
44
local 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+
610
local finders = {}
711

812
local _callable_obj = function()
@@ -104,179 +108,24 @@ function JobFinder:_find(prompt, process_result, process_complete)
104108
self.job:start()
105109
end
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.
270116
finders._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)
277119
end
278120

279121
finders.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
}
299148
end
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
305155
finders.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,
@@ -331,7 +182,7 @@ end
331182
-- results table, the results to run on
332183
-- entry_maker function, the function to convert results to entries.
333184
finders.new_table = function(t)
334-
return StaticFinder:new(t)
185+
return async_static_finder(t)
335186
end
336187

337188
return finders

0 commit comments

Comments
 (0)