diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index 2f44ca8843..f59510bea5 100644 --- 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 index b2673a17a4..0058be7f2f 100644 --- 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 index ed80cf14bf..c771d374ba 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -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()|. diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 6ea87d3817..1657941342 100644 --- 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()