From e9623f686590c5a90cbb47daf99cd2ccf2b94df4 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Fri, 27 Jun 2025 21:00:51 +0200 Subject: [PATCH] nvim: fix deprecated `vim.diagnostic` navigation --- config/nvim/lua/fschauen/diagnostic.lua | 23 ++++++++++------------- config/nvim/lua/fschauen/keymap.lua | 4 ++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/config/nvim/lua/fschauen/diagnostic.lua b/config/nvim/lua/fschauen/diagnostic.lua index 2e09957..05339fe 100644 --- a/config/nvim/lua/fschauen/diagnostic.lua +++ b/config/nvim/lua/fschauen/diagnostic.lua @@ -5,16 +5,19 @@ M.severity = vim.diagnostic.severity.WARN -- Go to next/prev diagnostic, but only if next item has a visible virtual text. -- If we can move, then also center screen at target location. -local conditional_goto = function(condition, move, opts) - opts = vim.tbl_extend("keep", opts or {}, { +local conditional_jump = function(count) + vim.validate("count", count, "number") + + local get_diagnostic = count > 0 and vim.diagnostic.get_next or vim.diagnostic.get_prev + local diagnostic = get_diagnostic { wrap = false, -- don't wrap around the begin/end of file severity = { -- only navigate items with visible virtual text min = M.severity, }, - }) + } - if condition(opts) then - move(opts) + if diagnostic then + vim.diagnostic.jump { diagnostic = diagnostic, float = true } vim.cmd("normal zz") else local level = vim.diagnostic.severity[M.severity] or "???" @@ -24,16 +27,10 @@ local conditional_goto = function(condition, move, opts) end ---Move to the next diagnostic. ----@param opts table\nil: options passed along to `vim.diagnostic.goto_next`. -M.goto_next = function(opts) - conditional_goto(vim.diagnostic.get_next_pos, vim.diagnostic.goto_next, opts) -end +M.goto_next = function() conditional_jump(1) end ---Move to the previous diagnostic. ----@param opts table|nil: options passed along to `vim.diagnostic.goto_prev`. -M.goto_prev = function(opts) - conditional_goto(vim.diagnostic.get_prev_pos, vim.diagnostic.goto_prev, opts) -end +M.goto_prev = function() conditional_jump(-1) end ---Show diagnostics in a floating window. ---@param opts table|nil: options passed along to `vim.diagnostic.open_float`. diff --git a/config/nvim/lua/fschauen/keymap.lua b/config/nvim/lua/fschauen/keymap.lua index b06deb6..e327987 100644 --- a/config/nvim/lua/fschauen/keymap.lua +++ b/config/nvim/lua/fschauen/keymap.lua @@ -91,8 +91,8 @@ M.setup = function() local ui = require("fschauen.util.icons").ui -- navigate diagnostics - map("n", "]d", diagnostic.goto_next, { desc = ui.Diagnostic.." [d]iagnostic [n]ext" }) - map("n", "[d", diagnostic.goto_prev, { desc = ui.Diagnostic.." [d]iagnostic [p]revious" }) + map("n", "]d", diagnostic.goto_next, { desc = ui.Diagnostic.." next [d]iagnostic" }) + map("n", "[d", diagnostic.goto_prev, { desc = ui.Diagnostic.." previous [d]iagnostic" }) map("n", "dd", diagnostic.toggle, { desc = ui.Diagnostic.." [d]iagnostic enable/[d]isable" }) map("n", "do", diagnostic.open_float, { desc = ui.Diagnostic.." [d]iagnostic [o]pen" }) map("n", "dh", diagnostic.hide, { desc = ui.Diagnostic.." [d]iagnostic [h]ide" })