Compare commits

...

2 Commits

Author SHA1 Message Date
glepnir
2bbe25219d
Merge 5bb11844b3 into 160cbd0ef4 2024-12-18 21:39:48 +01:00
glepnir
5bb11844b3 fix(diagnostic): support function type of float in open_float()
Problem: the float field of vim.diagnostic.config can be a function type
but function was ignored in open_float function.

Solution: check global_diagnostic_options.float type before use for funciton type
2024-12-14 18:05:46 +08:00
2 changed files with 51 additions and 4 deletions

View File

@ -1824,10 +1824,13 @@ function M.open_float(opts, ...)
-- Resolve options with user settings from vim.diagnostic.config
-- Unlike the other decoration functions (e.g. set_virtual_text, set_signs, etc.) `open_float`
-- does not have a dedicated table for configuration options; instead, the options are mixed in
-- with its `opts` table which also includes "keyword" parameters. So we create a dedicated
-- options table that inherits missing keys from the global configuration before resolving.
local t = global_diagnostic_options.float
local float_opts = vim.tbl_extend('keep', opts, type(t) == 'table' and t or {})
-- with its `opts` table. We create a dedicated options table (`float_opts`) that inherits
-- missing keys from the global configuration (`global_diagnostic_options.float`), which can
-- be a table or a function.
local o = global_diagnostic_options
local t = type(o.float) == 'table' and o.float
or (type(o.float) == 'function' and o.float(0, bufnr) or {})
local float_opts = vim.tbl_extend('keep', opts, t)
opts = get_resolved_options({ float = float_opts }, nil, bufnr).float
end

View File

@ -1750,6 +1750,19 @@ describe('vim.diagnostic', function()
return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
end)
)
--`float` option defined as a function.
eq(
true,
exec_lua(function()
vim.diagnostic.config({
float = function(_, _)
return { border = 'rounded' }
end,
})
return type(vim.diagnostic.config().float) == 'function'
end)
)
end)
it('can use functions for config values', function()
@ -2501,6 +2514,37 @@ describe('vim.diagnostic', function()
)
end)
it('', function()
eq(
{ { 'Diagnostics:', '1. syntax error' }, '', '' },
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)
vim.diagnostic.config({
float = function(_, bufnr)
return { border = bufnr == _G.diagnostic_bufnr and 'rounded' or 'single' }
end,
})
local float_bufnr, winnr = vim.diagnostic.open_float()
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
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('can show diagnostics from the whole buffer', function()
eq(
{ '1. Syntax error', '2. Some warning' },