commit 4fae013a21a9bb14cec684438ab9bea44c3aee1e
parent 40b64e91007e364fd7c7eaab64ce7c8cf0150aec
Author: Maria José Solano <majosolano99@gmail.com>
Date: Tue, 13 May 2025 21:32:00 -0500
feat(diagnostic): add `enabled` filter (#33981)
Diffstat:
4 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt
@@ -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|
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
@@ -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
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
@@ -834,9 +834,22 @@ local function get_diagnostics(bufnr, opts, clamp)
---@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()|.
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
@@ -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()