mirror of
https://github.com/neovim/neovim.git
synced 2025-12-15 19:05:40 +00:00
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'.
This commit is contained in:
committed by
GitHub
parent
807a65b2da
commit
b79ff967ac
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user