diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index 9ed75e1356..199c04be98 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -239,7 +239,9 @@ config({opts}, {namespace}) *vim.diagnostic.config()* • severity_sort: (default false) Sort diagnostics by severity. This affects the 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 {namespace} number|nil Update the options for the given namespace. When omitted, update the global diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 6547188594..18a2023b50 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -452,7 +452,9 @@ end --- - update_in_insert: (default false) Update diagnostics in Insert mode (if false, --- diagnostics are updated on InsertLeave) --- - 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 ---@param namespace number|nil Update the options for the given namespace. When omitted, update the --- 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 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) + else + table.sort(diagnostics, function(a, b) return a.severity > b.severity end) end end diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 3c8d6e8f2c..2971d3bc51 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -540,11 +540,11 @@ describe('vim.diagnostic', function() end) it('allows sorting by severity', function() - local result = exec_lua([[ + exec_lua [[ vim.diagnostic.config({ - underline = true, - virtual_text = false, - severity_sort = false, + underline = false, + signs = true, + virtual_text = true, }) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { @@ -553,31 +553,41 @@ describe('vim.diagnostic', function() 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({ - severity_sort = true, - }) + local virt_texts = {} + 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({ - severity_sort = { reverse = true }, - }) + local result = exec_lua [[return get_virt_text_and_signs(false)]] - 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 } - ]]) - eq('DiagnosticUnderlineWarn', result[1]) - eq('DiagnosticUnderlineError', result[2]) - eq('DiagnosticUnderlineInfo', result[3]) + result = exec_lua [[return get_virt_text_and_signs({ reverse = true })]] + eq({'Error', 'Warn', 'Info'}, result[1]) + eq({'Info', 'Warn', 'Error'}, result[2]) end) end)