fix diagnostics filter

master
blob42 10 months ago
parent 7e683b2b2c
commit 8646984b4a

@ -17,12 +17,18 @@ end
local orig_diag_virt_handler = vim.diagnostic.handlers.virtual_text local orig_diag_virt_handler = vim.diagnostic.handlers.virtual_text
local ns = vim.api.nvim_create_namespace("my_diagnostics") local ns = vim.api.nvim_create_namespace("my_diagnostics")
local filter_diagnostics = function(diagnostics, level) local filter_diagnostics = function(diagnostics, level, bufnr)
assert(bufnr, "bufnr is required")
local filtered_diag = {} local filtered_diag = {}
if level == -1 then return {} end if level == -1 then return {} end
if not diagnostics then return {} end
for _, d in ipairs(diagnostics) do for _, d in ipairs(diagnostics) do
if d.severity <= level then if d.severity <= level then
table.insert(filtered_diag, 1, d) -- check if diagnostic line is out of range in current buffer
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
if d.end_lnum <= #lines then
table.insert(filtered_diag, 1, d)
end
end end
end end
return filtered_diag return filtered_diag
@ -36,7 +42,7 @@ M.set_diagnostics_level = function(level)
show = function(_, bufnr, _, opts) show = function(_, bufnr, _, opts)
-- get all diagnostics for local buffer -- get all diagnostics for local buffer
local diagnostics = vim.diagnostic.get(bufnr) local diagnostics = vim.diagnostic.get(bufnr)
filtered = filter_diagnostics(diagnostics, level) local filtered = filter_diagnostics(diagnostics, level, bufnr)
-- filter diags based on severity -- filter diags based on severity
orig_diag_virt_handler.show(ns, bufnr, filtered, opts) orig_diag_virt_handler.show(ns, bufnr, filtered, opts)
end, end,
@ -45,13 +51,13 @@ M.set_diagnostics_level = function(level)
end end
} }
bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
-- hide all diagnostics -- hide all diagnostics
vim.diagnostic.hide(nil, bufnr) vim.diagnostic.hide(nil, bufnr)
local diags = vim.diagnostic.get(bufnr) local diags = vim.diagnostic.get(bufnr)
if #diags > 0 then if #diags > 0 then
filtered = filter_diagnostics(diags, level) local filtered = filter_diagnostics(diags, level, bufnr)
vim.diagnostic.show(ns, bufnr, filtered) vim.diagnostic.show(ns, bufnr, filtered)
end end

Loading…
Cancel
Save