feat(diagnostic): enable(…, opts)

Problem:
vim.diagnostic.enable() does not match the signature of vim.lsp.inlay_hint.enable()

Solution:
- Change the signature so that the first 2 args are (bufnr, enable).
- Introduce a 3rd `opts` arg.
    - Currently it only supports `opts.ns_id`.
This commit is contained in:
Justin M. Keyes 2024-04-15 18:35:59 +02:00
parent 26765e8461
commit 5ed9916a28
5 changed files with 128 additions and 51 deletions

View File

@ -372,11 +372,13 @@ Use existing common {verb} names (actions) if possible:
- create: Creates a new (non-trivial) thing (TODO: rename to "def"?) - create: Creates a new (non-trivial) thing (TODO: rename to "def"?)
- del: Deletes a thing (or group of things) - del: Deletes a thing (or group of things)
- detach: Dispose attached listener (TODO: rename to "un"?) - detach: Dispose attached listener (TODO: rename to "un"?)
- enable: Enables/disables functionality.
- eval: Evaluates an expression - eval: Evaluates an expression
- exec: Executes code - exec: Executes code
- fmt: Formats - fmt: Formats
- get: Gets things (often by a query) - get: Gets things (often by a query)
- inspect: Presents a high-level, often interactive, view - inspect: Presents a high-level, often interactive, view
- is_enabled: Checks if functionality is enabled.
- open: Opens something (a buffer, window, …) - open: Opens something (a buffer, window, …)
- parse: Parses something into a structured form - parse: Parses something into a structured form
- set: Sets a thing (or group of things) - set: Sets a thing (or group of things)

View File

@ -367,6 +367,13 @@ Lua module: vim.diagnostic *diagnostic-api*
• {user_data}? (`any`) arbitrary data plugins can add • {user_data}? (`any`) arbitrary data plugins can add
• {namespace}? (`integer`) • {namespace}? (`integer`)
*vim.diagnostic.Filter*
Extends: |vim.diagnostic.Opts|
Fields: ~
• {ns_id}? (`integer`) Namespace
*vim.diagnostic.GetOpts* *vim.diagnostic.GetOpts*
A table with the following keys: A table with the following keys:
@ -616,7 +623,7 @@ count({bufnr}, {opts}) *vim.diagnostic.count()*
(`table`) Table with actually present severity values as keys (see (`table`) Table with actually present severity values as keys (see
|diagnostic-severity|) and integer counts as values. |diagnostic-severity|) and integer counts as values.
enable({bufnr}, {namespace}, {enable}) *vim.diagnostic.enable()* enable({bufnr}, {enable}, {opts}) *vim.diagnostic.enable()*
Enables or disables diagnostics. Enables or disables diagnostics.
To "toggle", pass the inverse of `is_enabled()`: >lua To "toggle", pass the inverse of `is_enabled()`: >lua
@ -624,11 +631,12 @@ enable({bufnr}, {namespace}, {enable}) *vim.diagnostic.enable()*
< <
Parameters: ~ Parameters: ~
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer, or • {bufnr} (`integer?`) Buffer number, or 0 for current buffer, or
`nil` for all buffers. `nil` for all buffers.
• {namespace} (`integer?`) Only for the given namespace, or `nil` for • {enable} (`boolean?`) true/nil to enable, false to disable
all. • {opts} (`vim.diagnostic.Filter?`) Filter by these opts, or `nil`
• {enable} (`boolean?`) true/nil to enable, false to disable for all. Only `ns_id` is supported, currently. See
|vim.diagnostic.Filter|.
fromqflist({list}) *vim.diagnostic.fromqflist()* fromqflist({list}) *vim.diagnostic.fromqflist()*
Convert a list of quickfix items to a list of diagnostics. Convert a list of quickfix items to a list of diagnostics.

View File

