nvim/diagnostic: clobber repeated diagnostic floats

Due to the way we render `vim.diagnostic.open_float/1` on every `CursorHold`
event, this window will open over other floats that a user (may have)
opened, particularly `keys.Buffer.Lsp.N.ShowDocumentation`, which can be
frustrating.

This commit changes the behaviour to open the diagnostic float once per
cursor location, as this is probably what the user wants for the most
part. It is still possible for a user to run into the scenario where
they move to a diagnostic and open a float only for it to be replaced
on the `CursorHold`, but this should be much more infrequent.

Credit to github.com/wookayin for explaining how to do this.
See:
  wookayin/dotfiles@ee687b036c
  /nvim/lua/config/lsp.lua
  #L249-L263
This commit is contained in:
Paul Stemmet 2022-10-08 14:53:05 +00:00
parent 8d00216396
commit 9e6a96c5b0
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 14 additions and 1 deletions

View File

@ -11,6 +11,19 @@ local sign = function(o)
}) })
end end
local DiagnosticFloat = function()
-- current, last diagnostic cursor position
local current = vim.api.nvim_win_get_cursor(0)
local last = vim.w.diagnostics_last_cursor or { nil, nil }
-- Show the popup diagnostics window,
-- but only once for the current cursor location (unless moved afterwards).
if not (current[1] == last[1] and current[2] == last[2]) then
vim.w.diagnostics_last_cursor = current
D.open_float({ focusable = false, scope = 'cursor' })
end
end
function M.config() function M.config()
-- Setup signs -- Setup signs
sign({ name = 'DiagnosticSignError', text = '' }) sign({ name = 'DiagnosticSignError', text = '' })
@ -44,7 +57,7 @@ end
function M.autocmds() function M.autocmds()
au.PsoxDiagnosticHover { au.PsoxDiagnosticHover {
{ 'CursorHold', '*', function() D.open_float({ focusable = false, scope = 'cursor' }) end } { 'CursorHold', '*', DiagnosticFloat }
} }
end end