From 24cd59f124920432e727b2ed64ff0e05e2e45f52 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Mon, 16 Sep 2024 23:38:16 +0530 Subject: [PATCH] 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. --- nvim/.config/nvim/lua/lsp.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/nvim/.config/nvim/lua/lsp.lua b/nvim/.config/nvim/lua/lsp.lua index b7e5d0d..09f2153 100644 --- a/nvim/.config/nvim/lua/lsp.lua +++ b/nvim/.config/nvim/lua/lsp.lua @@ -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) } - local params = lsp_util.make_range_params() - params.context = context - vim.lsp.buf_request(0, 'textDocument/codeAction', params, function(err, result, ctx, config) + -- 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(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, })