feat(diagnostic): is_enabled, enable(…, enable:boolean)

Problem:
`vim.diagnostic.is_disabled` and `vim.diagnostic.disable` are unnecessary
and inconsistent with the "toggle" pattern (established starting with
`vim.lsp.inlay_hint`, see https://github.com/neovim/neovim/pull/25512#pullrequestreview-1676750276

As a reminder, the rationale is:
- we always need `enable()`
- we always end up needing `is_enabled()`
- "toggle" can be achieved via `enable(not is_enabled())`
- therefore,
    - `toggle()` and `disable()` are redundant
    - `is_disabled()` is a needless inconsistency

Solution:
- Introduce `vim.diagnostic.is_enabled`, and `vim.diagnostic.enable(…, enable:boolean)`
    - Note: Future improvement would be to add an `enable()` overload `enable(enable:boolean, opts: table)`.
- Deprecate `vim.diagnostic.is_disabled`, `vim.diagnostic.disable`
This commit is contained in:
Justin M. Keyes 2024-04-08 00:41:41 +02:00
parent 57adf8c6e0
commit 26765e8461
7 changed files with 154 additions and 96 deletions

View File

@ -80,12 +80,15 @@ HIGHLIGHTS
- *hl-VertSplit* Use |hl-WinSeparator| instead.
LSP DIAGNOSTICS
- *vim.diagnostic.disable()* Use |vim.diagnostic.enable()|
- *vim.diagnostic.is_disabled()* Use |vim.diagnostic.is_enabled()|
For each of the functions below, use the corresponding function in
|vim.diagnostic| instead (unless otherwise noted). For example, use
|vim.diagnostic.get()| instead of |vim.lsp.diagnostic.get()|.
- *vim.lsp.diagnostic.clear()* Use |vim.diagnostic.hide()| instead.
- *vim.lsp.diagnostic.disable()*
- *vim.lsp.diagnostic.disable()* Use |vim.diagnostic.enable()| instead.
- *vim.lsp.diagnostic.display()* Use |vim.diagnostic.show()| instead.
- *vim.lsp.diagnostic.enable()*
- *vim.lsp.diagnostic.get()*

View File

@ -383,9 +383,12 @@ Use existing common {verb} names (actions) if possible:
- try_{verb}: Best-effort operation, failure returns null or error obj
Do NOT use these deprecated verbs:
- disable: Prefer `enable(enable: boolean)`.
- is_disabled: Prefer `is_enabled()`.
- list: Redundant with "get"
- show: Redundant with "print", "echo"
- notify: Redundant with "print", "echo"
- show: Redundant with "print", "echo"
- toggle: Prefer `enable(not is_enabled())`.
Use consistent names for {noun} (nouns) in API functions: buffer is called
"buf" everywhere, not "buffer" in some places and "buf" in others.

View File

@ -616,23 +616,19 @@ count({bufnr}, {opts}) *vim.diagnostic.count()*
(`table`) Table with actually present severity values as keys (see
|diagnostic-severity|) and integer counts as values.
disable({bufnr}, {namespace}) *vim.diagnostic.disable()*
Disable diagnostics in the given buffer.
enable({bufnr}, {namespace}, {enable}) *vim.diagnostic.enable()*
Enables or disables diagnostics.
To "toggle", pass the inverse of `is_enabled()`: >lua
vim.diagnostic.enable(0, not vim.diagnostic.is_enabled())
<
Parameters: ~
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer. When
omitted, disable diagnostics in all buffers.
• {namespace} (`integer?`) Only disable diagnostics for the given
namespace.
enable({bufnr}, {namespace}) *vim.diagnostic.enable()*
Enable diagnostics in the given buffer.
Parameters: ~
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer. When
omitted, enable diagnostics in all buffers.
• {namespace} (`integer?`) Only enable diagnostics for the given
namespace.
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer, or
`nil` for all buffers.
• {namespace} (`integer?`) Only for the given namespace, or `nil` for
all.
• {enable} (`boolean?`) true/nil to enable, false to disable
fromqflist({list}) *vim.diagnostic.fromqflist()*
Convert a list of quickfix items to a list of diagnostics.
@ -733,7 +729,7 @@ hide({namespace}, {bufnr}) *vim.diagnostic.hide()*
diagnostics, use |vim.diagnostic.reset()|.
To hide diagnostics and prevent them from re-displaying, use
|vim.diagnostic.disable()|.
|vim.diagnostic.enable()|.
Parameters: ~
• {namespace} (`integer?`) Diagnostic namespace. When omitted, hide
@ -741,14 +737,16 @@ hide({namespace}, {bufnr}) *vim.diagnostic.hide()*
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer. When
omitted, hide diagnostics in all buffers.
is_disabled({bufnr}, {namespace}) *vim.diagnostic.is_disabled()*
Check whether diagnostics are disabled in a given buffer.
is_enabled({bufnr}, {namespace}) *vim.diagnostic.is_enabled()*
Check whether diagnostics are enabled.
Note: ~
• This API is pre-release (unstable).
Parameters: ~
• {bufnr} (`integer?`) Buffer number, or 0 for current buffer.
• {namespace} (`integer?`) Diagnostic namespace. When omitted, checks
if all diagnostics are disabled in {bufnr}. Otherwise,
only checks if diagnostics from {namespace} are disabled.
• {namespace} (`integer?`) Diagnostic namespace, or `nil` for all
diagnostics in {bufnr}.
Return: ~
(`boolean`)

View File

@ -162,7 +162,7 @@ The following new APIs or features were added.
• |vim.diagnostic| now supports LSP DiagnosticsTag.
See: https://microsoft.github.io/language-server-protocol/specification/#diagnosticTag
|vim.diagnostic.is_disabled()| checks if diagnostics are disabled in a given
• vim.diagnostic.is_disabled() checks if diagnostics are disabled in a given
buffer or namespace.
• Treesitter captures can now be transformed by directives. This will allow

View File

@ -328,6 +328,8 @@ The following new APIs and features were added.
|vim.diagnostic.get()| when only the number of diagnostics is needed, but
not the diagnostics themselves.
• Introduced |vim.diagnostic.is_enabled()|
• |vim.deepcopy()| has a `noref` argument to avoid hashing table values.
• Terminal buffers emit a |TermRequest| autocommand event when the child
@ -419,6 +421,8 @@ The following changes to existing APIs or features add new behavior.
• |vim.diagnostic.get()| and |vim.diagnostic.count()| accept multiple
namespaces rather than just a single namespace.
• |vim.diagnostic.enable()| accepts an `enabled` boolean parameter.
• 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()|
API function already allowed you to define such ranges, but highlight regions
@ -520,6 +524,10 @@ release.
- |nvim_win_get_option()| Use |nvim_get_option_value()| instead.
- |nvim_win_set_option()| Use |nvim_set_option_value()| instead.
• vim.diagnostic functions:
- |vim.diagnostic.disable()|
- |vim.diagnostic.is_disabled()|
• vim.lsp functions:
- |vim.lsp.util.get_progress_messages()| Use |vim.lsp.status()| instead.
- |vim.lsp.get_active_clients()| Use |vim.lsp.get_clients()| instead.

View File

@ -1492,7 +1492,7 @@ end
--- diagnostics, use |vim.diagnostic.reset()|.
---
--- To hide diagnostics and prevent them from re-displaying, use
--- |vim.diagnostic.disable()|.
--- |vim.diagnostic.enable()|.
---
---@param namespace integer? Diagnostic namespace. When omitted, hide
--- diagnostics from all namespaces.
@ -1517,25 +1517,35 @@ function M.hide(namespace, bufnr)
end
end
--- Check whether diagnostics are disabled in a given buffer.
--- Check whether diagnostics are enabled.
---
---@param bufnr integer? Buffer number, or 0 for current buffer.
---@param namespace integer? Diagnostic namespace. When omitted, checks if
--- all diagnostics are disabled in {bufnr}.
--- Otherwise, only checks if diagnostics from
--- {namespace} are disabled.
---@return boolean
function M.is_disabled(bufnr, namespace)
--- @param bufnr integer? Buffer number, or 0 for current buffer.
--- @param namespace integer? Diagnostic namespace, or `nil` for all diagnostics in {bufnr}.
--- @return boolean
--- @since 12
function M.is_enabled(bufnr, namespace)
bufnr = get_bufnr(bufnr)
if namespace and M.get_namespace(namespace).disabled then
return true
return false
end
if type(diagnostic_disabled[bufnr]) == 'table' then
return diagnostic_disabled[bufnr][namespace]
return not diagnostic_disabled[bufnr][namespace]
end
return diagnostic_disabled[bufnr] ~= nil
return diagnostic_disabled[bufnr] == nil
end
--- @deprecated use `vim.diagnostic.is_enabled()`
function M.is_disabled(bufnr, namespace)
vim.deprecate(
'Defining diagnostic signs with :sign-define or sign_define()',
'vim.diagnostic.is_enabled()',
'0.12',
nil,
false
)
return not M.is_enabled(bufnr, namespace)
end
--- Display diagnostics for the given namespace and buffer.
@ -1923,71 +1933,75 @@ function M.setloclist(opts)
set_list(true, opts)
end
--- Disable diagnostics in the given buffer.
---
---@param bufnr integer? Buffer number, or 0 for current buffer. When
--- omitted, disable diagnostics in all buffers.
---@param namespace integer? Only disable diagnostics for the given namespace.
--- @deprecated use `vim.diagnostic.enabled(…, false)`
function M.disable(bufnr, namespace)
vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } })
if bufnr == nil then
if namespace == nil then
-- Disable everything (including as yet non-existing buffers and
-- namespaces) by setting diagnostic_disabled to an empty table and set
-- its metatable to always return true. This metatable is removed
-- in enable()
diagnostic_disabled = setmetatable({}, {
__index = function()
return true
end,
})
else
local ns = M.get_namespace(namespace)
ns.disabled = true
end
else
bufnr = get_bufnr(bufnr)
if namespace == nil then
diagnostic_disabled[bufnr] = true
else
if type(diagnostic_disabled[bufnr]) ~= 'table' then
diagnostic_disabled[bufnr] = {}
end
diagnostic_disabled[bufnr][namespace] = true
end
end
M.hide(namespace, bufnr)
vim.deprecate(
'vim.diagnostic.disable()',
'vim.diagnostic.enabled(…, false)',
'0.12',
nil,
false
)
M.enable(bufnr, namespace, false)
end
--- Enable diagnostics in the given buffer.
--- Enables or disables diagnostics.
---
---@param bufnr integer? Buffer number, or 0 for current buffer. When
--- omitted, enable diagnostics in all buffers.
---@param namespace integer? Only enable diagnostics for the given namespace.
function M.enable(bufnr, namespace)
vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } })
--- To "toggle", pass the inverse of `is_enabled()`:
---
--- ```lua
--- vim.diagnostic.enable(0, not vim.diagnostic.is_enabled())
--- ```
---
--- @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
function M.enable(bufnr, namespace, enable)
vim.validate({
bufnr = { bufnr, 'n', true },
namespace = { namespace, 'n', true },
enable = { enable, 'b', true },
})
enable = enable == nil and true or enable
if bufnr == nil then
if namespace == nil then
-- Enable everything by setting diagnostic_disabled to an empty table
diagnostic_disabled = {}
diagnostic_disabled = (
enable
-- Enable everything by setting diagnostic_disabled to an empty table.
and {}
-- Disable everything (including as yet non-existing buffers and namespaces) by setting
-- diagnostic_disabled to an empty table and set its metatable to always return true.
or setmetatable({}, {
__index = function()
return true
end,
})
)
else
local ns = M.get_namespace(namespace)
ns.disabled = false
ns.disabled = not enable
end
else
bufnr = get_bufnr(bufnr)
if namespace == nil then
diagnostic_disabled[bufnr] = nil
diagnostic_disabled[bufnr] = (not enable) and true or nil
else
if type(diagnostic_disabled[bufnr]) ~= 'table' then
return
if enable then
return
else
diagnostic_disabled[bufnr] = {}
end
end
diagnostic_disabled[bufnr][namespace] = nil
diagnostic_disabled[bufnr][namespace] = (not enable) and true or nil
end
end
M.show(namespace, bufnr)
if enable then
M.show(namespace, bufnr)
else
M.hide(namespace, bufnr)
end
end
--- Parse a diagnostic from a string.

