refactor(diagnostic): make display handlers generic (#16137)

Rather than treating virtual_text, signs, and underline specially,
introduce the concept of generic "handlers", of which those three are
simply the defaults bundled with Nvim. Handlers are called in
`vim.diagnostic.show()` and `vim.diagnostic.hide()` and are used to
handle how diagnostics are displayed.
This commit is contained in:
Gregory Anders
2021-10-29 19:47:34 -06:00
committed by GitHub
parent 4472c56d54
commit e921e98ce3
5 changed files with 433 additions and 1019 deletions

View File

@@ -6,6 +6,8 @@ local clear = helpers.clear
local exec_lua = helpers.exec_lua
local eq = helpers.eq
local nvim = helpers.nvim
local matches = helpers.matches
local pcall_err = helpers.pcall_err
describe('vim.diagnostic', function()
before_each(function()
@@ -47,7 +49,21 @@ describe('vim.diagnostic', function()
end
function count_extmarks(bufnr, namespace)
return #vim.api.nvim_buf_get_extmarks(bufnr, namespace, 0, -1, {})
local ns = vim.diagnostic.get_namespace(namespace)
local extmarks = 0
if ns.user_data.virt_text_ns then
extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {})
end
if ns.user_data.underline_ns then
extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {})
end
return extmarks
end
function get_virt_text_extmarks(ns)
local ns = vim.diagnostic.get_namespace(ns)
local virt_text_ns = ns.user_data.virt_text_ns
return vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, virt_text_ns, 0, -1, {details = true})
end
]]
@@ -567,7 +583,7 @@ describe('vim.diagnostic', function()
]]
eq(1, exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]])
eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]])
-- eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]])
end)
it('allows filtering by severity', function()
@@ -615,7 +631,7 @@ describe('vim.diagnostic', function()
severity_sort = severity_sort,
})
local virt_text = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})[1][4].virt_text
local virt_text = get_virt_text_extmarks(diagnostic_ns)[1][4].virt_text
local virt_texts = {}
for i = 2, #virt_text do
@@ -661,7 +677,7 @@ describe('vim.diagnostic', function()
}
})
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})
local extmarks = get_virt_text_extmarks(diagnostic_ns)
local virt_text = extmarks[1][4].virt_text[2][1]
return virt_text
]]
@@ -676,7 +692,7 @@ describe('vim.diagnostic', function()
}
}, diagnostic_ns)
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})
local extmarks = get_virt_text_extmarks(diagnostic_ns)
local virt_text = extmarks[1][4].virt_text[2][1]
return virt_text
]]
@@ -696,7 +712,7 @@ describe('vim.diagnostic', function()
}
})
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})
local extmarks = get_virt_text_extmarks(diagnostic_ns)
local virt_text = {extmarks[1][4].virt_text[2][1], extmarks[2][4].virt_text[2][1]}
return virt_text
]]
@@ -724,7 +740,7 @@ describe('vim.diagnostic', function()
make_error('Error', 1, 0, 1, 0),
})
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})
local extmarks = get_virt_text_extmarks(diagnostic_ns)
return {extmarks[1][4].virt_text, extmarks[2][4].virt_text}
]]
eq(" 👀 Warning", result[1][2][1])
@@ -752,7 +768,7 @@ describe('vim.diagnostic', function()
make_error('Error', 1, 0, 1, 0, 'another_linter'),
})
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})
local extmarks = get_virt_text_extmarks(diagnostic_ns)
return {extmarks[1][4].virt_text, extmarks[2][4].virt_text}
]]
eq(" some_linter: 👀 Warning", result[1][2][1])
@@ -800,13 +816,11 @@ describe('vim.diagnostic', function()
virtual_text = true,
})
-- Count how many times we call display.
SetVirtualTextOriginal = vim.diagnostic._set_virtual_text
DisplayCount = 0
vim.diagnostic._set_virtual_text = function(...)
local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
vim.diagnostic.handlers.virtual_text.show = function(...)
DisplayCount = DisplayCount + 1
return SetVirtualTextOriginal(...)
return set_virtual_text(...)
end
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
@@ -850,13 +864,12 @@ describe('vim.diagnostic', function()
virtual_text = false,
})
-- Count how many times we call display.
SetVirtualTextOriginal = vim.diagnostic._set_virtual_text
DisplayCount = 0
vim.diagnostic._set_virtual_text = function(...)
local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
vim.diagnostic.handlers.virtual_text.show = function(...)
DisplayCount = DisplayCount + 1
return SetVirtualTextOriginal(...)
return set_virtual_text(...)
end
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
@@ -1347,4 +1360,73 @@ describe('vim.diagnostic', function()
eq(result[1], result[2])
end)
end)
describe('handlers', function()
it('checks that a new handler is a table', function()
matches([[.*handler: expected table, got string.*]], pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = "bar" ]]))
matches([[.*handler: expected table, got function.*]], pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = function() end ]]))
end)
it('can add new handlers', function()
eq(true, exec_lua [[
local handler_called = false
vim.diagnostic.handlers.test = {
show = function(namespace, bufnr, diagnostics, opts)
assert(namespace == diagnostic_ns)
assert(bufnr == diagnostic_bufnr)
assert(#diagnostics == 1)
assert(opts.test.some_opt == 42)
handler_called = true
end,
}
vim.diagnostic.config({test = {some_opt = 42}})
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_warning("Warning", 0, 0, 0, 0),
})
return handler_called
]])
end)
it('can disable handlers by setting the corresponding option to false', function()
eq(false, exec_lua [[
local handler_called = false
vim.diagnostic.handlers.test = {
show = function(namespace, bufnr, diagnostics, opts)
handler_called = true
end,
}
vim.diagnostic.config({test = false})
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_warning("Warning", 0, 0, 0, 0),
})
return handler_called
]])
end)
it('always calls a handler\'s hide function if defined', function()
eq({false, true}, exec_lua [[
local hide_called = false
local show_called = false
vim.diagnostic.handlers.test = {
show = function(namespace, bufnr, diagnostics, opts)
show_called = true
end,
hide = function(namespace, bufnr)
assert(namespace == diagnostic_ns)
assert(bufnr == diagnostic_bufnr)
hide_called = true
end,
}
vim.diagnostic.config({test = false})
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_warning("Warning", 0, 0, 0, 0),
})
vim.diagnostic.hide(diagnostic_ns, diagnostic_bufnr)
return {show_called, hide_called}
]])
end)
end)
end)