Skip to content

Commit 8821d1b

Browse files
committed
(mini.statusline) Improve performance of section_diagnostics().
Details: - And overall 'mini.statusline' startup footprint by avoiding usage of `vim.diagnostic` during module load.
1 parent 1cd8d7e commit 8821d1b

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

lua/mini/statusline.lua

+17-8
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,11 @@ MiniStatusline.section_diagnostics = function(args)
299299
local dont_show_lsp = MiniStatusline.is_truncated(args.trunc_width) or H.isnt_normal_buffer() or hasnt_attached_client
300300
if dont_show_lsp then return '' end
301301

302-
-- Construct diagnostic info using predefined order
303-
local t = {}
302+
-- Construct string parts
303+
local counts = H.get_diagnostic_count()
304+
local severity, t = vim.diagnostic.severity, {}
304305
for _, level in ipairs(H.diagnostic_levels) do
305-
local n = H.get_diagnostic_count(level.id)
306+
local n = counts[severity[level.name]] or 0
306307
-- Add level info only if diagnostic is present
307308
if n > 0 then table.insert(t, string.format(' %s%s', level.sign, n)) end
308309
end
@@ -419,10 +420,10 @@ H.default_config = vim.deepcopy(MiniStatusline.config)
419420

420421
-- Showed diagnostic levels
421422
H.diagnostic_levels = {
422-
{ id = vim.diagnostic.severity.ERROR, sign = 'E' },
423-
{ id = vim.diagnostic.severity.WARN, sign = 'W' },
424-
{ id = vim.diagnostic.severity.INFO, sign = 'I' },
425-
{ id = vim.diagnostic.severity.HINT, sign = 'H' },
423+
{ name = 'ERROR', sign = 'E' },
424+
{ name = 'WARN', sign = 'W' },
425+
{ name = 'INFO', sign = 'I' },
426+
{ name = 'HINT', sign = 'H' },
426427
}
427428

428429
-- Helper functionality =======================================================
@@ -582,6 +583,14 @@ H.get_filetype_icon = function()
582583
return devicons.get_icon(file_name, file_ext, { default = true })
583584
end
584585

585-
H.get_diagnostic_count = function(id) return #vim.diagnostic.get(0, { severity = id }) end
586+
H.get_diagnostic_count = function()
587+
local res = {}
588+
for _, d in ipairs(vim.diagnostic.get(0)) do
589+
res[d.severity] = (res[d.severity] or 0) + 1
590+
end
591+
return res
592+
end
593+
594+
if vim.fn.has('nvim-0.10') == 1 then H.get_diagnostic_count = function() return vim.diagnostic.count(0) end end
586595

587596
return MiniStatusline
+20-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
vim.lsp.buf_get_clients = function() return { 'mock client' } end
22

3-
vim.diagnostic.get = function(buf_id, opts)
4-
local severity = vim.diagnostic.severity
5-
local n = ({ [severity.ERROR] = 4, [severity.WARN] = 3, [severity.INFO] = 2, [severity.HINT] = 1 })[opts.severity]
3+
vim.diagnostic.get = function(_, _)
4+
local s = vim.diagnostic.severity
5+
return {
6+
{ severity = s.ERROR },
7+
{ severity = s.WARN },
8+
{ severity = s.INFO },
9+
{ severity = s.HINT },
10+
{ severity = s.ERROR },
11+
{ severity = s.WARN },
12+
{ severity = s.INFO },
13+
{ severity = s.ERROR },
14+
{ severity = s.WARN },
15+
{ severity = s.ERROR },
16+
}
17+
end
618

7-
-- Original returns array with one entry per diagnostic element
8-
return (vim.split(string.rep('a', n), ''))
19+
if vim.fn.has('nvim-0.10') == 1 then
20+
vim.diagnostic.count = function(_, _)
21+
local s = vim.diagnostic.severity
22+
return { [s.ERROR] = 4, [s.WARN] = 3, [s.INFO] = 2, [s.HINT] = 1 }
23+
end
924
end

tests/test_statusline.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ T['section_diagnostics()']['works'] = function()
260260
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' E4 W3 I2 H1')
261261

262262
-- Should return predefined string if no diagnostic output
263-
child.lua('vim.lsp.diagnostic.get_count = function(...) return 0 end')
263+
child.lua('vim.diagnostic.get = function(...) return {} end')
264+
child.lua('vim.diagnostic.count = function(...) return {} end')
264265
child.lua('vim.diagnostic.get = function(...) return {} end')
265266
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' -')
266267

0 commit comments

Comments
 (0)