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

@@ -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),