-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathhandler.lua
More file actions
567 lines (508 loc) · 15.2 KB
/
handler.lua
File metadata and controls
567 lines (508 loc) · 15.2 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
---@class BqfPreviewHandler
local M = {}
local api = vim.api
local fn = vim.fn
local cmd = vim.cmd
local autoPreview
local clicked
local shouldPreviewCallback
local keepPreview, origPos
local bufLabel
local lastIdx
local config = require('bqf.config')
local qfs = require('bqf.qfwin.session')
local pvs = require('bqf.preview.session')
local ts = require('bqf.preview.treesitter')
local utils = require('bqf.utils')
local debounce = require('bqf.lib.debounce')
local function execPreview(item, lspRangeHl, patternHl)
local lnum, col, pattern = item.lnum, item.col, item.pattern
if lnum < 1 then
api.nvim_win_set_cursor(0, {1, 0})
if pattern ~= '' then
fn.search(pattern, 'c')
end
elseif not pcall(api.nvim_win_set_cursor, 0, {lnum, math.max(0, col - 1)}) then
return
end
origPos = api.nvim_win_get_cursor(0)
-- scrolling horizontally reset
cmd('norm! ze')
fn.clearmatches()
local posList = {}
if lspRangeHl and not vim.tbl_isempty(lspRangeHl) then
posList = utils.lspRangeToPosList(lspRangeHl)
elseif patternHl and patternHl ~= '' then
posList = utils.patternToPosList(patternHl)
else
local endLnum, endCol = item.end_lnum, item.end_col
posList = utils.qfRangeToPosList(lnum, col, endLnum, endCol)
end
if not vim.tbl_isempty(posList) then
utils.matchAddPos('BqfPreviewRange', posList)
else
if lnum < 1 then
fn.matchadd('BqfPreviewRange', pattern)
elseif col < 1 then
utils.matchAddPos('BqfPreviewRange', {{lnum}})
end
end
if lnum > 0 then
utils.matchAddPos('BqfPreviewCursor', {{lnum, math.max(1, col)}}, 11)
end
end
---
---@param qwinid? number
---@param skipValid? boolean
---@return BqfPreviewSession?
local function previewSession(qwinid, skipValid)
qwinid = qwinid or api.nvim_get_current_win()
local ps = pvs:get(qwinid)
if not skipValid then
return ps and ps:validate() and ps or nil
else
return ps
end
end
local function detectFileType(bufnr, fbufnr)
local name = fn.fnameescape(api.nvim_buf_get_name(bufnr))
if utils.has08() then
return vim.filetype.match({
filename = name,
buf = fbufnr,
})
end
local ft
local eiBak = vim.o.ei
local ok, res = pcall(api.nvim_buf_call, fbufnr, function()
vim.o.ei = 'FileType'
cmd(('do filetypedetect BufRead %s'):format(name))
return vim.bo.ft
end)
vim.o.ei = eiBak
if ok then
ft = res
end
return ft
end
local function doSyntax(qwinid)
local ps = previewSession(qwinid)
if not ps or not ps.bufnr or ps.syntax then
return
end
local ft
local fbufnr = ps:floatBufnr()
local loaded = utils.isBufLoaded(ps.bufnr)
if loaded then
ft = vim.bo[ps.bufnr].ft
else
-- https://github.com/nvim-treesitter/nvim-treesitter/issues/898
-- fuxx min.js!
local lcount = api.nvim_buf_line_count(fbufnr)
local bytes = api.nvim_buf_get_offset(fbufnr, lcount)
-- bytes / lcount < 500 LGTM :)
if bytes / lcount < 500 then
ft = detectFileType(ps.bufnr, fbufnr)
end
end
if type(ft) == 'string' then
ps.syntax = ts.attach(ps.bufnr, fbufnr, ft)
if not ps.syntax then
vim.bo[fbufnr].syntax = ft
ps.syntax = true
end
end
end
local function showCountLabel(qlist, idx)
local items = qlist:items()
local curBufnr = items[idx].bufnr
local lo, hi = idx, idx
for i = idx - 1, 1, -1 do
if items[i].bufnr == curBufnr then
lo = i
else
break
end
end
for i = idx + 1, #items do
if items[i].bufnr == curBufnr then
hi = i
else
break
end
end
local cur = idx - lo + 1
local cnt = hi - lo + 1
pvs:showCountLabel(('[%d/%d]'):format(cur, cnt), 'BqfPreviewBufLabel')
end
local function createWinResizeEvent()
if fn.exists('##WinResized') == 1 and fn.exists('#BqfPreview#WinResized') == 0 then
cmd([[au BqfPreview WinResized * lua require('bqf.preview.handler').qfResized()]])
else
cmd([[au BqfPreview VimResized <buffer> lua require('bqf.preview.handler').redrawWin()]])
end
end
local function destroyWinResizeEvent()
if fn.exists('##WinResized') == 1 then
cmd('au! BqfPreview WinResized *')
else
cmd('au! BqfPreview VimResized *')
end
end
function M.qfResized()
local wins = vim.v.event.windows
local curWinid = api.nvim_get_current_win()
for _, winid in ipairs(wins) do
if curWinid == winid then
local qs = qfs:get(winid)
if qs then
M.redrawWin(winid)
end
end
end
end
function M.redrawWin(qwinid)
if not qwinid then
local bufnr = tonumber(fn.expand('<abuf>')) or api.nvim_get_current_buf()
qwinid = fn.bufwinid(bufnr)
end
if utils.isWinValid(qwinid) then
local ps = previewSession(qwinid, true)
M.close(qwinid)
if ps then
M.open(qwinid)
end
end
end
function M.autoEnabled()
return autoPreview
end
function M.keepPreview()
keepPreview = true
end
function M.toggleMode(qwinid)
local ps = previewSession(qwinid)
if not ps then
return
end
ps.full = ps.full ~= true
lastIdx = -1
M.open(qwinid, nil, true)
end
---
---@param qwinid? number
function M.close(qwinid)
if keepPreview then
keepPreview = nil
return
end
lastIdx = -1
ts.shrinkCache()
qwinid = qwinid or api.nvim_get_current_win()
local ps = previewSession(qwinid, true)
if ps then
ps:close()
ps.bufnr = nil
end
destroyWinResizeEvent()
end
---
---@param qwinid number
---@param qidx? number
---@param force? boolean
function M.open(qwinid, qidx, force)
qwinid = qwinid or api.nvim_get_current_win()
local ps = previewSession(qwinid, true)
if not ps or api.nvim_tabpage_list_wins(0) == 1 then
return
end
local qs = qfs:get(qwinid)
local pwinid = qs:previousWinid()
qidx = qidx or api.nvim_win_get_cursor(qwinid)[1]
if not force and qidx == lastIdx or fn.win_gettype(pwinid) ~= '' then
return
end
lastIdx = qidx
local qlist = qs:list()
local item = qlist:item(qidx)
if not item then
M.close(qwinid)
return
end
local pbufnr = item.bufnr
if pbufnr == 0 or not api.nvim_buf_is_valid(pbufnr) then
M.close(qwinid)
return
end
if ps.bufnr ~= pbufnr and not force and shouldPreviewCallback and
not shouldPreviewCallback(pbufnr, qwinid) then
M.close(qwinid)
return
end
local loaded = utils.isBufLoaded(pbufnr)
local size = qlist:getQfList({size = 0}).size
local fbufnr
ps:display(pwinid, pbufnr, qidx, size, function()
fbufnr = ps:floatBufnr()
if force or ps.bufnr ~= pbufnr then
ps:floatBufReset()
ts.disableActive(fbufnr)
ps:transferBuf(pbufnr)
ps.syntax = ts.tryAttach(pbufnr, fbufnr, loaded)
end
end)
if not ps.bufnr or not fbufnr then
return
end
if not ps.syntax then
M.doSyntax(qwinid)
end
local ctx = qlist:context().bqf or {}
local lspRangeHlList, patternHl = ctx.lsp_ranges_hl, ctx.pattern_hl
local lspRangeHl
if type(lspRangeHlList) == 'table' then
lspRangeHl = lspRangeHlList[qidx]
end
ps:floatWinExec(function()
execPreview(item, lspRangeHl, patternHl)
utils.zz()
ps:scroll(loaded)
end)
if bufLabel then
if size < 1000 or qlist:itemsCached() then
showCountLabel(qlist, qidx)
else
vim.defer_fn(function()
if utils.isWinValid(qwinid) and qlist.id == qfs:get(qwinid):list().id and
qidx == api.nvim_win_get_cursor(qwinid)[1] then
showCountLabel(qlist, qidx)
end
end, 50)
end
end
createWinResizeEvent()
end
---
---@param direction number
---@param qwinid? number
function M.scroll(direction, qwinid)
qwinid = qwinid or api.nvim_get_current_win()
local ps = previewSession(qwinid)
if ps and direction then
ps:floatWinExec(function()
if direction == 0 then
api.nvim_win_set_cursor(0, origPos)
else
-- ^D = 0x04, ^U = 0x15
cmd(('norm! %c'):format(direction > 0 and 0x04 or 0x15))
end
utils.zz()
ps:scroll()
end)
end
end
---
---@param qwinid? number
function M.toggle(qwinid)
qwinid = qwinid or api.nvim_get_current_win()
local ps = previewSession(qwinid, true)
if not ps then
return
end
autoPreview = autoPreview ~= true
if autoPreview then
utils.warn('Enable preview automatically')
M.open(qwinid)
else
utils.warn('Disable preview automatically')
M.close(qwinid)
end
end
---
---@param qwinid? number
---@return boolean
function M.showWindow(qwinid)
local res = false
qwinid = qwinid or api.nvim_get_current_win()
if not previewSession(qwinid) then
M.open(qwinid, nil, true)
res = true
end
return res
end
---
---@param qwinid? number
---@return boolean
function M.hideWindow(qwinid)
local res = false
qwinid = qwinid or api.nvim_get_current_win()
if previewSession(qwinid) then
M.close(qwinid)
res = true
end
return res
end
function M.toggleWindow(qwinid)
qwinid = qwinid or api.nvim_get_current_win()
if previewSession(qwinid) then
M.close(qwinid)
else
M.open(qwinid, nil, true)
end
end
function M.moveCursor()
local qwinid = api.nvim_get_current_win()
local ps = previewSession(qwinid, true)
if not ps then
return
end
if autoPreview then
M.open(qwinid)
else
if api.nvim_win_get_cursor(qwinid)[1] ~= lastIdx then
M.close(qwinid)
end
end
end
local function checkClicked()
fn.getchar()
local winid = vim.v.mouse_winid
clicked = pvs:floatWinid() == winid
return winid
end
function M.clicked()
local res = clicked
clicked = false
return res
end
function M.mouseClick(mode)
local clickedWinid = checkClicked()
local clickedBufnr = api.nvim_win_get_buf(clickedWinid)
local qfBufClicked = api.nvim_get_option_value('bt', {buf = clickedBufnr}) == 'quickfix'
if not clicked then
if not qfBufClicked then
api.nvim_set_current_win(clickedWinid)
end
cmd(('norm! %dgg%d|'):format(vim.v.mouse_lnum, vim.v.mouse_col))
else
if mode == 't' then
cmd('startinsert')
end
end
end
function M.mouseDoubleClick(mode)
local clickedWinid = checkClicked()
local clickedBufnr = api.nvim_win_get_buf(clickedWinid)
local qfBufClicked = api.nvim_get_option_value('bt', {buf = clickedBufnr}) == 'quickfix'
if api.nvim_get_current_win() == clickedWinid then
if qfBufClicked then
-- ^M = 0x0d
cmd(('norm %c'):format(0x0d))
else
cmd(('norm! %c'):format(0x0d))
end
elseif clicked then
if mode == 't' then
cmd('startinsert')
api.nvim_feedkeys(('%c'):format(0x0d), 'it', false)
else
local lnum, vcol = vim.v.mouse_lnum, vim.v.mouse_col
local col
if utils.has08() then
col = math.max(0, fn.virtcol2col(clickedWinid, lnum, vcol) - 1)
end
cmd(('norm %c'):format(0x0d))
if col then
api.nvim_win_set_cursor(0, {lnum, col})
else
cmd(('keepj norm! %dgg%d|'):format(lnum, vcol))
end
utils.zz()
end
end
end
function M.initialize(qwinid)
cmd([[
aug BqfPreview
au! * <buffer>
au CursorMoved,WinEnter <buffer> lua require('bqf.preview.handler').moveCursor()
au WinLeave,BufWipeout,BufHidden <buffer> lua require('bqf.preview.handler').close()
aug END
]])
local mouseEnabled = vim.o.mouse:match('[na]') ~= nil
pvs:new(qwinid, mouseEnabled)
-- some plugins will change the quickfix window, preview window should init later
vim.defer_fn(function()
lastIdx = -1
-- delayed called, qwinid maybe invalid
if not utils.isWinValid(qwinid) then
return
end
if mouseEnabled then
local qbufnr = api.nvim_win_get_buf(qwinid)
api.nvim_buf_set_keymap(qbufnr, '', '<LeftMouse>',
[[<Cmd>lua require('bqf.preview.handler').mouseClick()<CR>]],
{nowait = true, noremap = false, desc = 'Preview the item under the cursor'})
api.nvim_buf_set_keymap(qbufnr, 'n', '<2-LeftMouse>',
[[<Cmd>lua require('bqf.preview.handler').mouseDoubleClick()<CR>]],
{nowait = true, noremap = false, desc = 'Open the item under the cursor'})
end
if autoPreview and api.nvim_get_current_win() == qwinid then
M.open(qwinid)
end
end, 50)
end
local function init()
local pconf = config.preview
vim.validate({preview = {pconf, 'table'}})
local delaySyntax = tonumber(pconf.delay_syntax)
autoPreview = pconf.auto_preview
shouldPreviewCallback = pconf.should_preview_cb
local wrap, winblend = pconf.wrap, pconf.winblend
local showTitle, showScrollBar = pconf.show_title, pconf.show_scroll_bar
local winHeight = tonumber(pconf.win_height)
local winVHeight = tonumber(pconf.win_vheight or winHeight)
bufLabel = pconf.buf_label
vim.validate({
auto_preview = {autoPreview, 'boolean'},
delay_syntax = {delaySyntax, 'number'},
should_preview_cb = {shouldPreviewCallback, 'function', true},
wrap = {wrap, 'boolean'},
show_title = {showTitle, 'boolean'},
show_scroll_bar = {showScrollBar, 'boolean'},
win_height = {winHeight, 'number'},
win_vheight = {winVHeight, 'number'},
winblend = {winblend, 'number'},
buf_label = {bufLabel, 'boolean'}
})
pvs:initialize({
border = pconf.border,
wrap = wrap,
showTitle = showTitle,
showScrollBar = showScrollBar,
winHeight = winHeight,
winVHeight = winVHeight,
winblend = winblend
})
cmd([[
hi default link BqfPreviewFloat Normal
hi default link BqfPreviewBorder FloatBorder
hi default link BqfPreviewTitle Title
hi default link BqfPreviewThumb PmenuThumb
hi default link BqfPreviewSbar PmenuSbar
hi default link BqfPreviewCursor Cursor
hi default link BqfPreviewCursorLine CursorLine
hi default link BqfPreviewRange IncSearch
hi default link BqfPreviewBufLabel BqfPreviewRange
aug BqfPreview
au!
aug END
]])
clicked = false
-- Damn it! someone wants to disable syntax :(
-- https://github.com/kevinhwang91/nvim-bqf/issues/89
---@diagnostic disable-next-line: unused-local
M.doSyntax = delaySyntax >= 0 and debounce(doSyntax, delaySyntax) or function(qwinid) end
end
init()
return M