Skip to content

Commit

Permalink
fix(statusline)!: make section_diagnostics() depend only on diagnostic
Browse files Browse the repository at this point in the history
Details:
- Depend only on if there are actually present diagnostic entries.
  This allows showing in not normal buffers and if there is no LSP
  server attached.
  Do not show ' -' if there is no diagnostics, as this will be
  **always** shown, which is not needed.
- Use "Diag" fallback icon instead of "LSP".

Resolve #687
  • Loading branch information
echasnovski committed May 23, 2024
1 parent 5e11636 commit f536bae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@

## mini.statusline

- BREAKING: `section_diagnostics()` now depends only on defined diagnostic. This means:
- 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".
- 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'.
- BREAKING FEATURE: update default active content to use both `section_git()` and `section_diff()`.
Expand Down
4 changes: 2 additions & 2 deletions doc/mini-statusline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ Return ~
`MiniStatusline.section_diagnostics`({args})
Section for Neovim's builtin diagnostics

Shows nothing if there is no attached LSP clients or for short output.
Otherwise uses builtin Neovim capabilities to compute and show number of
Shows nothing if diagnostics is disabled, no diagnostic is set, or for short
output. Otherwise uses |vim.diagnostic.get()| to compute and show number of
errors ('E'), warnings ('W'), information ('I'), and hints ('H').

Short output is returned if window width is lower than `args.trunc_width`.
Expand Down
23 changes: 8 additions & 15 deletions lua/mini/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ end

--- Section for Neovim's builtin diagnostics
---
--- Shows nothing if there is no attached LSP clients or for short output.
--- Otherwise uses builtin Neovim capabilities to compute and show number of
--- Shows nothing if diagnostics is disabled, no diagnostic is set, or for short
--- output. Otherwise uses |vim.diagnostic.get()| to compute and show number of
--- errors ('E'), warnings ('W'), information ('I'), and hints ('H').
---
--- Short output is returned if window width is lower than `args.trunc_width`.
Expand All @@ -318,8 +318,7 @@ end
---
---@return __statusline_section
MiniStatusline.section_diagnostics = function(args)
local dont_show = MiniStatusline.is_truncated(args.trunc_width) or H.isnt_normal_buffer() or H.has_no_lsp_attached()
if dont_show or H.diagnostic_is_disabled() then return '' end
if MiniStatusline.is_truncated(args.trunc_width) or H.diagnostic_is_disabled() then return '' end

-- Construct string parts
local count = H.diagnostic_get_count()
Expand All @@ -329,11 +328,11 @@ MiniStatusline.section_diagnostics = function(args)
-- Add level info only if diagnostic is present
if n > 0 then table.insert(t, ' ' .. level.sign .. n) end
end
if #t == 0 then return '' end

local use_icons = H.use_icons or H.get_config().use_icons
local icon = args.icon or (use_icons and '' or 'LSP')
if vim.tbl_count(t) == 0 then return ('%s -'):format(icon) end
return string.format('%s%s', icon, table.concat(t, ''))
local icon = args.icon or (use_icons and '' or 'Diag')
return icon .. table.concat(t, '')
end

--- Section for file name
Expand Down Expand Up @@ -370,9 +369,8 @@ end
MiniStatusline.section_fileinfo = function(args)
local filetype = vim.bo.filetype

-- Don't show anything if can't detect file type or not inside a "normal
-- buffer"
if (filetype == '') or H.isnt_normal_buffer() then return '' end
-- Don't show anything if no filetype or not inside a "normal buffer"
if filetype == '' or vim.bo.buftype ~= '' then return '' end

-- Add filetype icon
H.ensure_get_icon()
Expand Down Expand Up @@ -604,11 +602,6 @@ end
H.default_content_inactive = function() return '%#MiniStatuslineInactive#%F%=' end

-- Utilities ------------------------------------------------------------------
H.isnt_normal_buffer = function()
-- For more information see ":h buftype"
return vim.bo.buftype ~= ''
end

H.get_filesize = function()
local size = vim.fn.getfsize(vim.fn.getreg('%'))
if size < 1024 then
Expand Down
22 changes: 13 additions & 9 deletions tests/test_statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,14 @@ T['section_diagnostics()'] = new_set({ hooks = { pre_case = mock_diagnostics } }
T['section_diagnostics()']['works'] = function()
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' E4 W3 I2 H1')

-- Should return predefined string if no diagnostic output
child.lua('vim.diagnostic.get = function(...) return {} end')
child.lua('vim.diagnostic.count = function(...) return {} end')
child.lua('vim.diagnostic.get = function(...) return {} end')
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' -')

-- Should return empty string if no LSP client attached
-- Should not depend on LSP server attached
child.lua('vim.lsp.buf_get_clients = function() return {} end')
if child.fn.has('nvim-0.8') == 1 then child.lua('_G.detach_lsp()') end
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' E4 W3 I2 H1')

-- Should return empty string if no diagnostic entries defined
child.lua('vim.diagnostic.get = function(...) return {} end')
child.lua('vim.diagnostic.count = function(...) return {} end')
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), '')
end

Expand All @@ -333,15 +332,20 @@ end

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

-- Should also use buffer local config
child.b.ministatusline_config = { use_icons = true }
eq(child.lua_get([[MiniStatusline.section_diagnostics({})]]), ' E4 W3 I2 H1')
end

T['section_diagnostics()']['is shown only in normal buffers'] = function()
T['section_diagnostics()']['works in not normal buffers'] = function()
-- Should return empty string if there is no diagnostic defined
child.cmd('help')
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), ' E4 W3 I2 H1')

child.lua('vim.diagnostic.get = function(...) return {} end')
child.lua('vim.diagnostic.count = function(...) return {} end')
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), '')
end

Expand Down

0 comments on commit f536bae

Please sign in to comment.