Skip to content

Commit

Permalink
feat: asyncify pickers - except for live_grep (nvim-telescope#709)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tjdevries authored Apr 8, 2021
1 parent e5fbe6f commit 64e5906
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 353 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
doc/tags

.luacheckcache
14 changes: 7 additions & 7 deletions lua/telescope/builtin/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ end
files.file_browser = function(opts)
opts = opts or {}

opts.depth = opts.depth or 1
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()

local gen_new_finder = function(path)
opts.new_finder = opts.new_finder or function(path)
opts.cwd = path
local data = {}

scan.scan_dir(path, {
hidden = opts.hidden or false,
add_dirs = true,
depth = 1,
depth = opts.depth,
on_insert = function(entry, typ)
table.insert(data, typ == 'directory' and (entry .. os_sep) or entry)
end
Expand All @@ -242,8 +242,8 @@ files.file_browser = function(opts)
end

pickers.new(opts, {
prompt_title = 'Find Files',
finder = gen_new_finder(opts.cwd),
prompt_title = 'File Browser',
finder = opts.new_finder(opts.cwd),
previewer = conf.file_previewer(opts),
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
Expand All @@ -253,7 +253,7 @@ files.file_browser = function(opts)
local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2))
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
end)

local create_new_file = function()
Expand All @@ -276,7 +276,7 @@ files.file_browser = function(opts)
Path:new(fpath:sub(1, -2)):mkdir({ parents = true })
local new_cwd = vim.fn.expand(fpath)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
end
end

Expand Down
2 changes: 0 additions & 2 deletions lua/telescope/builtin/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,6 @@ internal.autocommands = function(opts)
inner_loop(line)
end

-- print(vim.inspect(autocmd_table))

pickers.new(opts,{
prompt_title = 'autocommands',
finder = finders.new_table {
Expand Down
10 changes: 1 addition & 9 deletions lua/telescope/entry_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if past loop of must have scores,
local EntryManager = {}
EntryManager.__index = EntryManager

function EntryManager:new(max_results, set_entry, info, id)
function EntryManager:new(max_results, set_entry, info)
log.trace("Creating entry_manager...")

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

return setmetatable({
id = id,
linked_states = LinkedList:new { track_at = max_results },
info = info,
max_results = max_results,
Expand Down Expand Up @@ -128,13 +127,6 @@ function EntryManager:_append_container(picker, new_container, should_update)
end

function EntryManager:add_entry(picker, score, entry)
if picker and picker.id then
if picker.request_number ~= self.id then
error("ADDING ENTRY TOO LATE!")
return
end
end

score = score or 0

local max_res = self.max_results
Expand Down
187 changes: 19 additions & 168 deletions lua/telescope/finders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local Job = require('plenary.job')
local make_entry = require('telescope.make_entry')
local log = require('telescope.log')

local async_static_finder = require('telescope.finders.async_static_finder')
local async_oneshot_finder = require('telescope.finders.async_oneshot_finder')
-- local async_job_finder = require('telescope.finders.async_job_finder')

local finders = {}

local _callable_obj = function()
Expand Down Expand Up @@ -104,179 +108,24 @@ function JobFinder:_find(prompt, process_result, process_complete)
self.job:start()
end

local OneshotJobFinder = _callable_obj()

function OneshotJobFinder:new(opts)
opts = opts or {}

assert(not opts.results, "`results` should be used with finder.new_table")
assert(not opts.static, "`static` should be used with finder.new_oneshot_job")

local obj = setmetatable({
fn_command = opts.fn_command,
entry_maker = opts.entry_maker or make_entry.from_string,

cwd = opts.cwd,
writer = opts.writer,

maximum_results = opts.maximum_results,

_started = false,
}, self)

obj._find = coroutine.wrap(function(finder, _, process_result, process_complete)
local num_execution = 1
local num_results = 0

local results = setmetatable({}, {
__newindex = function(t, k, v)
rawset(t, k, v)
process_result(v)
end
})

local job_opts = finder:fn_command(_)
if not job_opts then
error(debug.traceback("expected `job_opts` from fn_command"))
end

local writer = nil
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 on_output = function(_, line)
-- This will call the metamethod, process_result
num_results = num_results + 1
results[num_results] = finder.entry_maker(line)
end

local completed = false
local job = Job:new {
command = job_opts.command,
args = job_opts.args,
cwd = job_opts.cwd or finder.cwd,

maximum_results = finder.maximum_results,

writer = writer,

enable_recording = false,

on_stdout = on_output,
on_stderr = on_output,

on_exit = function()
process_complete()
completed = true
end,
}

job:start()

while true do
finder, _, process_result, process_complete = coroutine.yield()
num_execution = num_execution + 1

local current_count = num_results
for index = 1, current_count do
process_result(results[index])
end

if completed then
process_complete()
end
end
end)

return obj
end

function OneshotJobFinder:old_find(_, process_result, process_complete)
local first_run = false

if not self._started then
first_run = true

self._started = true

end

-- First time we get called, start on up that job.
-- Every time after that, just use the results LUL
if not first_run then
return
end
end



--[[ =============================================================
Static Finders
A static finder has results that never change.
They are passed in directly as a result.
-- ============================================================= ]]
local StaticFinder = _callable_obj()

function StaticFinder:new(opts)
assert(opts, "Options are required. See documentation for usage")

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()

assert(input_results)
assert(input_results, "Results are required for static finder")
assert(type(input_results) == 'table', "self.results must be a table")

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 }, self)
end

function StaticFinder:_find(_, process_result, process_complete)
for _, v in ipairs(self.results) do
process_result(v)
end

process_complete()
end


-- local


--- Return a new Finder
--
-- Use at your own risk.
-- This opts dictionary is likely to change, but you are welcome to use it right now.
-- I will try not to change it needlessly, but I will change it sometimes and I won't feel bad.
finders._new = function(opts)
if opts.results then
print("finder.new is deprecated with `results`. You should use `finder.new_table`")
return StaticFinder:new(opts)
end

assert(not opts.results, "finder.new is deprecated with `results`. You should use `finder.new_table`")
return JobFinder:new(opts)
end

finders.new_job = function(command_generator, entry_maker, maximum_results, cwd)
-- return async_job_finder {
-- command_generator = command_generator,
-- entry_maker = entry_maker,
-- maximum_results = maximum_results,
-- cwd = cwd,
-- }

return JobFinder:new {
fn_command = function(_, prompt)
local command_list = command_generator(prompt)
Expand All @@ -298,18 +147,20 @@ finders.new_job = function(command_generator, entry_maker, maximum_results, cwd)
}
end

---@param command_list string[] Command list to execute.
---@param opts table
--- One shot job
---@param command_list string[]: Command list to execute.
---@param opts table: stuff
--- @key entry_maker function Optional: function(line: string) => table
--- @key cwd string
finders.new_oneshot_job = function(command_list, opts)
opts = opts or {}

command_list = vim.deepcopy(command_list)
assert(not opts.results, "`results` should be used with finder.new_table")

command_list = vim.deepcopy(command_list)
local command = table.remove(command_list, 1)

return OneshotJobFinder:new {
return async_oneshot_finder {
entry_maker = opts.entry_maker or make_entry.gen_from_string(),

cwd = opts.cwd,
Expand All @@ -331,7 +182,7 @@ end
-- results table, the results to run on
-- entry_maker function, the function to convert results to entries.
finders.new_table = function(t)
return StaticFinder:new(t)
return async_static_finder(t)
end

return finders
Loading

0 comments on commit 64e5906

Please sign in to comment.