@ -421,7 +421,8 @@ The following changes to existing APIs or features add new behavior.
• |vim.diagnostic.get()| and |vim.diagnostic.count()| accept multiple • |vim.diagnostic.get()| and |vim.diagnostic.count()| accept multiple
namespaces rather than just a single namespace. namespaces rather than just a single namespace.
• |vim.diagnostic.enable()| accepts an `enabled` boolean parameter. • |vim.diagnostic.enable()| gained new parameters, and the old signature is
deprecated.
• Extmarks now fully support multi-line ranges, and a single extmark can be • Extmarks now fully support multi-line ranges, and a single extmark can be
used to highlight a range of arbitrary length. The |nvim_buf_set_extmark()| used to highlight a range of arbitrary length. The |nvim_buf_set_extmark()|

View File

@ -239,6 +239,9 @@ local M = {}
--- whole line the sign is placed in. --- whole line the sign is placed in.
--- @field linehl? table<vim.diagnostic.Severity,string> --- @field linehl? table<vim.diagnostic.Severity,string>
--- @class vim.diagnostic.Filter : vim.diagnostic.Opts
--- @field ns_id? integer Namespace
--- @nodoc --- @nodoc
--- @enum vim.diagnostic.Severity --- @enum vim.diagnostic.Severity
M.severity = { M.severity = {
@ -1538,13 +1541,7 @@ end
--- @deprecated use `vim.diagnostic.is_enabled()` --- @deprecated use `vim.diagnostic.is_enabled()`
function M.is_disabled(bufnr, namespace) function M.is_disabled(bufnr, namespace)
vim.deprecate( vim.deprecate('vim.diagnostic.is_disabled()', 'vim.diagnostic.is_enabled()', '0.12', nil, false)
'Defining diagnostic signs with :sign-define or sign_define()',
'vim.diagnostic.is_enabled()',
'0.12',
nil,
false
)
return not M.is_enabled(bufnr, namespace) return not M.is_enabled(bufnr, namespace)
end end
@ -1591,7 +1588,7 @@ function M.show(namespace, bufnr, diagnostics, opts)
return return
end end
if M.is_disabled(bufnr, namespace) then if not M.is_enabled(bufnr, namespace) then
return return
end end
@ -1942,7 +1939,7 @@ function M.disable(bufnr, namespace)
nil, nil,
false false
) )
M.enable(bufnr, namespace, false) M.enable(bufnr, false, { ns_id = namespace })
end end
--- Enables or disables diagnostics. --- Enables or disables diagnostics.
@ -1954,17 +1951,49 @@ end
--- ``` --- ```
--- ---
--- @param bufnr integer? Buffer number, or 0 for current buffer, or `nil` for all buffers. --- @param bufnr integer? Buffer number, or 0 for current buffer, or `nil` for all buffers.
--- @param namespace integer? Only for the given namespace, or `nil` for all.
--- @param enable (boolean|nil) true/nil to enable, false to disable --- @param enable (boolean|nil) true/nil to enable, false to disable
function M.enable(bufnr, namespace, enable) --- @param opts vim.diagnostic.Filter? Filter by these opts, or `nil` for all. Only `ns_id` is
--- supported, currently.
function M.enable(bufnr, enable, opts)
opts = opts or {}
if type(enable) == 'number' then
-- Legacy signature.
vim.deprecate(
'vim.diagnostic.enable(buf:number, namespace)',
'vim.diagnostic.enable(buf:number, enable:boolean, opts)',
'0.12',
nil,
false
)
opts.ns_id = enable
enable = true
end
vim.validate({ vim.validate({
bufnr = { bufnr, 'n', true }, bufnr = { bufnr, 'n', true },
namespace = { namespace, 'n', true }, enable = {
enable = { enable, 'b', true }, enable,
function(o)
return o == nil or type(o) == 'boolean' or type(o) == 'number'
end,
'boolean or number (deprecated)',
},
opts = {
opts,
function(o)
return o == nil
or (
type(o) == 'table'
-- TODO(justinmk): support other `vim.diagnostic.Filter` fields.
and (vim.tbl_isempty(o) or vim.deep_equal(vim.tbl_keys(o), { 'ns_id' }))
)
end,
'vim.diagnostic.Filter table (only ns_id is supported currently)',
},
}) })
enable = enable == nil and true or enable enable = enable == nil and true or enable
if bufnr == nil then if bufnr == nil then
if namespace == nil then if opts.ns_id == nil then
diagnostic_disabled = ( diagnostic_disabled = (
enable enable
-- Enable everything by setting diagnostic_disabled to an empty table. -- Enable everything by setting diagnostic_disabled to an empty table.
@ -1978,12 +2007,12 @@ function M.enable(bufnr, namespace, enable)
}) })
) )
else else
local ns = M.get_namespace(namespace) local ns = M.get_namespace(opts.ns_id)
ns.disabled = not enable ns.disabled = not enable
end end
else else
bufnr = get_bufnr(bufnr) bufnr = get_bufnr(bufnr)
if namespace == nil then if opts.ns_id == nil then
diagnostic_disabled[bufnr] = (not enable) and true or nil diagnostic_disabled[bufnr] = (not enable) and true or nil
else else
if type(diagnostic_disabled[bufnr]) ~= 'table' then if type(diagnostic_disabled[bufnr]) ~= 'table' then
@ -1993,14 +2022,14 @@ function M.enable(bufnr, namespace, enable)
diagnostic_disabled[bufnr] = {} diagnostic_disabled[bufnr] = {}
end end
end end
diagnostic_disabled[bufnr][namespace] = (not enable) and true or nil diagnostic_disabled[bufnr][opts.ns_id] = (not enable) and true or nil
end end
end end
if enable then if enable then
M.show(namespace, bufnr) M.show(opts.ns_id, bufnr)
else else
M.hide(namespace, bufnr) M.hide(opts.ns_id, bufnr)
end end
end end

