feat(diagnostic): add enabled filter (#33981)

This commit is contained in:
Maria José Solano
2025-05-13 21:32:00 -05:00
committed by GitHub
parent 40b64e9100
commit 4fae013a21
4 changed files with 46 additions and 0 deletions

View File

@ -440,6 +440,9 @@ Lua module: vim.diagnostic *diagnostic-api*
specified line number.
• {severity}? (`vim.diagnostic.SeverityFilter`) See
|diagnostic-severity|.
• {enabled}? (`boolean`, default: `nil`) Limit diagnostics to only
enabled or disabled. If nil, enablement is ignored. See
|vim.diagnostic.enable()|
*vim.diagnostic.JumpOpts*
Extends: |vim.diagnostic.GetOpts|

View File

@ -129,6 +129,8 @@ DIAGNOSTICS
• |vim.diagnostic.setloclist()| and |vim.diagnostic.setqflist()| now support a
`format` function to modify (or filter) diagnostics before being set in the
location/quickfix list.
• |vim.diagnostic.get()| now accepts an `enabled` filter to only return
enabled or disabled diagnostics.
EDITOR

View File

@ -832,11 +832,24 @@ local function get_diagnostics(bufnr, opts, clamp)
return true
end
---@param b integer
---@param d vim.Diagnostic
local match_enablement = function(d, b)
if opts.enabled == nil then
return true
end
local enabled = M.is_enabled({ bufnr = b, ns_id = d.namespace })
return (enabled and opts.enabled) or (not enabled and not opts.enabled)
end
---@param b integer
---@param d vim.Diagnostic
local function add(b, d)
if
match_severity(d)
and match_enablement(d, b)
and (not opts.lnum or (opts.lnum >= d.lnum and opts.lnum <= (d.end_lnum or d.lnum)))
then
if clamp and api.nvim_buf_is_loaded(b) then
@ -1345,6 +1358,11 @@ end
---
--- See |diagnostic-severity|.
--- @field severity? vim.diagnostic.SeverityFilter
---
--- Limit diagnostics to only enabled or disabled. If nil, enablement is ignored.
--- See |vim.diagnostic.enable()|
--- (default: `nil`)
--- @field enabled? boolean
--- Configuration table with the keys listed below. Some parameters can have their default values
--- changed with |vim.diagnostic.config()|.

View File

@ -1531,6 +1531,29 @@ describe('vim.diagnostic', function()
end)
)
end)
it('allows filtering by enablement', function()
eq(
{ 3, 1, 2 },
exec_lua(function()
vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
_G.make_error('Error 1', 1, 1, 1, 5),
})
vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
_G.make_error('Error 2', 1, 1, 1, 5),
_G.make_error('Error 3', 3, 1, 3, 5),
})
vim.diagnostic.enable(false, { ns_id = _G.other_ns })
return {
#vim.diagnostic.get(_G.diagnostic_bufnr),
#vim.diagnostic.get(_G.diagnostic_bufnr, { enabled = true }),
#vim.diagnostic.get(_G.diagnostic_bufnr, { enabled = false }),
}
end)
)
end)
end)
describe('count', function()