From f562204a5cd0949376102f85a250c842e5951af4 Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Wed, 6 May 2026 19:01:21 +0800 Subject: [PATCH] feat(diagnostic)!: deprecate `format` as a table #39603 --- runtime/doc/diagnostic.txt | 57 ++++++++++----------- runtime/doc/news.txt | 3 +- runtime/lua/vim/diagnostic.lua | 68 +++++++++++-------------- test/functional/lua/diagnostic_spec.lua | 6 +-- 4 files changed, 62 insertions(+), 72 deletions(-) diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index 0a2e8a549a..170e52a99c 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -672,36 +672,33 @@ Lua module: vim.diagnostic *diagnostic-api* *vim.diagnostic.Opts.Status* Fields: ~ - • {format}? (`table|(fun(counts:table): string)`) - Either: - • a table mapping |diagnostic-severity| to the text to use - for each existing severity section. - • a function that accepts a mapping of - |diagnostic-severity| to the number of diagnostics of the - corresponding severity (only those severity levels that - have at least 1 diagnostic) and returns a 'statusline' - component. In this case highlights must be applied by the - user in the `format` function. Example: >lua - local signs = { - [vim.diagnostic.severity.ERROR] = "A", - -- ... - } - local hl_map = { - [vim.diagnostic.severity.ERROR] = 'DiagnosticSignError', - -- ... - } - vim.diagnostic.config({ - status = { - format = function(counts) - local items = {} - for level, _ in ipairs(vim.diagnostic.severity) do - local count = counts[level] or 0 - table.insert(items, ("%%#%s#%s %s"):format(hl_map[level], signs[level], count)) - end - return table.concat(items, " ") - end - } - }) + • {format}? (`fun(counts:table): string`) + A function that accepts a mapping of |diagnostic-severity| + to the number of diagnostics of the corresponding severity + (only those severity levels that have at least 1 + diagnostic) and returns a 'statusline' component. In this + case highlights must be applied by the user in the `format` + function. Example: >lua + local signs = { + [vim.diagnostic.severity.ERROR] = "A", + -- ... + } + local hl_map = { + [vim.diagnostic.severity.ERROR] = 'DiagnosticSignError', + -- ... + } + vim.diagnostic.config({ + status = { + format = function(severity_counts) + local items = {} + for severity in ipairs(vim.diagnostic.severity) do + local count = severity_counts[severity] or 0 + table.insert(items, ("%%#%s#%s %s"):format(hl_map[severity], signs[severity], count)) + end + return table.concat(items, " ") + end + } + }) < *vim.diagnostic.Opts.Underline* diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 3496a0fe14..2dc422af1f 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -73,7 +73,8 @@ API DIAGNOSTICS -• todo +• `vim.diagnostic.Opts.Status.format` no longer accepts the table mapping + from severity to text. Use `vim.diagnostic.Opts.Signs.text` instead. EDITOR diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 5b804a8736..30287d7f02 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -177,37 +177,34 @@ local M = vim._defer_require('vim.diagnostic', { --- @class vim.diagnostic.Opts.Status --- ---- Either: ---- - a table mapping |diagnostic-severity| to the text to use for each ---- existing severity section. ---- - a function that accepts a mapping of |diagnostic-severity| to the ---- number of diagnostics of the corresponding severity (only those ---- severity levels that have at least 1 diagnostic) and returns ---- a 'statusline' component. In this case highlights must be applied ---- by the user in the `format` function. Example: ---- ```lua ---- local signs = { ---- [vim.diagnostic.severity.ERROR] = "A", ---- -- ... ---- } ---- local hl_map = { ---- [vim.diagnostic.severity.ERROR] = 'DiagnosticSignError', ---- -- ... ---- } ---- vim.diagnostic.config({ ---- status = { ---- format = function(counts) ---- local items = {} ---- for level, _ in ipairs(vim.diagnostic.severity) do ---- local count = counts[level] or 0 ---- table.insert(items, ("%%#%s#%s %s"):format(hl_map[level], signs[level], count)) ---- end ---- return table.concat(items, " ") +--- A function that accepts a mapping of |diagnostic-severity| to the number of +--- diagnostics of the corresponding severity (only those severity levels that +--- have at least 1 diagnostic) and returns a 'statusline' component. +--- In this case highlights must be applied by the user in the `format` function. +--- Example: +--- ```lua +--- local signs = { +--- [vim.diagnostic.severity.ERROR] = "A", +--- -- ... +--- } +--- local hl_map = { +--- [vim.diagnostic.severity.ERROR] = 'DiagnosticSignError', +--- -- ... +--- } +--- vim.diagnostic.config({ +--- status = { +--- format = function(severity_counts) +--- local items = {} +--- for severity in ipairs(vim.diagnostic.severity) do +--- local count = severity_counts[severity] or 0 +--- table.insert(items, ("%%#%s#%s %s"):format(hl_map[severity], signs[severity], count)) --- end ---- } ---- }) ---- ``` ---- @field format? table|(fun(counts:table): string) +--- return table.concat(items, " ") +--- end +--- } +--- }) +--- ``` +--- @field format? (fun(counts:table): string) --- @class vim.diagnostic.Opts.Underline --- @@ -1118,7 +1115,7 @@ function M.status(buf) vim.validate('buf', buf, 'number', true) buf = buf or 0 local config = assert(vim.diagnostic.config()).status or {} --- @type vim.diagnostic.Opts.Status - vim.validate('config.format', config.format, { 'table', 'function' }, true) + vim.validate('config.format', config.format, 'function', true) local counts = M.count(buf) local format = config.format @@ -1126,13 +1123,8 @@ function M.status(buf) if type(format) == 'function' then result_str = format(counts) else - local signs ---@type table - if type(format) == 'table' then - signs = format - else - signs = M._config.get_resolved_options(vim.diagnostic.config(), nil, buf).signs.text - or default_status_signs - end + local signs = M._config.get_resolved_options(vim.diagnostic.config(), nil, buf).signs.text + or default_status_signs result_str = vim .iter(pairs(counts)) :map(function(level, value) diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 34bacf5add..da9c5bc869 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -4219,11 +4219,11 @@ describe('vim.diagnostic', function() ) end) - it('uses text from diagnostic.config().status.format[severity]', function() + it('uses text from diagnostic.config().signs.text[severity]', function() local result = exec_lua(function() vim.diagnostic.config({ - status = { - format = { + signs = { + text = { [vim.diagnostic.severity.ERROR] = '⨯', [vim.diagnostic.severity.WARN] = '⚠︎', },