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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
local a = vim.api
local popup = require('popup')
local log = require('telescope.log')
local mappings = require('telescope.mappings')
local state = require('telescope.state')
local utils = require('telescope.utils')
local pickers = {}
--- Picker is the main UI that shows up to interact w/ your results.
-- Takes a filter & a previewr
local Picker = {}
Picker.__index = Picker
local Sorter = require('telescope.sorters').Sorter
local Previewer = require('telescope.previewers').Previewer
assert(Sorter)
assert(Previewer)
---@class PickOpts
---@field filter Sorter
---@field maps table
---@field unseen string
--- Create new picker
--- @param opts PickOpts
function Picker:new(opts)
return setmetatable({
filter = opts.filter,
previewer = opts.previewer,
maps = opts.maps,
}, Picker)
end
function Picker._get_window_options(max_columns, max_lines, prompt_title)
local preview = {
border = {},
enter = false,
highlight = false
}
local results = {
border = {},
enter = false,
}
local prompt = {
title = prompt_title,
border = {},
enter = true
}
local width_padding = 10
if max_columns < 200 then
preview.width = 80
else
preview.width = 120
end
local other_width = max_columns - preview.width - (2 * width_padding)
results.width = other_width
prompt.width = other_width
results.height = 25
results.minheight = results.height
prompt.height = 1
prompt.minheight = prompt.height
preview.height = results.height + prompt.height + 2
preview.minheight = preview.height
results.col = width_padding
prompt.col = width_padding
preview.col = results.col + results.width + 2
local height_padding = math.floor(0.95 * max_lines)
results.line = max_lines - height_padding
prompt.line = results.line + results.height + 2
preview.line = results.line
return {
preview = preview,
results = results,
prompt = prompt,
}
end
function Picker:find(opts)
opts = opts or {}
local finder = opts.finder
assert(finder, "Finder is required to do picking")
local sorter = opts.sorter
local prompt_string = opts.prompt
-- Create three windows:
-- 1. Prompt window
-- 2. Options window
-- 3. Preview window
local popup_opts = Picker._get_window_options(vim.o.columns, vim.o.lines, prompt_string)
-- TODO: Add back the borders after fixing some stuff in popup.nvim
local results_win, results_opts = popup.create('', popup_opts.results)
local results_bufnr = a.nvim_win_get_buf(results_win)
local preview_win, preview_opts = popup.create('', popup_opts.preview)
local preview_bufnr = a.nvim_win_get_buf(preview_win)
-- TODO: For some reason, highlighting is kind of weird on these windows.
-- It may actually be my colorscheme tho...
a.nvim_win_set_option(preview_win, 'winhl', 'Normal:Normal')
a.nvim_win_set_option(preview_win, 'winblend', 1)
-- TODO: We need to center this and make it prettier...
local prompt_win, prompt_opts = popup.create('', popup_opts.prompt)
local prompt_bufnr = a.nvim_win_get_buf(prompt_win)
-- a.nvim_buf_set_option(prompt_bufnr, 'buftype', 'prompt')
-- vim.fn.prompt_setprompt(prompt_bufnr, prompt_string)
-- First thing we want to do is set all the lines to blank.
self.max_results = popup_opts.results.height - 1
local initial_lines = {}
for _ = 1, self.max_results do table.insert(initial_lines, "") end
vim.api.nvim_buf_set_lines(results_bufnr, 0, self.max_results, false, initial_lines)
local on_lines = function(_, _, _, first_line, last_line)
local prompt = vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1]
-- Create a closure that has all the data we need
-- We pass a function called "newResult" to get_results
-- get_results calles "newResult" every time it gets a new result
-- picker then (if available) calls sorter
-- and then appropriately places new result in the buffer.
-- TODO: We need to fix the sorting
-- TODO: We should provide a simple fuzzy matcher in Lua for people
-- TODO: We should get all the stuff on the bottom line directly, not floating around
-- TODO: We need to handle huge lists in a good way, cause currently we'll just put too much stuff in the buffer
-- TODO: Stop having things crash if we have an error.
local line_manager = pickers.line_manager(
self.max_results,
function(index, line)
local row = self.max_results - index + 1
log.trace("Setting row", row, "with value", line)
vim.api.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {line})
end
)
local process_result = function(line)
if vim.trim(line) == "" then
return
end
log.trace("Processing result... ", line)
local sort_score = 0
if sorter then
sort_score = sorter:score(prompt, line)
if sort_score == -1 then
log.trace("Filtering out result: ", line)
return
end
end
line_manager:add_result(sort_score, line)
end
local process_complete = function()
local worst_line = self.max_results - line_manager.num_results()
local empty_lines = utils.repeated_table(worst_line, "")
-- vim.api.nvim_buf_set_lines(results_bufnr, 0, worst_line + 1, false, empty_lines)
log.debug("Worst Line after process_complete: %s", worst_line, results_bufnr)
-- local fun = require('fun')
-- local zip = fun.zip
-- local tomap = fun.tomap
-- log.trace("%s", tomap(zip(
-- a.nvim_buf_get_lines(results_bufnr, worst_line, self.max_results, false),
-- self.line_scores
-- )))
end
local ok, msg = pcall(function()
return finder(prompt, process_result, process_complete)
end)
if not ok then
log.warn("Failed with msg: ", msg)
end
end
-- TODO: Uncomment
vim.schedule(function()
on_lines(nil, nil, nil, 0, 1)
end)
-- Register attach
vim.api.nvim_buf_attach(prompt_bufnr, true, {
on_lines = vim.schedule_wrap(on_lines),
on_detach = function(...)
-- print("DETACH:", ...)
end,
})
-- TODO: Use WinLeave as well?
local on_buf_leave = string.format(
[[ autocmd BufLeave <buffer> ++nested ++once :lua __TelescopeOnLeave(%s)]],
prompt_bufnr)
vim.cmd([[augroup PickerInsert]])
vim.cmd([[ au!]])
vim.cmd( on_buf_leave)
vim.cmd([[augroup END]])
self.prompt_bufnr = prompt_bufnr
state.set_status(prompt_bufnr, {
prompt_bufnr = prompt_bufnr,
prompt_win = prompt_win,
prompt_border_win = prompt_opts.border.win_id,
results_bufnr = results_bufnr,
results_win = results_win,
results_border_win = results_opts.border.win_id,
preview_bufnr = preview_bufnr,
preview_win = preview_win,
preview_border_win = preview_opts.border.win_id,
picker = self,
previewer = self.previewer,
finder = finder,
})
mappings.set_keymap(prompt_bufnr, results_bufnr)
vim.cmd [[startinsert]]
end
function Picker:close_windows(status)
local prompt_win = status.prompt_win
local results_win = status.results_win
local preview_win = status.preview_win
local prompt_border_win = status.prompt_border_win
local results_border_win = status.results_border_win
local preview_border_win = status.preview_border_win
local function del_win(name, win_id, force)
if not vim.api.nvim_win_is_valid(win_id) then
return
end
if not pcall(vim.api.nvim_win_close, win_id, force) then
log.trace("Unable to close window: %s/%s", name, win_id)
end
end
del_win("prompt_win", prompt_win, true)
del_win("results_win", results_win, true)
del_win("preview_win", preview_win, true)
del_win("prompt_border_win", prompt_border_win, true)
del_win("results_border_win", results_border_win, true)
del_win("preview_border_win", preview_border_win, true)
-- vim.cmd(string.format("bdelete! %s", status.prompt_bufnr))
-- Major hack?? Why do I have to od this.
-- Probably because we're currently IN the buffer.
-- Should wait to do this until after we're done.
vim.defer_fn(function()
del_win("prompt_win", prompt_win, true)
end, 10)
state.clear_status(status.prompt_bufnr)
end
local ns_telescope_selection = a.nvim_create_namespace('telescope_selection')
function Picker:get_selection()
return self.selection or self.max_results
end
function Picker:move_selection(change)
self:set_selection(self:get_selection() + change)
end
function Picker:set_selection(row)
if row > self.max_results then
row = self.max_results
elseif row < 1 then
row = 1
end
local status = state.get_status(self.prompt_bufnr)
a.nvim_buf_clear_namespace(status.results_bufnr, ns_telescope_selection, 0, -1)
a.nvim_buf_add_highlight(
status.results_bufnr,
ns_telescope_selection,
'Error',
row,
0,
-1
)
-- TODO: Don't let you go over / under the buffer limits
-- TODO: Make sure you start exactly at the bottom selected
-- TODO: Get row & text in the same obj
self.selection = row
if self.previewer then
self.previewer:preview(
status.preview_win,
status.preview_bufnr,
status.results_bufnr,
row
)
end
end
pickers.new = function(...)
return Picker:new(...)
end
-- TODO: We should consider adding `process_bulk` or `bulk_line_manager` for things
-- that we always know the items and can score quickly, so as to avoid drawing so much.
pickers.line_manager = function(max_results, set_line)
log.debug("Creating line_manager...")
-- state contains list of
-- {
-- score = ...
-- line = ...
-- metadata ? ...
-- }
local state = {}
set_line = set_line or function() end
return setmetatable({
add_result = function(self, score, line)
score = score or 0
for index, item in ipairs(state) do
if item.score > score then
return self:insert(index, {
score = score,
line = line,
})
end
-- Don't add results that are too bad.
if index >= max_results then
return self
end
end
return self:insert({
score = score,
line = line,
})
end,
insert = function(self, index, item)
if item == nil then
item = index
index = #state + 1
end
-- To insert something, we place at the next available index (or specified index)
-- and then shift all the corresponding items one place.
local next_item
repeat
next_item = state[index]
set_line(index, item.line)
state[index] = item
index = index + 1
item = next_item
until not next_item
end,
num_results = function()
return #state
end,
_get_state = function()
return state
end,
}, {
-- insert =
-- __index = function(_, line)
-- end,
-- __newindex = function(_, index, line)
-- end,
})
end
return pickers
|