From 410744210386c8305da047e1a0e91f798c2cac0c Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Tue, 25 Nov 2025 10:14:46 -0800 Subject: [PATCH] feat(diagnostic): highlights in diagnostic.status() #36685 Applies the appropriate `DiagnosticSign*` highlight to each group, resetting the highlights at the end of the expression. --- runtime/lua/vim/_meta/options.lua | 2 +- runtime/lua/vim/diagnostic.lua | 13 ++++++++++++- src/nvim/options.lua | 2 +- test/functional/lua/diagnostic_spec.lua | 7 +++++-- test/functional/ui/statusline_spec.lua | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/runtime/lua/vim/_meta/options.lua b/runtime/lua/vim/_meta/options.lua index 1f4a97d0f2..d69ad6b095 100644 --- a/runtime/lua/vim/_meta/options.lua +++ b/runtime/lua/vim/_meta/options.lua @@ -6999,7 +6999,7 @@ vim.wo.stc = vim.wo.statuscolumn --- --- --- @type string -vim.o.statusline = "%<%f %h%w%m%r %=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &busy > 0 ? '◐ ' : '' %}%(%{luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status()) or '''' ')} %)%{% &ruler ? ( &rulerformat == '' ? '%-14.(%l,%c%V%) %P' : &rulerformat ) : '' %}" +vim.o.statusline = "%<%f %h%w%m%r %=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &busy > 0 ? '◐ ' : '' %}%{% luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status() .. '' '') or '''' ') %}%{% &ruler ? ( &rulerformat == '' ? '%-14.(%l,%c%V%) %P' : &rulerformat ) : '' %}" vim.o.stl = vim.o.statusline vim.wo.statusline = vim.o.statusline vim.wo.stl = vim.wo.statusline diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index a33ed7e082..de51b3a84b 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -2876,6 +2876,13 @@ function M.fromqflist(list) return diagnostics end +local hl_map = { + [M.severity.ERROR] = 'DiagnosticSignError', + [M.severity.WARN] = 'DiagnosticSignWarn', + [M.severity.INFO] = 'DiagnosticSignInfo', + [M.severity.HINT] = 'DiagnosticSignHint', +} + --- Returns formatted string with diagnostics for the current buffer. --- The severities with 0 diagnostics are left out. --- Example `E:2 W:3 I:4 H:5` @@ -2899,10 +2906,14 @@ function M.status(bufnr) local result_str = vim .iter(pairs(counts)) :map(function(severity, count) - return ('%s:%s'):format(signs[severity], count) + return ('%%#%s#%s:%s'):format(hl_map[severity], signs[severity], count) end) :join(' ') + if result_str:len() > 0 then + result_str = result_str .. '%##' + end + return result_str end diff --git a/src/nvim/options.lua b/src/nvim/options.lua index fbef3a19b2..b1116d860f 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -8831,7 +8831,7 @@ local options = { "%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}", "%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}", "%{% &busy > 0 ? '◐ ' : '' %}", - "%(%{luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status()) or '''' ')} %)", + "%{% luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status() .. '' '') or '''' ') %}", "%{% &ruler ? ( &rulerformat == '' ? '%-14.(%l,%c%V%) %P' : &rulerformat ) : '' %}", }), doc = 'is very long', diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 4a971ae515..a80b280db3 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -4081,7 +4081,10 @@ describe('vim.diagnostic', function() return vim.diagnostic.status() end) - eq('E:1 W:2 I:3 H:4', result) + eq( + '%#DiagnosticSignError#E:1 %#DiagnosticSignWarn#W:2 %#DiagnosticSignInfo#I:3 %#DiagnosticSignHint#H:4%##', + result + ) exec_lua('vim.cmd.enew()') @@ -4113,7 +4116,7 @@ describe('vim.diagnostic', function() return vim.diagnostic.status() end) - eq('⨯:1 ⚠︎:1', result) + eq('%#DiagnosticSignError#⨯:1 %#DiagnosticSignWarn#⚠︎:1%##', result) end) end) diff --git a/test/functional/ui/statusline_spec.lua b/test/functional/ui/statusline_spec.lua index 5cea1443fc..03c1d11842 100644 --- a/test/functional/ui/statusline_spec.lua +++ b/test/functional/ui/statusline_spec.lua @@ -850,7 +850,7 @@ describe('default statusline', function() "%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}", "%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}", "%{% &busy > 0 ? '◐ ' : '' %}", - "%(%{luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status()) or '''' ')} %)", + "%{% luaeval('(package.loaded[''vim.diagnostic''] and vim.diagnostic.status() .. '' '') or '''' ') %}", "%{% &ruler ? ( &rulerformat == '' ? '%-14.(%l,%c%V%) %P' : &rulerformat ) : '' %}", })