dotfiles/config/nvim/lua/config/diagnostic.lua

140 lines
4.3 KiB
Lua

local M = {}
---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
---Toggle diagnostics in the given buffer.
---@param bufnr integer|nil: Buffer number (0 for current buffer, nil for all buffers.
M.toggle = function(bufnr)
local filter = { bufnr = bufnr or 0 }
if vim.diagnostic.is_enabled(filter) then
vim.diagnostic.enable(false, filter)
else
vim.diagnostic.enable(true, filter)
end
end
---Hide currently displayed diagnostics.
---@param bufnr integer|nil: Buffer number (0 for current buffer, nil for all buffers.
M.hide = function(bufnr) vim.diagnostic.hide(nil, bufnr or 0) end
local icons = require("util.icons")
---Use `Telescope` to set a new diagnostic severity.
M.set_severity = function()
if not pcall(require, "telescope") then
vim.notify("Telescope not available!", vim.log.levels.ERROR)
return
end
local displayer = require("telescope.pickers.entry_display").create {
separator = "",
items = { { width = 3 }, { width = 3 }, { remaining = true } },
}
local make_display = function(entry)
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.title, entry.value.highlight },
}
end
local opts = require("telescope.themes").get_dropdown()
require("telescope.pickers")
.new(opts, {
prompt_title = "Min. severity for virtual text:",
finder = require("telescope.finders").new_table {
results = {
{
title = "Off",
severity = 0,
icon = icons.ui.Off,
highlight = "Comment",
},
{
title = "Error",
severity = vim.diagnostic.severity.ERROR,
icon = icons.diagnostics_bold.Error,
highlight = "DiagnosticError",
},
{
title = "Warning",
severity = vim.diagnostic.severity.WARN,
icon = icons.diagnostics_bold.Warn,
highlight = "DiagnosticWarn",
},
{
title = "Info",
severity = vim.diagnostic.severity.INFO,
icon = icons.diagnostics_bold.Info,
highlight = "DiagnosticInfo",
},
{
title = "Hint",
severity = vim.diagnostic.severity.HINT,
icon = icons.diagnostics_bold.Hint,
highlight = "DiagnosticHint",
},
},
entry_maker = function(entry)
return { value = entry, ordinal = entry.title, display = make_display }
end,
},
sorter = require("telescope.config").values.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
require("telescope.actions.set").select:replace(function()
local selection = require("telescope.actions.state").get_selected_entry()
require("telescope.actions").close(prompt_bufnr)
M.setup { severity = { min = selection.value.severity } }
end)
return true -- Attach default mappings as well.
end,
})
:find()
end
---Customize nvim's diagnostics display.
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