Skip to content

Commit

Permalink
fix(lsp): ignore hover and signatureHelp responses on buffer change (#…
Browse files Browse the repository at this point in the history
…21121)

Language servers can take some time to respond to the
`textDocument/hover` and `textDocument/signatureHelp` messages. During
that time, the user could have already moved to another buffer. The
popup was always shown in the current buffer, which could be a different
one than the buffer for which the request was sent.

This was particularly annoying when moving to a buffer with a `BufLeave`
autocmd, as that autocmd was triggered when the hover popup was shown
for the original buffer.

Ignoring the response from these 2 messages if they are for a buffer
that is not the current one leads to less noise. The popup will only be
shown for the buffer for which it was requested.

A more robust solution could involve cancelling the hover/signatureHelp
request if the buffer changes so the language server can free its
resources. It could be implemented in the future.
  • Loading branch information
Gelio committed Nov 19, 2022
1 parent 7c57f06 commit cfdf5e6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions runtime/lua/vim/lsp/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ end
function M.hover(_, result, ctx, config)
config = config or {}
config.focus_id = ctx.method
if api.nvim_get_current_buf() ~= ctx.bufnr then
-- Ignore result since buffer changed. This happens for slow language servers.
return
end
if not (result and result.contents) then
vim.notify('No information available')
return
Expand Down Expand Up @@ -408,6 +412,10 @@ M['textDocument/implementation'] = location_handler
function M.signature_help(_, result, ctx, config)
config = config or {}
config.focus_id = ctx.method
if api.nvim_get_current_buf() ~= ctx.bufnr then
-- Ignore result since buffer changed. This happens for slow language servers.
return
end
-- When use `autocmd CompleteDone <silent><buffer> lua vim.lsp.buf.signature_help()` to call signatureHelp handler
-- If the completion item doesn't have signatures It will make noise. Change to use `print` that can use `<silent>` to ignore
if not (result and result.signatures and result.signatures[1]) then
Expand Down

2 comments on commit cfdf5e6

@jmbuhr
Copy link
Contributor

@jmbuhr jmbuhr commented on cfdf5e6 Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way of making this opt-in or opt-out on a case-by-case basis? I wrote a plugin that does LSP things for embedded code chunks by creating hidden buffers with only the code and forwarding lps requests back and forth (https://github.com/jmbuhr/otter.nvim). This commit breaks the hover functionality because the request really comes from a different buffer, but this is intentional.

See quarto-dev/quarto-nvim#32

@jmbuhr
Copy link
Contributor

@jmbuhr jmbuhr commented on cfdf5e6 Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worked around this with jmbuhr/otter.nvim#19

Please sign in to comment.