nvim: modernize diagnostics for neovim 0.11

This commit is contained in:
Fernando Schauenburg 2025-06-27 21:34:31 +02:00
parent e9623f6865
commit c850f7edb9
2 changed files with 45 additions and 61 deletions

View file

@ -1,37 +1,5 @@
local M = {}
-- Show/navigate warning and errors by default.
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_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 diagnostic then
vim.diagnostic.jump { diagnostic = diagnostic, float = true }
vim.cmd("normal zz")
else
local level = vim.diagnostic.severity[M.severity] or "???"
local msg = string.format("No more diagnostics [level: %s]", level)
vim.notify(msg, vim.log.levels.WARN)
end
end
---Move to the next diagnostic.
M.goto_next = function() conditional_jump(1) end
---Move to the previous diagnostic.
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`.
M.open_float = function(opts) vim.diagnostic.open_float(opts) end
@ -53,6 +21,49 @@ M.hide = function(bufnr) vim.diagnostic.hide(nil, bufnr or 0) end
local icons = require("fschauen.util.icons")
local diagnostics_opts = function(severity)
severity = severity or vim.diagnostic.severity.WARN
return {
underline = false,
virtual_text = {
spacing = 6,
prefix = icons.ui.Circle,
severity = {
min = severity,
},
},
virtual_lines = {
severity = {
min = severity,
},
},
jump = {
wrap = false,
severity = {
min = severity,
},
},
float = {
border = "rounded",
},
severity_sort = true,
signs = {
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostics.Error,
[vim.diagnostic.severity.HINT] = icons.diagnostics.Hint,
[vim.diagnostic.severity.INFO] = icons.diagnostics.Info,
[vim.diagnostic.severity.WARN] = icons.diagnostics.Warn,
},
},
}
end
---Select minimum diagnostic severity for which to show virtual text.
M.select_virtual_text_severity = function()
if not pcall(require, "telescope") then
@ -113,12 +124,7 @@ M.select_virtual_text_severity = function()
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
M.severity = selection.value.severity or M.severity
vim.diagnostic.config {
virtual_text = {
severity = { min = M.severity },
},
}
vim.diagnostic.config(diagnostics_opts(selection.value.severity))
end)
return true
end,
@ -127,26 +133,6 @@ M.select_virtual_text_severity = function()
end
---Customize nvim's diagnostics display.
M.setup = function()
vim.diagnostic.config {
underline = false,
virtual_text = {
spacing = 6,
prefix = icons.ui.Circle,
severity = {
min = M.severity,
},
},
float = {
border = "rounded",
},
severity_sort = true,
}
for type, icon in pairs(icons.diagnostics) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
end
M.setup = function() vim.diagnostic.config(diagnostics_opts()) end
return M

View file

@ -91,8 +91,6 @@ M.setup = function()
local ui = require("fschauen.util.icons").ui
-- navigate diagnostics
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", "<leader>dd", diagnostic.toggle, { desc = ui.Diagnostic.." [d]iagnostic enable/[d]isable" })
map("n", "<leader>do", diagnostic.open_float, { desc = ui.Diagnostic.." [d]iagnostic [o]pen" })
map("n", "<leader>dh", diagnostic.hide, { desc = ui.Diagnostic.." [d]iagnostic [h]ide" })