fix(diagnostic): change default severity_sort order

When severity_sort is true, higher severities should be displayed before
lower severities (e.g. ERROR is displayed over WARN).

Also improved the test case for this.
This commit is contained in:
Gregory Anders 2021-09-17 14:59:30 -06:00
parent ede5695eb1
commit 938ed458e2
3 changed files with 39 additions and 25 deletions

View File

@ -239,7 +239,9 @@ config({opts}, {namespace}) *vim.diagnostic.config()*
• severity_sort: (default false) Sort • severity_sort: (default false) Sort
diagnostics by severity. This affects the diagnostics by severity. This affects the
order in which signs and virtual text are order in which signs and virtual text are
displayed. Options: displayed. When true, higher severities are
displayed before lower severities (e.g.
ERROR is displayed before WARN). Options:
• reverse: (boolean) Reverse sort order • reverse: (boolean) Reverse sort order
{namespace} number|nil Update the options for the given {namespace} number|nil Update the options for the given
namespace. When omitted, update the global namespace. When omitted, update the global

View File

@ -452,7 +452,9 @@ end
--- - update_in_insert: (default false) Update diagnostics in Insert mode (if false, --- - update_in_insert: (default false) Update diagnostics in Insert mode (if false,
--- diagnostics are updated on InsertLeave) --- diagnostics are updated on InsertLeave)
--- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in --- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in
--- which signs and virtual text are displayed. Options: --- which signs and virtual text are displayed. When true, higher severities
--- are displayed before lower severities (e.g. ERROR is displayed before WARN).
--- Options:
--- * reverse: (boolean) Reverse sort order --- * reverse: (boolean) Reverse sort order
---@param namespace number|nil Update the options for the given namespace. When omitted, update the ---@param namespace number|nil Update the options for the given namespace. When omitted, update the
--- global diagnostic options. --- global diagnostic options.
@ -998,9 +1000,9 @@ function M.show(namespace, bufnr, diagnostics, opts)
if vim.F.if_nil(opts.severity_sort, false) then if vim.F.if_nil(opts.severity_sort, false) then
if type(opts.severity_sort) == "table" and opts.severity_sort.reverse then if type(opts.severity_sort) == "table" and opts.severity_sort.reverse then
table.sort(diagnostics, function(a, b) return a.severity > b.severity end)
else
table.sort(diagnostics, function(a, b) return a.severity < b.severity end) table.sort(diagnostics, function(a, b) return a.severity < b.severity end)
else
table.sort(diagnostics, function(a, b) return a.severity > b.severity end)
end end
end end

View File

@ -540,11 +540,11 @@ describe('vim.diagnostic', function()
end) end)
it('allows sorting by severity', function() it('allows sorting by severity', function()
local result = exec_lua([[ exec_lua [[
vim.diagnostic.config({ vim.diagnostic.config({
underline = true, underline = false,
virtual_text = false, signs = true,
severity_sort = false, virtual_text = true,
}) })
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
@ -553,31 +553,41 @@ describe('vim.diagnostic', function()
make_info('Info', 4, 4, 4, 4), make_info('Info', 4, 4, 4, 4),
}) })
local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) function get_virt_text_and_signs(severity_sort)
vim.diagnostic.config({
severity_sort = severity_sort,
})
local warn_highlight = extmarks[1][4].hl_group local virt_text = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true})[1][4].virt_text
vim.diagnostic.config({ local virt_texts = {}
severity_sort = true, for i = 2, #virt_text do
}) table.insert(virt_texts, (string.gsub(virt_text[i][2], "DiagnosticVirtualText", "")))
end
extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) local signs = {}
for _, v in ipairs(vim.fn.sign_getplaced(diagnostic_bufnr, {group = "*"})[1].signs) do
table.insert(signs, (string.gsub(v.name, "DiagnosticSign", "")))
end
local err_highlight = extmarks[1][4].hl_group return {virt_texts, signs}
end
]]
vim.diagnostic.config({ local result = exec_lua [[return get_virt_text_and_signs(false)]]
severity_sort = { reverse = true },
})
extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) -- Virt texts are defined lowest priority to highest, signs from
-- highest to lowest
eq({'Warn', 'Error', 'Info'}, result[1])
eq({'Info', 'Error', 'Warn'}, result[2])
local info_highlight = extmarks[1][4].hl_group result = exec_lua [[return get_virt_text_and_signs(true)]]
eq({'Info', 'Warn', 'Error'}, result[1])
eq({'Error', 'Warn', 'Info'}, result[2])
return { warn_highlight, err_highlight, info_highlight } result = exec_lua [[return get_virt_text_and_signs({ reverse = true })]]
]]) eq({'Error', 'Warn', 'Info'}, result[1])
eq('DiagnosticUnderlineWarn', result[1]) eq({'Info', 'Warn', 'Error'}, result[2])
eq('DiagnosticUnderlineError', result[2])
eq('DiagnosticUnderlineInfo', result[3])
end) end)
end) end)