Skip to content

Commit 81f270a

Browse files
sudo-teeFrancis Belanger
and
Francis Belanger
authored
Fix highlight errors when lsp crash or stop (#864)
* 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>
1 parent f92fb11 commit 81f270a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,16 @@ require('lazy').setup({
514514
-- When you move your cursor, the highlights will be cleared (the second autocommand).
515515
local client = vim.lsp.get_client_by_id(event.data.client_id)
516516
if client and client.server_capabilities.documentHighlightProvider then
517+
local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = true })
517518
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
518519
buffer = event.buf,
520+
group = highlight_augroup,
519521
callback = vim.lsp.buf.document_highlight,
520522
})
521523

522524
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
523525
buffer = event.buf,
526+
group = highlight_augroup,
524527
callback = vim.lsp.buf.clear_references,
525528
})
526529
end
@@ -537,6 +540,14 @@ require('lazy').setup({
537540
end,
538541
})
539542

543+
vim.api.nvim_create_autocmd('LspDetach', {
544+
group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
545+
callback = function(event)
546+
vim.lsp.buf.clear_references()
547+
vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf }
548+
end,
549+
})
550+
540551
-- LSP servers and clients are able to communicate to each other what features they support.
541552
-- By default, Neovim doesn't support everything that is in the LSP specification.
542553
-- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.

0 commit comments

Comments
 (0)