You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
my-nvim-lua/lua/spike/diagnostics.lua

60 lines
1.5 KiB
Lua

local M = {}
M.shown = true
-- toggle diagnostics with show/hide
M.toggle = function()
if M.shown then
M.shown = false
return vim.diagnostic.hide()
end
M.shown = true
vim.diagnostic.show()
end
local orig_diag_virt_handler = vim.diagnostic.handlers.virtual_text
local ns = vim.api.nvim_create_namespace("my_diagnostics")
local filter_diagnostics = function(diagnostics, level)
local filtered_diag = {}
for _, d in ipairs(diagnostics) do
if d.severity <= level then
table.insert(filtered_diag, 1, d)
end
end
return filtered_diag
end
--NOTE: apply diagnostics filter to current buffer / all buffers
M.set_diagnostics_level = function(level)
vim.diagnostic.handlers.virtual_text = {
show = function(_, bufnr, _, opts)
-- get all diagnostics for local buffer
local diagnostics = vim.diagnostic.get(bufnr)
filtered = filter_diagnostics(diagnostics, level)
-- filter diags based on severity
orig_diag_virt_handler.show(ns, bufnr, filtered, opts)
end,
hide = function(_, bufnr)
orig_diag_virt_handler.hide(ns, bufnr)
end
}
bufnr = vim.api.nvim_get_current_buf()
-- hide all diagnostics
vim.diagnostic.hide(nil, bufnr)
local diags = vim.diagnostic.get(bufnr)
if #diags > 0 then
filtered = filter_diagnostics(diags, level)
vim.diagnostic.show(ns, bufnr, filtered)
end
end
return M