fix(diagnostic): open_float() handles config.float function #31577

Problem:
The `float` field of vim.diagnostic.config can be a function,
but diagnostic.open_float() does not handle it correctly.

Solution:
Add handling for it in open_float().
This commit is contained in:
glepnir
2026-03-16 21:50:13 +08:00
committed by GitHub
parent 46f538c210
commit 88a72efb46
3 changed files with 43 additions and 28 deletions

View File

@@ -3215,28 +3215,40 @@ describe('vim.diagnostic', function()
)
end)
it(
'creates floating window and returns float bufnr and winnr without header, if requested',
function()
-- One line (since no header):
-- 1. <msg>
eq(
1,
exec_lua(function()
local diagnostics = {
_G.make_error('Syntax error', 0, 1, 0, 3),
}
vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
local float_bufnr, winnr =
vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return #lines
end)
)
end
)
it('creates floating window without header and supports function-based float config', function()
-- One line (since no header):
-- 1. <msg>
eq(
{ 1, '', '' },
exec_lua(function()
local diagnostics = {
_G.make_error('Syntax error', 0, 1, 0, 3),
}
vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
local float_bufnr, winnr =
vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
vim.diagnostic.config({
float = function(_, bufnr)
return { border = bufnr == _G.diagnostic_bufnr and 'rounded' or 'single' }
end,
})
_, winnr = vim.diagnostic.open_float()
local border1 = vim.api.nvim_win_get_config(winnr).border
vim.api.nvim_win_close(winnr, true)
local other_bufnr = vim.api.nvim_create_buf(true, true)
vim.api.nvim_win_set_buf(0, other_bufnr)
vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, diagnostics)
local _, winnr1 = vim.diagnostic.open_float()
local border2 = vim.api.nvim_win_get_config(winnr1).border
vim.api.nvim_win_close(winnr1, true)
return { #lines, border1[1], border2[1] }
end)
)
end)
it('clamps diagnostic line numbers within the valid range', function()
eq(