View File

@ -329,7 +329,7 @@ describe('vim.diagnostic', function()
eq(
{ 1, 1, 2, 0, 2 },
exec_lua [[
vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns)
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false)
return {
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_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(other_ns, diagnostic_bufnr, ns_2_diags)
vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns)
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false)
return {
count_extmarks(diagnostic_bufnr, diagnostic_ns),
@ -384,7 +384,7 @@ describe('vim.diagnostic', function()
{ 4, 0 },
exec_lua [[
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns)
vim.diagnostic.disable(diagnostic_bufnr, other_ns)
vim.diagnostic.enable(diagnostic_bufnr, other_ns, false)
return {
count_extmarks(diagnostic_bufnr, diagnostic_ns),
@ -500,7 +500,7 @@ describe('vim.diagnostic', function()
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.disable()
vim.diagnostic.enable(nil, nil, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
@ -561,7 +561,7 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.disable(diagnostic_bufnr)
vim.diagnostic.enable(diagnostic_bufnr, nil, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
@ -573,7 +573,7 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.disable(other_bufnr)
vim.diagnostic.enable(other_bufnr, nil, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
@ -610,7 +610,7 @@ describe('vim.diagnostic', function()
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.disable(nil, diagnostic_ns)
vim.diagnostic.enable(nil, diagnostic_ns, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
@ -620,7 +620,7 @@ describe('vim.diagnostic', function()
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
vim.diagnostic.disable(nil, other_ns)
vim.diagnostic.enable(nil, other_ns, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns))
@ -663,13 +663,13 @@ describe('vim.diagnostic', function()
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns)
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
vim.diagnostic.disable(diagnostic_bufnr, other_ns)
vim.diagnostic.enable(diagnostic_bufnr, other_ns, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
@ -682,7 +682,7 @@ describe('vim.diagnostic', function()
count_extmarks(other_bufnr, diagnostic_ns))
-- Should have no effect
vim.diagnostic.disable(other_bufnr, other_ns)
vim.diagnostic.enable(other_bufnr, other_ns, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
@ -1742,7 +1742,7 @@ describe('vim.diagnostic', function()
eq(
0,
exec_lua [[
vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns)
vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
})
@ -2711,7 +2711,39 @@ describe('vim.diagnostic', function()
)
end)
it('checks if diagnostics are disabled in a buffer', function()
it('is_enabled', function()
eq(
{ false, false, false, false },
exec_lua [[
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error('Diagnostic #1', 1, 1, 1, 1),
})
vim.api.nvim_set_current_buf(diagnostic_bufnr)
vim.diagnostic.enable(nil, nil, false)
return {
vim.diagnostic.is_enabled(),
vim.diagnostic.is_enabled(diagnostic_bufnr),
vim.diagnostic.is_enabled(diagnostic_bufnr, diagnostic_ns),
vim.diagnostic.is_enabled(_, diagnostic_ns),
}
]]
)
eq(
{ true, true, true, true },
exec_lua [[
vim.diagnostic.enable()
return {
vim.diagnostic.is_enabled(),
vim.diagnostic.is_enabled(diagnostic_bufnr),
vim.diagnostic.is_enabled(diagnostic_bufnr, diagnostic_ns),
vim.diagnostic.is_enabled(_, diagnostic_ns),
}
]]
)
end)
it('is_disabled (deprecated)', function()
eq(
{ true, true, true, true },
exec_lua [[