Skip to content

Commit

Permalink
feat(statusline): allow custom signs in section_diagnostics()
Browse files Browse the repository at this point in the history
Another approach would be to use sign text from
`vim.diagnostic.config().signs.text`. However, it has downsides:
- Users might want to show different severity levels in signcolumn
  and statusline. Not wanting info and hint levels in signcolumn but
  want in statusline seems like a pretty reasonable setup.
- It is only present in Neovim>=0.10. For reverse compatibility, it
  should also use `vim.fn.sign_getdefined()`, which is cumbersome.
- For best performance, some kind of caching should be done (otherwise
  execution time increases from median ~2.6 microseconds to ~4.7).
  Yet it is cumbersome to have it properly updated (and even created)
  without forcing `require('vim.diagnostic')` during startup.

The price to pay for using `args.signs` is that it requires explicit
(possibly duplicating) lines in config via the whole and verbose
`active` content function.

Resolve #769
  • Loading branch information
echasnovski committed May 31, 2024
1 parent 5ce69e4 commit 581d6f9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- Something is shown **only** if there is any diagnostic actually present in the buffer. No diagnostic entries - nothing is shown.
Previously it did not show if there was no LSP servers attached (as initially diagnostics came only from LSP) or buffer was not normal.
- Fallback icon is "Diag" instead of "LSP".
- FEATURE: `section_diagnostics()` now supports `signs` table option to customize signs for severity levels.
- BREAKING FEATURE: `section_git()` now prefers using data from 'mini.git' with fallback on pure HEAD data from 'lewis6991/gistigns.nvim'.
- FEATURE: add `section_diff()` to show data from 'mini.diff' with fallback on diff data from 'lewis6991/gistigns.nvim'.
- FEATURE: add `section_lsp()` to show indicator of LSP servers attached to the buffer.
Expand Down
2 changes: 2 additions & 0 deletions doc/mini-statusline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ Short output is returned if window width is lower than `args.trunc_width`.

Parameters ~
{args} `(table)` Section arguments. Use `args.icon` to supply your own icon.
Use `args.signs` to use custom signs per severity level name. For example:
`{ ERROR = '!', WARN = '?', INFO = '@', HINT = '*' }`

Return ~
`(string)` Section string.
Expand Down
6 changes: 4 additions & 2 deletions lua/mini/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,20 @@ end
--- Short output is returned if window width is lower than `args.trunc_width`.
---
---@param args __statusline_args Use `args.icon` to supply your own icon.
--- Use `args.signs` to use custom signs per severity level name. For example:
--- `{ ERROR = '!', WARN = '?', INFO = '@', HINT = '*' }`
---
---@return __statusline_section
MiniStatusline.section_diagnostics = function(args)
if MiniStatusline.is_truncated(args.trunc_width) or H.diagnostic_is_disabled() then return '' end

-- Construct string parts
local count = H.diagnostic_get_count()
local severity, t = vim.diagnostic.severity, {}
local severity, signs, t = vim.diagnostic.severity, args.signs or {}, {}
for _, level in ipairs(H.diagnostic_levels) do
local n = count[severity[level.name]] or 0
-- Add level info only if diagnostic is present
if n > 0 then table.insert(t, ' ' .. level.sign .. n) end
if n > 0 then table.insert(t, ' ' .. (signs[level.name] or level.sign) .. n) end
end
if #t == 0 then return '' end

Expand Down
7 changes: 7 additions & 0 deletions tests/test_statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ T['section_diagnostics()']['respects `args.icon`'] = function()
eq(child.lua_get([[MiniStatusline.section_diagnostics({icon = 'AAA'})]]), 'AAA E4 W3 I2 H1')
end

T['section_diagnostics()']['respects `args.signs`'] = function()
local out = child.lua_get(
[[MiniStatusline.section_diagnostics({ signs = { ERROR = '!', WARN = '?', INFO = '@', HINT = '*' } })]]
)
eq(out, ' !4 ?3 @2 *1')
end

T['section_diagnostics()']['respects `config.use_icons`'] = function()
child.lua('MiniStatusline.config.use_icons = false')
eq(child.lua_get([[MiniStatusline.section_diagnostics({})]]), 'Diag E4 W3 I2 H1')
Expand Down

0 comments on commit 581d6f9

Please sign in to comment.