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 else
params = util.make_range_params(win, client.offset_encoding) params = util.make_range_params(win, client.offset_encoding)
end 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_push = vim.lsp.diagnostic.get_namespace(client.id, false)
local ns_pull = vim.lsp.diagnostic.get_namespace(client.id, true) local ns_pull = vim.lsp.diagnostic.get_namespace(client.id, true)
local diagnostics = {} local diagnostics = {}
local lnum = api.nvim_win_get_cursor(0)[1] - 1 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_pull, lnum = lnum }))
vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_push, lnum = lnum })) vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_push, lnum = lnum }))
---@diagnostic disable-next-line: no-unknown params.context = vim.tbl_extend('force', context, {
context.diagnostics = vim.tbl_map(function(d) ---@diagnostic disable-next-line: no-unknown
return d.user_data.lsp diagnostics = vim.tbl_map(function(d)
end, diagnostics) return d.user_data.lsp
end, diagnostics),
})
end end
params.context = context
client.request(ms.textDocument_codeAction, params, on_result, bufnr) client.request(ms.textDocument_codeAction, params, on_result, bufnr)
end end
end end