From 4d73020681e3ea49687f839e9a90dc079441741c Mon Sep 17 00:00:00 2001 From: Francis Belanger Date: Fri, 19 Apr 2024 08:23:42 -0400 Subject: [PATCH 1/4] 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 a182828eebf..bad1ae2347f 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 From f45cec078ceb9e9c3bfc086515addc400087f209 Mon Sep 17 00:00:00 2001 From: Francis Belanger Date: Mon, 22 Apr 2024 08:29:42 -0400 Subject: [PATCH 2/4] Delete the highlight autocommands in the LspDetatch event --- init.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/init.lua b/init.lua index bad1ae2347f..99820df23c9 100644 --- a/init.lua +++ b/init.lua @@ -514,25 +514,17 @@ 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, - 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, + group = highlight_augroup, + callback = vim.lsp.buf.document_highlight, }) vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { buffer = event.buf, - callback = function() - if not vim.lsp.get_client_by_id(event.data.client_id) then - return true - end - vim.lsp.buf.clear_references() - end, + group = highlight_augroup, + callback = vim.lsp.buf.clear_references, }) end @@ -548,6 +540,14 @@ require('lazy').setup({ end, }) + vim.api.nvim_create_autocmd('LspDetach', { + group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }), + callback = function() + vim.lsp.buf.clear_references() + vim.api.nvim_del_augroup_by_name 'kickstart-lsp-highlight' + 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. From a37febf80baeb0391d23099e83ebd6153704962e Mon Sep 17 00:00:00 2001 From: Francis Belanger Date: Mon, 22 Apr 2024 09:19:57 -0400 Subject: [PATCH 3/4] Only delete autocmds for the current buffer with the group name --- init.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 99820df23c9..f587aec12f9 100644 --- a/init.lua +++ b/init.lua @@ -542,9 +542,12 @@ require('lazy').setup({ vim.api.nvim_create_autocmd('LspDetach', { group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }), - callback = function() + callback = function(event) vim.lsp.buf.clear_references() - vim.api.nvim_del_augroup_by_name 'kickstart-lsp-highlight' + local cmds = vim.api.nvim_get_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf } + for _, cmd in ipairs(cmds) do + vim.api.nvim_del_autocmd(cmd.id) + end end, }) From 5ba86c5e6ac7cd1bbbfd8dfb46a39370b7a425bb Mon Sep 17 00:00:00 2001 From: Francis Belanger Date: Mon, 22 Apr 2024 10:15:45 -0400 Subject: [PATCH 4/4] Simplify clearing the autocommands --- init.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/init.lua b/init.lua index f587aec12f9..50e56d52df3 100644 --- a/init.lua +++ b/init.lua @@ -544,10 +544,7 @@ require('lazy').setup({ group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }), callback = function(event) vim.lsp.buf.clear_references() - local cmds = vim.api.nvim_get_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf } - for _, cmd in ipairs(cmds) do - vim.api.nvim_del_autocmd(cmd.id) - end + vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf } end, })