local M = {}
M.severity = {
ERROR = 1,
WARN = 2,
INFO = 3,
HINT = 4,
}
vim.tbl_add_reverse_lookup(M.severity)
-- Mappings from qflist/loclist error types to severities
M.severity.E = M.severity.ERROR
M.severity.W = M.severity.WARN
M.severity.I = M.severity.INFO
M.severity.N = M.severity.HINT
local global_diagnostic_options = {
signs = true,
underline = true,
virtual_text = true,
update_in_insert = false,
severity_sort = false,
}
-- Local functions {{{
---@private
local function to_severity(severity)
return type(severity) == 'string' and M.severity[string.upper(severity)] or severity
end
---@private
local function filter_by_severity(severity, diagnostics)
if not severity then
return diagnostics
end
if type(severity) ~= "table" then
severity = to_severity(severity)
return vim.tbl_filter(function(t) return t.severity == severity end, diagnostics)
end
local min_severity = to_severity(severity.min) or M.severity.HINT
local max_severity = to_severity(severity.max) or M.severity.ERROR
return vim.tbl_filter(function(t) return t.severity <= min_severity and t.severity >= max_severity end, diagnostics)
end
---@private
local function prefix_source(source, diagnostics)
vim.validate { source = {source, function(v)
return v == "always" or v == "if_many"
end, "Invalid value for option 'source'" } }
if source == "if_many" then
local sources = {}
for _, d in pairs(diagnostics) do
if d.source then
sources[d.source] = true
end
end
if #vim.tbl_keys(sources) <= 1 then
return diagnostics
end
end
return vim.tbl_map(function(d)
if not d.source then
return d
end
local t = vim.deepcopy(d)
t.message = string.format("%s: %s", d.source, d.message)
return t
end, diagnostics)
end
---@private
local function reformat_diagnostics(format, diagnostics)
vim.validate {
format = {format, 'f'},
diagnostics = {diagnostics, 't'},
}
local formatted = vim.deepcopy(diagnostics)
for _, diagnostic in ipairs(formatted) do
diagnostic.message = format(diagnostic)
end
return formatted
end
---@private
local function resolve_optional_value(option, namespace, bufnr)
local enabled_val = {}
if not option then
return false
elseif option == true then
return enabled_val
elseif type(option) == 'function' then
local val = option(namespace, bufnr)
if val == true then
return enabled_val
else
return val
end
elseif type(option) == 'table' then
return option
else
error("Unexpected option type: " .. vim.inspect(option))
end
end
local all_namespaces = {}
---@private
local function get_namespace(ns)
if not all_namespaces[ns] then
local name
for k, v in pairs(vim.api.nvim_get_namespaces()) do
if ns == v then
name = k
break
end
end
if not name then
return vim.notify("namespace does not exist or is anonymous", vim.log.levels.ERROR)
end
all_namespaces[ns] = {
name = name,
sign_group = string.format("vim.diagnostic.%s", name),
opts = {}
|