From 7031949be065870f0daf74a9f1be3df47f83312c Mon Sep 17 00:00:00 2001 From: Grzegorz Rozdzialik Date: Wed, 7 Aug 2024 17:28:01 +0200 Subject: fix(lsp): avoid reusing diagnostics from different servers in actions (#30002) Problem: When preparing the parameters for a code actions LSP request, the code set `context.diagnostics` when processing the first LSP client, and then reused those `context.diagnostics` for subsequent LSP clients. This meant that the second and next LSP clients got diagnostics that did not originate from them, and they did not get the diagnostics that they sent. Solution: Avoid setting `context.diagnostics` (which is referenced by all clients). Instead, set `params.context.diagnostics` directly, which is specific to a single client. Fixes #30001 Caused by #29501 --- runtime/lua/vim/lsp/buf.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index a512d48a06..d7463d74f8 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -897,19 +897,23 @@ function M.code_action(opts) else params = util.make_range_params(win, client.offset_encoding) end - if not context.diagnostics then + if context.diagnostics then + params.context = context + else local ns_push = vim.lsp.diagnostic.get_namespace(client.id, false) local ns_pull = vim.lsp.diagnostic.get_namespace(client.id, true) local diagnostics = {} local lnum = api.nvim_win_get_cursor(0)[1] - 1 vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_pull, lnum = lnum })) vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_push, lnum = lnum })) - ---@diagnostic disable-next-line: no-unknown - context.diagnostics = vim.tbl_map(function(d) - return d.user_data.lsp - end, diagnostics) + params.context = vim.tbl_extend('force', context, { + ---@diagnostic disable-next-line: no-unknown + diagnostics = vim.tbl_map(function(d) + return d.user_data.lsp + end, diagnostics), + }) end - params.context = context + client.request(ms.textDocument_codeAction, params, on_result, bufnr) end end -- cgit