Skip to content

Commit 8784eeb

Browse files
committed
fix(preview): adjust qf window height for preview (#126)
1 parent a906a9d commit 8784eeb

1 file changed

Lines changed: 46 additions & 13 deletions

File tree

lua/bqf/preview/handler.lua

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,49 @@ local function showCountLabel(qlist, idx)
135135
pvs:showCountLabel(('[%d/%d]'):format(cur, cnt), 'BqfPreviewBufLabel')
136136
end
137137

138+
local function createWinResizeEvent()
139+
if fn.exists('##WinResized') == 1 and fn.exists('#BqfPreview#WinResized') == 0 then
140+
cmd([[au BqfPreview WinResized * lua require('bqf.preview.handler').qfResized()]])
141+
else
142+
cmd([[au BqfPreview VimResized <buffer> lua require('bqf.preview.handler').redrawWin()]])
143+
end
144+
end
145+
146+
local function destroyWinResizeEvent()
147+
if fn.exists('##WinResized') == 1 then
148+
cmd('au! BqfPreview WinResized *')
149+
else
150+
cmd('au! BqfPreview VimResized *')
151+
end
152+
end
153+
154+
function M.qfResized()
155+
local wins = vim.v.event.windows
156+
local curWinid = api.nvim_get_current_win()
157+
for _, winid in ipairs(wins) do
158+
if curWinid == winid then
159+
local qs = qfs:get(winid)
160+
if qs then
161+
M.redrawWin(winid)
162+
end
163+
end
164+
end
165+
end
166+
167+
function M.redrawWin(qwinid)
168+
if not qwinid then
169+
local bufnr = tonumber(fn.expand('<abuf>')) or api.nvim_get_current_buf()
170+
qwinid = fn.bufwinid(bufnr)
171+
end
172+
if utils.isWinValid(qwinid) then
173+
local preview = pvs.validate()
174+
M.close(qwinid)
175+
if preview then
176+
M.open(qwinid)
177+
end
178+
end
179+
end
180+
138181
function M.autoEnabled()
139182
return autoPreview
140183
end
@@ -172,6 +215,7 @@ function M.close(qwinid)
172215
if ps then
173216
ps.bufnr = nil
174217
end
218+
destroyWinResizeEvent()
175219
end
176220

177221
---
@@ -261,6 +305,8 @@ function M.open(qwinid, qidx, force)
261305
end, 50)
262306
end
263307
end
308+
309+
createWinResizeEvent()
264310
end
265311

266312
---
@@ -349,18 +395,6 @@ function M.moveCursor()
349395
end
350396
end
351397

352-
function M.redrawWin()
353-
local bufnr = tonumber(fn.expand('<abuf>')) or api.nvim_get_current_buf()
354-
local qwinid = fn.bufwinid(bufnr)
355-
if utils.isWinValid(qwinid) then
356-
local preview = pvs.validate()
357-
M.close(qwinid)
358-
if preview then
359-
M.open(qwinid)
360-
end
361-
end
362-
end
363-
364398
local function checkClicked()
365399
fn.getchar()
366400
local winid = vim.v.mouse_winid
@@ -416,7 +450,6 @@ function M.initialize(qwinid)
416450
cmd([[
417451
aug BqfPreview
418452
au! * <buffer>
419-
au VimResized <buffer> lua require('bqf.preview.handler').redrawWin()
420453
au CursorMoved,WinEnter <buffer> lua require('bqf.preview.handler').moveCursor()
421454
au WinLeave,BufWipeout,BufHidden <buffer> lua require('bqf.preview.handler').close()
422455
aug END

0 commit comments

Comments
 (0)