aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2024-04-15 18:35:59 +0200
committerJustin M. Keyes <justinkz@gmail.com>2024-04-15 21:32:31 +0200
commit5ed9916a28b9a09d2e5629f3e4e5b0e81403ded7 (patch)
treee4625022a73f20c8e18c21429c0e3cf96f562b64
parent26765e8461c1ba1e9a351632212cf89900221781 (diff)
downloadrneovim-5ed9916a28b9a09d2e5629f3e4e5b0e81403ded7.tar.gz
rneovim-5ed9916a28b9a09d2e5629f3e4e5b0e81403ded7.tar.bz2
rneovim-5ed9916a28b9a09d2e5629f3e4e5b0e81403ded7.zip
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`.
-rw-r--r--runtime/doc/develop.txt2
-rw-r--r--runtime/doc/diagnostic.txt20
-rw-r--r--runtime/doc/news.txt3
-rw-r--r--runtime/lua/vim/diagnostic.lua67
-rw-r--r--test/functional/lua/diagnostic_spec.lua87
5 files changed, 128 insertions, 51 deletions
diff --git a/runtime/doc/develop.txt b/runtime/doc/develop.txt
index a7373c84b3..6c27e4775f 100644
--- a/runtime/doc/develop.txt
+++ b/runtime/doc/develop.txt
@@ -372,11 +372,13 @@ Use existing common {verb} names (actions) if possible:
- create: Creates a new (non-trivial) thing (TODO: rename to "def"?)
- del: Deletes a thing (or group of things)
- detach: Dispose attached listener (TODO: rename to "un"?)
+ - enable: Enables/disables functionality.
- eval: Evaluates an expression
- exec: Executes code
- fmt: Formats
- get: Gets things (often by a query)
- inspect: Presents a high-level, often interactive, view
+ - is_enabled: Checks if functionality is enabled.
- open: Opens something (a buffer, window, …)
- parse: Parses something into a structured form
- set: Sets a thing (or group of things)
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt
index 1f3f1c80fe..1826cc4c08 100644
--- a/runtime/doc/diagnostic.txt
+++ b/runtime/doc/diagnostic.txt
@@ -367,6 +367,13 @@ Lua module: vim.diagnostic *diagnostic-api*
• {user_data}? (`any`) arbitrary data plugins can add
• {namespace}? (`integer`)
+*vim.diagnostic.Filter*
+ Extends: |vim.diagnostic.Opts|
+
+
+ Fields: ~
+ • {ns_id}? (`integer`) Namespace
+
*vim.diagnostic.GetOpts*
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
|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.
To "toggle", pass the inverse of `is_enabled()`: >lua
@@ -624,11 +631,12 @@ enable({bufnr}, {namespace}, {enable}) *vim.diagnostic.enable()*
<
Parameters: ~
- • {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
+ • {bufnr} (`integer?`) Buffer number, or 0 for current buffer, or
+ `nil` for all buffers.
+ • {enable} (`boolean?`) true/nil to enable, false to disable
+ • {opts} (`vim.diagnostic.Filter?`) Filter by these opts, or `nil`
+ for all. Only `ns_id` is supported, currently. See
+ |vim.diagnostic.Filter|.
fromqflist({list}) *vim.diagnostic.fromqflist()*
Convert a list of quickfix items to a list of diagnostics.
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
index 8609d84506..d320db4e51 100644
--- a/runtime/doc/news.txt
+++ b/runtime/doc/news.txt
@@ -421,7 +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.
+• |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
used to highlight a range of arbitrary length. The |nvim_buf_set_extmark()|
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index 26374aa055..4fcbfa7507 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -239,6 +239,9 @@ local M = {}
--- whole line the sign is placed in.
--- @field linehl? table<vim.diagnostic.Severity,string>
+--- @class vim.diagnostic.Filter : vim.diagnostic.Opts
+--- @field ns_id? integer Namespace
+
--- @nodoc
--- @enum vim.diagnostic.Severity
M.severity = {
@@ -1538,13 +1541,7 @@ 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
- )
+ vim.deprecate('vim.diagnostic.is_disabled()', 'vim.diagnostic.is_enabled()', '0.12', nil, false)
return not M.is_enabled(bufnr, namespace)
end
@@ -1591,7 +1588,7 @@ function M.show(namespace, bufnr, diagnostics, opts)
return
end
- if M.is_disabled(bufnr, namespace) then
+ if not M.is_enabled(bufnr, namespace) then
return
end
@@ -1942,7 +1939,7 @@ function M.disable(bufnr, namespace)
nil,
false
)
- M.enable(bufnr, namespace, false)
+ M.enable(bufnr, false, { ns_id = namespace })
end
--- 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 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)
+--- @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({
bufnr = { bufnr, 'n', true },
- namespace = { namespace, 'n', true },
- enable = { enable, 'b', true },
+ enable = {
+ 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
+
if bufnr == nil then
- if namespace == nil then
+ if opts.ns_id == nil then
diagnostic_disabled = (
enable
-- Enable everything by setting diagnostic_disabled to an empty table.
@@ -1978,12 +2007,12 @@ function M.enable(bufnr, namespace, enable)
})
)
else
- local ns = M.get_namespace(namespace)
+ local ns = M.get_namespace(opts.ns_id)
ns.disabled = not enable
end
else
bufnr = get_bufnr(bufnr)
- if namespace == nil then
+ if opts.ns_id == nil then
diagnostic_disabled[bufnr] = (not enable) and true or nil
else
if type(diagnostic_disabled[bufnr]) ~= 'table' then
@@ -1993,14 +2022,14 @@ function M.enable(bufnr, namespace, enable)
diagnostic_disabled[bufnr] = {}
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
if enable then
- M.show(namespace, bufnr)
+ M.show(opts.ns_id, bufnr)
else
- M.hide(namespace, bufnr)
+ M.hide(opts.ns_id, bufnr)
end
end
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
index 972e2baa71..1a87d159c7 100644
--- a/test/functional/lua/diagnostic_spec.lua
+++ b/test/functional/lua/diagnostic_spec.lua
@@ -329,7 +329,7 @@ describe('vim.diagnostic', function()
eq(
{ 1, 1, 2, 0, 2 },
exec_lua [[
- vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns, false)
+ vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
return {
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns),
count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns),
@@ -344,7 +344,7 @@ describe('vim.diagnostic', function()
eq(
all_highlights,
exec_lua([[
- vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns)
+ vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
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.enable(diagnostic_bufnr, diagnostic_ns, false)
+ vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = diagnostic_ns })
return {
count_extmarks(diagnostic_bufnr, diagnostic_ns),
@@ -383,8 +383,8 @@ describe('vim.diagnostic', function()
eq(
{ 4, 0 },
exec_lua [[
- vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns)
- vim.diagnostic.enable(diagnostic_bufnr, other_ns, false)
+ vim.diagnostic.enable(diagnostic_bufnr, true, { ns_id = diagnostic_ns })
+ vim.diagnostic.enable(diagnostic_bufnr, false, { ns_id = other_ns })
return {
count_extmarks(diagnostic_bufnr, diagnostic_ns),
@@ -478,7 +478,7 @@ describe('vim.diagnostic', function()
end)
describe('enable() and disable()', function()
- it('works without arguments', function()
+ it('without arguments', function()
local result = exec_lua [[
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) +
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) +
count_extmarks(diagnostic_bufnr, other_ns))
@@ -532,7 +532,7 @@ describe('vim.diagnostic', function()
eq(4, result[4])
end)
- it('works with only a buffer argument', function()
+ it('with buffer argument', function()
local result = exec_lua [[
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(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) +
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.enable(other_bufnr, nil, false)
+ vim.diagnostic.enable(other_bufnr, false)
table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) +
count_extmarks(diagnostic_bufnr, other_ns) +
@@ -588,7 +588,7 @@ describe('vim.diagnostic', function()
eq(3, result[4])
end)
- it('works with only a namespace argument', function()
+ it('with a namespace argument', function()
local result = exec_lua [[
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) +
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) +
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) +
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) +
count_extmarks(diagnostic_bufnr, other_ns))
@@ -634,8 +634,11 @@ describe('vim.diagnostic', function()
eq(2, result[4])
end)
- it('works with both a buffer and a namespace argument', function()
- local result = exec_lua [[
+ --- @return table
+ local function test_enable(legacy)
+ local result = exec_lua(
+ [[
+ local legacy = ...
local other_bufnr = vim.api.nvim_create_buf(true, false)
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(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) +
count_extmarks(diagnostic_bufnr, other_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) +
count_extmarks(diagnostic_bufnr, other_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) +
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
- -- Should have no effect
- vim.diagnostic.enable(other_bufnr, other_ns, false)
+ if legacy then
+ -- 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) +
count_extmarks(diagnostic_bufnr, other_ns) +
count_extmarks(other_bufnr, diagnostic_ns))
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(2, result[2])
eq(1, result[3])
@@ -1742,7 +1779,7 @@ describe('vim.diagnostic', function()
eq(
0,
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, {
make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
})
@@ -1753,7 +1790,7 @@ describe('vim.diagnostic', function()
eq(
2,
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)
]]
)
@@ -2719,7 +2756,7 @@ describe('vim.diagnostic', function()
make_error('Diagnostic #1', 1, 1, 1, 1),
})
vim.api.nvim_set_current_buf(diagnostic_bufnr)
- vim.diagnostic.enable(nil, nil, false)
+ vim.diagnostic.enable(nil, false)
return {
vim.diagnostic.is_enabled(),
vim.diagnostic.is_enabled(diagnostic_bufnr),