From c235959fd909d75248c066a781475e207606c5aa Mon Sep 17 00:00:00 2001 From: Chris AtLee Date: Thu, 31 Aug 2023 04:00:24 -0400 Subject: fix(lsp): only disable inlay hints / diagnostics if no other clients are connected (#24535) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the issue where the LspNotify handlers for inlay_hint / diagnostics would end up refreshing all attached clients. The handler would call util._refresh, which called vim.lsp.buf_request, which calls the method on all attached clients. Now util._refresh takes an optional client_id parameter, which is used to specify a specific client to update. This commit also fixes util._refresh's handling of the `only_visible` flag. Previously if `only_visible` was false, two requests would be made to the server: one for the visible region, and one for the entire file. Co-authored-by: Stanislav Asunkin <1353637+stasjok@users.noreply.github.com> Co-authored-by: Mathias Fußenegger --- runtime/lua/vim/lsp/inlay_hint.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/lsp/inlay_hint.lua') diff --git a/runtime/lua/vim/lsp/inlay_hint.lua b/runtime/lua/vim/lsp/inlay_hint.lua index 8407105d47..7b58188c53 100644 --- a/runtime/lua/vim/lsp/inlay_hint.lua +++ b/runtime/lua/vim/lsp/inlay_hint.lua @@ -131,6 +131,16 @@ local function disable(bufnr) end end +--- Refresh inlay hints, only if we have attached clients that support it +---@param bufnr (integer) Buffer handle, or 0 for current +---@param opts? table Additional options to pass to util._refresh +---@private +local function _refresh(bufnr, opts) + opts = opts or {} + opts['bufnr'] = bufnr + util._refresh(ms.textDocument_inlayHint, opts) +end + --- Enable inlay hints for a buffer ---@param bufnr (integer) Buffer handle, or 0 for current local function enable(bufnr) @@ -150,18 +160,18 @@ local function enable(bufnr) return end if bufstates[bufnr] and bufstates[bufnr].enabled then - util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) + _refresh(bufnr, { client_id = opts.data.client_id }) end end, group = augroup, }) - util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) + _refresh(bufnr) api.nvim_buf_attach(bufnr, false, { on_reload = function(_, cb_bufnr) clear(cb_bufnr) if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then bufstates[cb_bufnr].applied = {} - util._refresh(ms.textDocument_inlayHint, { bufnr = cb_bufnr }) + _refresh(cb_bufnr) end end, on_detach = function(_, cb_bufnr) @@ -170,14 +180,22 @@ local function enable(bufnr) }) api.nvim_create_autocmd('LspDetach', { buffer = bufnr, - callback = function() - disable(bufnr) + callback = function(args) + local clients = vim.lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_inlayHint }) + + if + not vim.iter(clients):any(function(c) + return c.id ~= args.data.client_id + end) + then + disable(bufnr) + end end, group = augroup, }) else bufstate.enabled = true - util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr }) + _refresh(bufnr) end end -- cgit