Skip to content

Commit

Permalink
fix: fix failure to resize match listing on VimResized
Browse files Browse the repository at this point in the history
I noticed we're only getting one callback (ie. for the prompt but not
for the match listing). Fix that by not doing a buffer-local autocmd.

Next problem: the `WinClosed` event doesn't fire reliably, or if it
does, `nvim_del_autocmd()` doesn't always work. Not sure which, but it
happens routinely. So, we hack around that with a group that we clear
before bringing up the UI. At worst, a lingering autocmd is going to
hang around until the next time the user uses Command-T.
  • Loading branch information
wincent committed Nov 9, 2024
1 parent e739fa7 commit 46f8723
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lua/wincent/commandt/private/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ ui.show = function(finder, options)

current_window = vim.api.nvim_get_current_win()

-- Work around an autocommand bug. We don't reliably get `WinClosed` events,
-- or if we do, our call to `nvim_del_autocmd()` doesn't always clean up for
-- us. So, we add some window-related autocommands to a group which we always
-- reset every time we show a new UI.
vim.api.nvim_create_augroup('CommandTWindow', { clear = true })

match_listing = MatchListing.new({
border = options.match_listing.border,
height = options.height,
Expand Down
11 changes: 8 additions & 3 deletions lua/wincent/commandt/private/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function Window.new(options)
_on_resize = options.on_resize,
_padded_title = options.title ~= '' and (' ' .. options.title .. ' ') or '',
_prompt = options.prompt,
_resize_autocmd = nil,
_selection_highlight = options.selection_highlight,
_title = options.title,
_title_buffer = nil,
Expand Down Expand Up @@ -246,15 +247,15 @@ function Window:show()
callback = callback,
})
end
vim.api.nvim_create_autocmd('VimResized', {
buffer = self._main_buffer,
self._resize_autocmd = vim.api.nvim_create_autocmd('VimResized', {
callback = function()
-- This will reposition title, too, so no need for a separate autocmd.
-- One autocmd will handle both title and main repositioning.
self:_reposition()
if self._on_resize then
self._on_resize()
end
end,
group = vim.api.nvim_create_augroup('CommandTWindow', { clear = false }),
})
vim.api.nvim_create_autocmd('BufWipeout', {
buffer = self._main_buffer,
Expand Down Expand Up @@ -296,6 +297,10 @@ function Window:show()
vim.api.nvim_win_close(self._title_window, true)
self._title_window = nil
end
if self._resize_autocmd ~= nil then
vim.api.nvim_del_autocmd(self._resize_autocmd)
self._resize_autocmd = nil
end
if self._on_close then
self._on_close()
end
Expand Down

0 comments on commit 46f8723

Please sign in to comment.