fix(diagnostic): unstable sorting by severity #37154

Problem: random order for same-severity diagnostics, severity_sort reversed.

Solution: add stable comparison with _extmark_id tiebreaker.
This commit is contained in:
glepnir
2025-12-30 17:28:32 +08:00
committed by GitHub
parent ea3562d739
commit 9833f0da5f
2 changed files with 78 additions and 15 deletions

View File

@@ -2538,6 +2538,31 @@ describe('vim.diagnostic', function()
end)
eq('Error here!', result[1][3][1])
end)
it('sorts by severity with stable tiebreaker #37137', function()
local result = exec_lua(function()
vim.diagnostic.config({ severity_sort = true, virtual_lines = { current_line = true } })
local m = 100
local diagnostics = {
{ end_col = m, lnum = 0, message = 'a', severity = 2 },
{ end_col = m, lnum = 0, message = 'b', severity = 2 },
{ end_col = m, lnum = 0, message = 'c', severity = 2 },
{ end_col = m, lnum = 2, message = 'd', severity = 2 },
{ end_col = m, lnum = 2, message = 'e', severity = 2 },
{ end_col = m, lnum = 2, message = 'f', severity = 2 },
}
vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {})
vim.diagnostic.show(_G.diagnostic_ns, _G.diagnostic_bufnr)
vim.api.nvim_win_set_cursor(0, { 1, 0 })
local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
local result = {}
for _, d in ipairs(extmarks[1][4].virt_lines) do
table.insert(result, d[3][1])
end
return result
end)
eq({ 'c', 'b', 'a' }, result)
end)
end)
describe('set()', function()