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

(cherry picked from commit 7031949be0)
This commit is contained in:
Grzegorz Rozdzialik 2024-08-07 17:28:01 +02:00 committed by Christian Clason
parent ee57bb5a8e
commit 339067ab7e

View File

@ -881,19 +881,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