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
This commit is contained in:
Francis Belanger 2024-04-19 08:23:42 -04:00
parent f5c9fe8e15
commit 4d73020681
1 changed files with 13 additions and 2 deletions

View File

@ -516,12 +516,23 @@ require('lazy').setup({
if client and client.server_capabilities.documentHighlightProvider then
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
buffer = event.buf,
callback = vim.lsp.buf.document_highlight,
callback = function()
if not vim.lsp.get_client_by_id(event.data.client_id) then
vim.lsp.buf.clear_references()
return true
end
vim.lsp.buf.document_highlight()
end,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
buffer = event.buf,
callback = vim.lsp.buf.clear_references,
callback = function()
if not vim.lsp.get_client_by_id(event.data.client_id) then
return true
end
vim.lsp.buf.clear_references()
end,
})
end