From 5bb11844b3f73c981c5d090caa07d4f9444fe5c1 Mon Sep 17 00:00:00 2001 From: glepnir Date: Sat, 14 Dec 2024 15:06:22 +0800 Subject: [PATCH] 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 --- runtime/lua/vim/diagnostic.lua | 11 ++++--- test/functional/lua/diagnostic_spec.lua | 44 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index ded7a8f89d..d5cba114b6 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -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 diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index eb1ac3e6a1..109703b716 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -1749,6 +1749,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() @@ -2500,6 +2513,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' },