-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.lua
More file actions
134 lines (119 loc) · 3.41 KB
/
main.lua
File metadata and controls
134 lines (119 loc) · 3.41 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
---@class BqfMain
local M = {}
local api = vim.api
local fn = vim.fn
local cmd = vim.cmd
local qfs = require('bqf.qfwin.session')
local preview = require('bqf.preview.handler')
local layout = require('bqf.layout')
local magicwin = require('bqf.magicwin.handler')
local keymap = require('bqf.keymap')
function M.toggle()
if vim.w.bqf_enabled then
M.disable()
else
M.enable()
end
end
function M.enable()
-- need after vim-patch:8.1.0877
if not layout.validQfWin() then
return false
end
local qwinid = api.nvim_get_current_win()
local qs = qfs:new(qwinid)
assert(qs, 'It is not a quickfix window')
local qlist = qs:list()
if qlist:changedtick() == 0 and vim.w.bqf_enabled then
return
end
vim.wo.nu, vim.wo.rnu = true, false
vim.wo.wrap = false
vim.wo.foldenable, vim.wo.foldcolumn = false, '0'
vim.wo.signcolumn = 'number'
local adjustHeightCallback = layout.initialize(qwinid)
preview.initialize(qwinid)
keymap.initialize()
local pwinid = qs:previousWinid()
cmd([[
aug Bqf
au! * <buffer>
au WinEnter <buffer> ++nested lua if require('bqf.config').auto_close then require('bqf.main').killAloneQf() end
au WinClosed <buffer> ++nested lua require('bqf.main').closeQf()
au WinLeave <buffer> lua require('bqf.main').saveWinView()
aug END
]])
-- TODO
-- After WinClosed callback in magic window, WinClosed in main can't be fired.
-- WinClosed event in magic window must after in main
magicwin.attach(qwinid, pwinid, nil, adjustHeightCallback)
vim.w.bqf_enabled = true
return true
end
function M.disable()
if vim.bo.buftype ~= 'quickfix' then
return false
end
vim.w.bqf_enabled = false
local qwinid = api.nvim_get_current_win()
preview.close(qwinid)
keymap.dispose()
cmd('au! Bqf')
cmd('sil! au! BqfPreview * <buffer>')
cmd('sil! au! BqfFilterFzf * <buffer>')
cmd('sil! au! BqfMagicWin')
qfs:dispose()
return true
end
local function close(winid)
local ok, msg = pcall(api.nvim_win_close, winid, false)
if not ok then
-- Vim:E444: Cannot close last window
---@diagnostic disable-next-line: need-check-nil
if msg:match('^Vim:E444') then
local function closeLastWin()
cmd('new')
api.nvim_win_close(winid, true)
end
-- after nvim 0.7+ Vim:E242 Can't split a window while closing another
if not pcall(closeLastWin) then
-- less redraw
cmd('noa enew')
local bufnr = api.nvim_get_current_buf()
vim.schedule(function()
closeLastWin()
cmd('noa bw ' .. bufnr)
end)
end
end
end
end
function M.saveWinView()
local winid = api.nvim_get_current_win()
qfs:saveWinView(winid)
end
function M.killAloneQf()
local winid = api.nvim_get_current_win()
local qs = qfs:get(winid)
if qs then
if qs:previousWinid() < 0 then
close(winid)
end
end
end
function M.closeQf()
local winid = tonumber(fn.expand('<afile>'))
if winid and api.nvim_win_is_valid(winid) then
qfs:dispose()
preview.close(winid)
end
end
local function init()
cmd([[
aug Bqf
au!
aug END
]])
end
init()
return M