From 4d73020681e3ea49687f839e9a90dc079441741c Mon Sep 17 00:00:00 2001 From: Francis Belanger Date: Fri, 19 Apr 2024 08:23:42 -0400 Subject: [PATCH] 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 --- init.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index a182828e..bad1ae23 100644 --- a/init.lua +++ b/init.lua @@ -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