nvim: improve diagnostic severity selection

This commit is contained in:
Fernando Schauenburg 2025-06-29 00:21:45 +02:00
parent bdb6d88fc5
commit 5f98ed1696
3 changed files with 57 additions and 54 deletions

View file

@ -21,63 +21,25 @@ M.hide = function(bufnr) vim.diagnostic.hide(nil, bufnr or 0) end
local icons = require("fschauen.util.icons") local icons = require("fschauen.util.icons")
local diagnostics_opts = function(severity) ---Use `Telescope` to set a new diagnostic severity.
severity = severity or vim.diagnostic.severity.WARN M.set_severity = function()
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 if not pcall(require, "telescope") then
vim.notify("Telescope not available!", vim.log.levels.ERROR) vim.notify("Telescope not available!", vim.log.levels.ERROR)
return return
end end
local display_row = require("telescope.pickers.entry_display").create { local displayer = require("telescope.pickers.entry_display").create {
separator = "", separator = "",
items = { { width = 3 }, { remaining = true } }, items = { { width = 3 }, { width = 3 }, { remaining = true } },
} }
local make_display = function(entry) local make_display = function(entry)
return display_row { local severity = vim.F.npcall(
function() return vim.diagnostic.config().signs.severity.min end
)
local marker = severity == entry.value.severity and icons.ui.Checkbox or ""
return displayer {
{ marker, "Comment" },
{ entry.value.icon, entry.value.highlight }, { entry.value.icon, entry.value.highlight },
{ entry.value.title, entry.value.highlight }, { entry.value.title, entry.value.highlight },
} }
@ -89,6 +51,12 @@ M.select_virtual_text_severity = function()
prompt_title = "Min. severity for virtual text:", prompt_title = "Min. severity for virtual text:",
finder = require("telescope.finders").new_table { finder = require("telescope.finders").new_table {
results = { results = {
{
title = "Off",
severity = 0,
icon = icons.ui.Off,
highlight = "Comment",
},
{ {
title = "Error", title = "Error",
severity = vim.diagnostic.severity.ERROR, severity = vim.diagnostic.severity.ERROR,
@ -124,7 +92,7 @@ M.select_virtual_text_severity = function()
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry() local selection = require("telescope.actions.state").get_selected_entry()
vim.diagnostic.config(diagnostics_opts(selection.value.severity)) M.setup { severity = { min = selection.value.severity } }
end) end)
return true return true
end, end,
@ -133,6 +101,40 @@ M.select_virtual_text_severity = function()
end end
---Customize nvim's diagnostics display. ---Customize nvim's diagnostics display.
M.setup = function() vim.diagnostic.config(diagnostics_opts()) end M.setup = function(opts)
opts = vim.tbl_deep_extend("keep", opts or {}, {
severity = {
min = vim.diagnostic.severity.HINT,
},
})
vim.diagnostic.config {
underline = false,
severity_sort = true,
signs = {
severity = opts.severity,
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostics.Error,
[vim.diagnostic.severity.WARN] = icons.diagnostics.Warn,
[vim.diagnostic.severity.INFO] = icons.diagnostics.Info,
[vim.diagnostic.severity.HINT] = icons.diagnostics.Hint,
},
},
virtual_text = {
severity = opts.severity,
prefix = function(_, index, total) return index == total and "" or "" end,
},
float = {
border = "rounded",
header = { " " .. icons.ui.Diagnostic .. " Diagnostics:", "Comment" },
prefix = function(_, index, _) return string.format("%2d. ", index), "Comment" end,
},
jump = {
severity = opts.severity,
wrap = false,
float = true,
},
}
end
return M return M

View file

@ -94,7 +94,7 @@ M.setup = function()
map("n", "<leader>dd", diagnostic.toggle, { desc = ui.Diagnostic.." [d]iagnostic enable/[d]isable" }) 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>do", diagnostic.open_float, { desc = ui.Diagnostic.." [d]iagnostic [o]pen" })
map("n", "<leader>dh", diagnostic.hide, { desc = ui.Diagnostic.." [d]iagnostic [h]ide" }) map("n", "<leader>dh", diagnostic.hide, { desc = ui.Diagnostic.." [d]iagnostic [h]ide" })
map("n", "<leader>ds", diagnostic.select_virtual_text_severity, { desc = ui.Diagnostic.." [d]iagnostic [s]everity" }) map("n", "<leader>ds", diagnostic.set_severity, { desc = ui.Diagnostic.." [d]iagnostic [s]everity" })
-- toggle quickfix and loclist -- toggle quickfix and loclist
map("n", "<leader>q", window.toggle_quickfix, { desc = ui.Toggle.." toggle quickfix" }) map("n", "<leader>q", window.toggle_quickfix, { desc = ui.Toggle.." toggle quickfix" })

View file

@ -114,6 +114,7 @@ M.ui = {
Markdown = "", Markdown = "",
Modified = "", Modified = "",
Note = "", --  Note = "", -- 
Off = "",
Paste = "", Paste = "",
Play = "", Play = "",
ReadOnly = "", -- "RO", ReadOnly = "", -- "RO",