Skip to content

Commit

Permalink
Fix highlight errors when lsp crash or stop (nvim-lua#864)
Browse files Browse the repository at this point in the history
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
  • Loading branch information
2 people authored and real-mielofon committed Apr 26, 2024
1 parent 68f4053 commit 6267ec5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,16 @@ require('lazy').setup({
-- When you move your cursor, the highlights will be cleared (the second autocommand).
local client = vim.lsp.get_client_by_id(event.data.client_id)
if client and client.server_capabilities.documentHighlightProvider then
local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = true })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
buffer = event.buf,
group = highlight_augroup,
callback = vim.lsp.buf.document_highlight,
})

vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
buffer = event.buf,
group = highlight_augroup,
callback = vim.lsp.buf.clear_references,
})
end
Expand Down Expand Up @@ -555,6 +558,14 @@ require('lazy').setup({
end,
})

vim.api.nvim_create_autocmd('LspDetach', {
group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
callback = function(event)
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf }
end,
})

-- LSP servers and clients are able to communicate to each other what features they support.
-- By default, Neovim doesn't support everything that is in the LSP specification.
-- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.
Expand Down

0 comments on commit 6267ec5

Please sign in to comment.