fix(diagnostic): invalid col number compare in next_diagnostic

Problem: when line is blank link then there will got an invalid column number in math.min compare.

Solution: make sure the min column number is 0 not an illegal number.
This commit is contained in:
glepnir 2024-04-18 18:03:31 +08:00
parent 96d0c709b6
commit 743a6e687a
2 changed files with 25 additions and 2 deletions

View File

@ -828,14 +828,14 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace)
return a.col < b.col
end
is_next = function(d)
return math.min(d.col, line_length - 1) > position[2]
return math.min(d.col, math.max(line_length - 1, 0)) > position[2]
end
else
sort_diagnostics = function(a, b)
return a.col > b.col
end
is_next = function(d)
return math.min(d.col, line_length - 1) < position[2]
return math.min(d.col, math.max(line_length - 1, 0)) < position[2]
end
end
table.sort(line_diagnostics[lnum], sort_diagnostics)

View File

@ -978,6 +978,29 @@ describe('vim.diagnostic', function()
]]
)
end)
it('works on blank line #28397', function()
eq(
{ 0, 2 },
exec_lua [[
local test_bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, {
'first line',
'',
'',
'end line',
})
vim.diagnostic.set(diagnostic_ns, test_bufnr, {
make_info('Diagnostic #1', 0, 2, 0, 2),
make_info('Diagnostic #2', 2, 0, 2, 0),
make_info('Diagnostic #3', 2, 0, 2, 0),
})
vim.api.nvim_win_set_buf(0, test_bufnr)
vim.api.nvim_win_set_cursor(0, {3, 0})
return vim.diagnostic.get_prev_pos { namespace = diagnostic_ns}
]]
)
end)
end)
describe('get()', function()