View File

@ -329,7 +329,7 @@ describe('vim.diagnostic', function()
eq( eq(
{ 1, 1, 2, 0, 2 }, { 1, 1, 2, 0, 2 },
exec_lua [[ exec_lua [[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false) vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
return { return {
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns),
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns),
@ -344,7 +344,7 @@ describe('vim.diagnostic', function()
eq( eq(
all_highlights, all_highlights,
exec_lua([[ exec_lua([[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns) vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
return { return {
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns),
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns),
@ -371,7 +371,7 @@ describe('vim.diagnostic', function()
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags)
vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags)
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false) vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
return { return {
count_extmarks(diagnostic_bufnr, diagnostic_ns), count_extmarks(diagnostic_bufnr, diagnostic_ns),
@ -383,8 +383,8 @@ describe('vim.diagnostic', function()
eq( eq(
{ 4, 0 }, { 4, 0 },
exec_lua [[ exec_lua [[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns) vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
vim.diagnostic.enable(diagnostic_bufnr, other_ns, false) vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = other_ns })
return { return {
count_extmarks(diagnostic_bufnr, diagnostic_ns), count_extmarks(diagnostic_bufnr, diagnostic_ns),
@ -478,7 +478,7 @@ describe('vim.diagnostic', function()
end) end)
describe('enable() and disable()', function() describe('enable() and disable()', function()
it('works without arguments', function() it('without arguments', function()
local result = exec_lua [[ local result = exec_lua [[
vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
@ -500,7 +500,7 @@ describe('vim.diagnostic', function()
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.enable(nil, nil, false) vim.diagnostic.enable(nil, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
@ -532,7 +532,7 @@ describe('vim.diagnostic', function()
eq(4, result[4]) eq(4, result[4])
end) end)
it('works with only a buffer argument', function() it('with buffer argument', function()
local result = exec_lua [[ local result = exec_lua [[
local other_bufnr = vim.api.nvim_create_buf(true, false) local other_bufnr = vim.api.nvim_create_buf(true, false)
@ -561,7 +561,7 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.enable(diagnostic_bufnr, nil, false) vim.diagnostic.enable(diagnostic_bufnr, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
@ -573,7 +573,7 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.enable(other_bufnr, nil, false) vim.diagnostic.enable(other_bufnr, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
@ -588,7 +588,7 @@ describe('vim.diagnostic', function()
eq(3, result[4]) eq(3, result[4])
end) end)
it('works with only a namespace argument', function() it('with a namespace argument', function()
local result = exec_lua [[ local result = exec_lua [[
vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
@ -610,17 +610,17 @@ describe('vim.diagnostic', function()
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.enable(nil, diagnostic_ns, false) vim.diagnostic.enable(nil, false, { ns_id = diagnostic_ns })
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.enable(nil, diagnostic_ns) vim.diagnostic.enable(nil, true, { ns_id = diagnostic_ns })
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.enable(nil, other_ns, false) vim.diagnostic.enable(nil, false, { ns_id = other_ns })
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns)) count_extmarks(diagnostic_bufnr, other_ns))
@ -634,8 +634,11 @@ describe('vim.diagnostic', function()
eq(2, result[4]) eq(2, result[4])
end) end)
it('works with both a buffer and a namespace argument', function() --- @return table
local result = exec_lua [[ local function test_enable(legacy)
local result = exec_lua(
[[
local legacy = ...
local other_bufnr = vim.api.nvim_create_buf(true, false) local other_bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(0, diagnostic_bufnr) vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
@ -663,34 +666,68 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false) if legacy then
vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns)
else
vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
end
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.enable(diagnostic_bufnr, other_ns, false) if legacy then
vim.diagnostic.disable(diagnostic_bufnr, other_ns)
else
vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = other_ns })
end
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns) if legacy then
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns)
else
vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
end
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
-- Should have no effect if legacy then
vim.diagnostic.enable(other_bufnr, other_ns, false) -- Should have no effect
vim.diagnostic.disable(other_bufnr, other_ns)
else
-- Should have no effect
vim.diagnostic.enable(other_bufnr, false, { ns_id = other_ns })
end
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) + count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns)) count_extmarks(other_bufnr, diagnostic_ns))
return result return result
]] ]],
legacy
)
return result
end
it('with both buffer and namespace arguments', function()
local result = test_enable(false)
eq(4, result[1])
eq(2, result[2])
eq(1, result[3])
eq(3, result[4])
eq(3, result[5])
end)
it('with both buffer and namespace arguments (deprecated signature)', function()
-- Exercise the legacy/deprecated signature.
local result = test_enable(true)
eq(4, result[1]) eq(4, result[1])
eq(2, result[2]) eq(2, result[2])
eq(1, result[3]) eq(1, result[3])
@ -1742,7 +1779,7 @@ describe('vim.diagnostic', function()
eq( eq(
0, 0,
exec_lua [[ exec_lua [[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false) vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
}) })
@ -1753,7 +1790,7 @@ describe('vim.diagnostic', function()
eq( eq(
2, 2,
exec_lua [[ exec_lua [[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns) vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
return count_extmarks(diagnostic_bufnr, diagnostic_ns) return count_extmarks(diagnostic_bufnr, diagnostic_ns)
]] ]]
) )
@ -2719,7 +2756,7 @@ describe('vim.diagnostic', function()
make_error('Diagnostic #1', 1, 1, 1, 1), make_error('Diagnostic #1', 1, 1, 1, 1),
}) })
vim.api.nvim_set_current_buf(diagnostic_bufnr) vim.api.nvim_set_current_buf(diagnostic_bufnr)
vim.diagnostic.enable(nil, nil, false) vim.diagnostic.enable(nil, false)
return { return {
vim.diagnostic.is_enabled(), vim.diagnostic.is_enabled(),
vim.diagnostic.is_enabled(diagnostic_bufnr), vim.diagnostic.is_enabled(diagnostic_bufnr),