1

While it may not be the only culprit, coc.nvim is a bit notorious for leaving popups hanging around the screen long after they should be cleared. Sometimes, as in the gif below, dangling popups will disappear when the next is instantiated, but in rarer cases, they will hang around for the remainder of the nvim session, which behavior can sometimes necessitate closing and restarting nvim entirely with obvious attendant loss in productivity and organization.

nagging popups

small side note for anyone paying close attention: the popup incorrectly stating that my_first_initial is defined as a space is a problem with the rust language server and not relevant to this discussion

vim provides popup_clear as an "[e]mergency solution to a misbehaving plugin", but nvim for some reason does not provide the same functionality. Can someone please point me to an nvim solution for clearing misbehaving popups from the screen?

EDIT: I stumbled across winnr and wincmd in another context and realized that winnr('$').'wincmd c', i.e. close the last window, is a fairly reliable way to do this, provided of course that a popup is open, and also that it is the last window, which it consistently seems to be.

Can anyone expand on this? Is the assumption that an open popup is the last window valid? Is there a more disciplined way to find the correct window to close (and do nothing if there is no popup open)?

1 Answer 1

2

The api-floatwin documentation has this helpful tidbit of information:

To close [a floating window] use |nvim_win_close()| or a command such as |:close|.

To check whether a window is floating, check whether the relative option in its config is non-empty:

if vim.api.nvim_win_get_config(window_id).relative ~= '' then
  -- window with this window_id is floating
end

With this in hand, it's possible to create a user command to manually close all floating windows like so.

vim.api.nvim_create_user_command('CloseFloatingWindows', function(opts)
    for _, window_id in ipairs(vim.api.nvim_list_wins()) do
        -- If window is floating
        if vim.api.nvim_win_get_config(window_id).relative ~= '' then
            -- Force close if called with !
            vim.api.nvim_win_close(window_id, opts.bang)
        end
    end
end, { bang = true, nargs = 0 })

vim.api.nvim_tabpage_list_wins(0) can be used in place of vim.api.nvim_list_wins() to only close floats on the current tab page, which might be preferable since plugins rarely leave orphaned popups across multiple tab pages.

1
  • I'd actually figured this out in the context of focusing on the floating window but forgotten about having asked the question. Thanks for the excellent answer! For anyone curious, the focus operation would be let c = nvim_win_get_config(window_id) then check if c.focusable && !empty(c.relative) and finally exec win_id2win(window_id) . 'wincmd w'. Nov 11, 2022 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.