diff options
| author | Anton Kastritskii <halloy52@gmail.com> | 2025-07-30 02:53:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-29 18:53:57 -0700 |
| commit | b79ff967ac91dce40f3598ea407c5ccaa2929250 (patch) | |
| tree | 6641dfd6bcaa82a9bcef44f09db8f919f9e2746b /runtime/lua/vim/diagnostic.lua | |
| parent | 807a65b2da5390aa930ea11c12df2e60174420f1 (diff) | |
| download | rneovim-b79ff967ac91dce40f3598ea407c5ccaa2929250.tar.gz rneovim-b79ff967ac91dce40f3598ea407c5ccaa2929250.tar.bz2 rneovim-b79ff967ac91dce40f3598ea407c5ccaa2929250.zip | |
feat(statusline): vim.diagnostic.status() #33723
Problem:
Not easy to get a status string for diagnostics.
Solution:
- Add vim.diagnostic.status().
- Add it to the default 'statusline'.
Diffstat (limited to 'runtime/lua/vim/diagnostic.lua')
| -rw-r--r-- | runtime/lua/vim/diagnostic.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 6ea6436c5b..1175ec6518 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -2840,4 +2840,42 @@ function M.fromqflist(list) return diagnostics end +--- 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` +--- +--- To customise appearance, set diagnostic signs text with +--- ```lua +--- vim.diagnostic.config({ +--- signs = { text = { [vim.diagnostic.severity.ERROR] = 'e', ... } } +--- }) +--- ``` +---@param bufnr? integer Buffer number to get diagnostics from. +--- Defaults to 0 for the current buffer +--- +---@return string +function M.status(bufnr) + vim.validate('bufnr', bufnr, 'number', true) + bufnr = bufnr or 0 + local counts = M.count(bufnr) + local user_signs = vim.tbl_get(M.config() --[[@as vim.diagnostic.Opts]], 'signs', 'text') or {} + local signs = vim.tbl_extend('keep', user_signs, { 'E', 'W', 'I', 'H' }) + local result_str = vim + .iter(pairs(counts)) + :map(function(severity, count) + return ('%s:%s'):format(signs[severity], count) + end) + :join(' ') + + return result_str +end + +vim.api.nvim_create_autocmd('DiagnosticChanged', { + group = vim.api.nvim_create_augroup('nvim.diagnostic.status', {}), + callback = function(ev) + vim.api.nvim__redraw({ buf = ev.buf, statusline = true }) + end, + desc = 'diagnostics component for the statusline', +}) + return M |