nvim: lsp: Fix use of diagnostic API

With get_line_diagnostics being deprecated we switched to
vim.diagnostic.get but vim diagnostics are different from
LSP diagnostics and need to be converted.

We copy the code from get_line_diagnostics here as that
will be dropped in 0.12. Strangely the issue only kept
coming up with ruff/Python and HLS/Haskell.

This fixes commit ef0daa5d4 which broke code action
light bulb functionality.
This commit is contained in:
Sanchayan Maity 2024-09-16 23:38:16 +05:30
parent fffb2c06bc
commit 24cd59f124
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3

View file

@ -183,7 +183,7 @@ local client_custom_cleanup = function(client, bufnr)
end
end
local on_attach = function(client, bufnr)
local on_attach = function(client_id, client, bufnr)
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
if client.config.flags then
@ -207,11 +207,18 @@ local on_attach = function(client, bufnr)
group = lsp_augroup_id,
buffer = bufnr,
callback = function()
-- Taken from https://github.com/neovim/nvim-lspconfig/wiki/Code-Actions
local context = { diagnostics = vim.diagnostic.get(bufnr) }
-- Adapted from https://github.com/neovim/nvim-lspconfig/wiki/Code-Actions
-- Copied from deprecated vim.lsp.diagnostic.get_line_diagnostics()
local diag_opts = {} --- @type vim.diagnostic.GetOpts
diag_opts.namespace = vim.lsp.diagnostic.get_namespace(client_id)
diag_opts.lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
local lsp_diagnostics = vim.lsp.diagnostic.from(vim.diagnostic.get(bufnr, diag_opts))
local context = { diagnostics = lsp_diagnostics }
local params = lsp_util.make_range_params()
params.context = context
vim.lsp.buf_request(0, 'textDocument/codeAction', params, function(err, result, ctx, config)
vim.lsp.buf_request(bufnr, 'textDocument/codeAction', params, function(err, result, ctx, config)
if result and not vim.tbl_isempty(result) then
local line = params.range.start.line
code_action_update_sign(vim.b.code_action_line, line + 1, bufnr)
@ -268,7 +275,7 @@ vim.api.nvim_create_autocmd("LspAttach", {
end
end
on_attach(client, bufnr)
on_attach(args.data.client_id, client, bufnr)
end,
})