1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
local Job = require('plenary.job')
local make_entry = require('telescope.make_entry')
local log = require('telescope.log')
local finders = {}
local _callable_obj = function()
local obj = {}
obj.__index = obj
obj.__call = function(t, ...) return t:_find(...) end
return obj
end
--[[ =============================================================
JobFinder
Uses an external Job to get results. Processes results as they arrive.
For more information about how Jobs are implemented, checkout 'plenary.job'
-- ============================================================= ]]
local JobFinder = _callable_obj()
--- Create a new finder command
---
---@param opts table Keys:
-- fn_command function The function to call
function JobFinder: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({
entry_maker = opts.entry_maker or make_entry.from_string,
fn_command = opts.fn_command,
cwd = opts.cwd,
writer = opts.writer,
-- Maximum number of results to process.
-- Particularly useful for live updating large queries.
maximum_results = opts.maximum_results,
}, self)
return obj
end
function JobFinder:_find(prompt, process_result, process_complete)
log.trace("Finding...")
if self.job and not self.job.is_shutdown then
log.debug("Shutting down old job")
self.job:shutdown()
end
local on_output = function(_, line, _)
if not line or line == "" then
return
end
if self.entry_maker then
line = self.entry_maker(line)
end
process_result(line)
end
local opts = self:fn_command(prompt)
if not opts then return end
local writer = nil
if opts.writer and Job.is_job(opts.writer) then
writer = opts.writer
elseif opts.writer then
writer = Job:new(opts.writer)
end
self.job = Job:new {
command = opts.command,
args = opts.args,
cwd = opts.cwd or self.cwd,
maximum_results = self.maximum_results,
writer = writer,
enable_recording = false,
on_stdout = on_output,
on_stderr = on_output,
on_exit = function()
process_complete()
end,
}
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
return JobFinder:new(opts)
end
finders.new_job = function(command_generator, entry_maker, maximum_results)
return JobFinder:new {
fn_command = function(_, prompt)
local command_list = 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,
entry_maker = entry_maker,
maximum_results = maximum_results,
}
end
---@param command_list string[] Command list to execute.
---@param opts table
--- @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)
local command = table.remove(command_list, 1)
return OneshotJobFinder:new {
entry_maker = opts.entry_maker or make_entry.gen_from_string(),
cwd = opts.cwd,
maximum_results = opts.maximum_results,
fn_command = function()
return {
command = command,
args = command_list,
}
end,
}
end
--- Used to create a finder for a Lua table.
-- If you only pass a table of results, then it will use that as the entries.
--
-- If you pass a table, and then a function, it's used as:
-- 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)
end
return finders
|