-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
local a = require "plenary.async" | ||
local log = require "plenary.log" | ||
|
||
---@class AsyncFinder: TelescopeFinder | ||
---@field closed boolean | ||
---@field entries FrecencyEntry[] | ||
---@field rx { recv: fun(): table } | ||
---@operator call():nil | ||
local AsyncFinder = {} | ||
|
||
---@param fs FrecencyFS | ||
---@param path string | ||
---@param entry_maker fun(file: FrecencyFile): FrecencyEntry | ||
---@param initial_results FrecencyFile[] | ||
---@return AsyncFinder | ||
AsyncFinder.new = function(fs, path, entry_maker, initial_results) | ||
local self = setmetatable({ closed = false, entries = {} }, { | ||
__index = AsyncFinder, | ||
__call = function(self, ...) | ||
return self:_find(...) | ||
end, | ||
}) | ||
for i, file in ipairs(initial_results) do | ||
local entry = entry_maker(file) | ||
entry.index = i | ||
table.insert(self.entries, entry) | ||
end | ||
local it = vim.F.nil_wrap(fs:scan_dir(path)) | ||
local index = #initial_results | ||
local count = 0 | ||
local tx, rx = a.control.channel.mpsc() | ||
self.rx = rx | ||
a.run(function() | ||
for name in it do | ||
if self.closed then | ||
break | ||
end | ||
index = index + 1 | ||
count = count + 1 | ||
---@diagnostic disable-next-line: missing-fields | ||
local entry = entry_maker { path = vim.fs.joinpath(path, name), score = 0 } | ||
if entry then | ||
entry.index = index | ||
table.insert(self.entries, entry) | ||
tx.send(entry) | ||
if count % 1000 == 0 then | ||
a.util.scheduler() | ||
end | ||
end | ||
end | ||
self:close() | ||
tx.send(nil) | ||
end) | ||
return self | ||
end | ||
|
||
---@param prompt string | ||
---@param process_result fun(entry: FrecencyEntry): nil | ||
---@param process_complete fun(): nil | ||
function AsyncFinder:_find(prompt, process_result, process_complete) | ||
for _, entry in ipairs(self.entries) do | ||
if process_result(entry) then | ||
return | ||
end | ||
end | ||
local count = 0 | ||
local last_index = self.entries[#self.entries].index | ||
while true do | ||
if self.closed then | ||
break | ||
end | ||
local entry = self.rx.recv() | ||
if entry then | ||
if entry.index > last_index then | ||
if process_result(entry) then | ||
return | ||
end | ||
count = count + 1 | ||
end | ||
else | ||
break | ||
end | ||
end | ||
process_complete() | ||
end | ||
|
||
function AsyncFinder:close() | ||
self.closed = true | ||
end | ||
|
||
return AsyncFinder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.