Skip to content

Commit 5df29cb

Browse files
committed
WIP: recalculate picker layout on VimResized
- implements `Picker:recalculate_layout` function - adds an autocommand to call this function on the `VimResized` event - relies on nvim-lua/plenary.nvim#180 and nvim-lua/popup.nvim#15 Currently has issues when the results window increases in height
1 parent 38907ce commit 5df29cb

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

lua/telescope/pickers.lua

+53
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,13 @@ function Picker:find()
441441
[[ autocmd BufLeave <buffer> ++nested ++once :silent lua require('telescope.pickers').on_close_prompt(%s)]],
442442
prompt_bufnr)
443443

444+
local on_vim_resize = string.format(
445+
[[ autocmd VimResized <buffer> ++nested :silent lua require('telescope.pickers').on_resize_window(%s)]],
446+
prompt_bufnr)
444447
vim.cmd([[augroup PickerInsert]])
445448
vim.cmd([[ au!]])
446449
vim.cmd( on_buf_leave)
450+
vim.cmd( on_vim_resize)
447451
vim.cmd([[augroup END]])
448452

449453
self.prompt_bufnr = prompt_bufnr
@@ -483,6 +487,39 @@ function Picker:find()
483487
end
484488
end
485489

490+
function Picker:recalculate_layout()
491+
local line_count = vim.o.lines - vim.o.cmdheight
492+
if vim.o.laststatus ~= 0 then
493+
line_count = line_count - 1
494+
end
495+
496+
local popup_opts = self:get_window_options(vim.o.columns, line_count)
497+
-- `popup.nvim` massaging so people don't have to remember minheight shenanigans
498+
popup_opts.results.minheight = popup_opts.results.height
499+
popup_opts.prompt.minheight = popup_opts.prompt.height
500+
if popup_opts.preview then
501+
popup_opts.preview.minheight = popup_opts.preview.height
502+
end
503+
504+
local status = state.get_status(self.prompt_bufnr)
505+
local prompt_win = status.prompt_win
506+
local results_win = status.results_win
507+
local preview_win = status.preview_win
508+
popup.resize(results_win, popup_opts.results)
509+
510+
511+
if popup_opts.preview then
512+
popup.resize(preview_win, popup_opts.preview)
513+
end
514+
515+
popup.resize(prompt_win, popup_opts.prompt)
516+
517+
-- Temporarily disabled: Draw the screen ASAP. This makes things feel speedier.
518+
-- vim.cmd [[redraw]]
519+
520+
self.max_results = popup_opts.results.height
521+
end
522+
486523
function Picker:hide_preview()
487524
-- 1. Hide the window (and border)
488525
-- 2. Resize prompt & results windows accordingly
@@ -1137,6 +1174,22 @@ function pickers.on_close_prompt(prompt_bufnr)
11371174
picker.close_windows(status)
11381175
end
11391176

1177+
function pickers.on_resize_window(prompt_bufnr)
1178+
print('resizing'..prompt_bufnr)
1179+
local status = state.get_status(prompt_bufnr)
1180+
local picker = status.picker
1181+
1182+
-- if picker.sorter then
1183+
-- picker.sorter:_destroy()
1184+
-- end
1185+
1186+
-- if picker.previewer then
1187+
-- picker.previewer:teardown()
1188+
-- end
1189+
1190+
picker:recalculate_layout()
1191+
end
1192+
11401193
--- Get the prompt text without the prompt prefix.
11411194
function Picker:_get_prompt()
11421195
return vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix + 1)

0 commit comments

Comments
 (0)