diff --git a/CHANGELOG.md b/CHANGELOG.md index 71444340..b9571e14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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()`. diff --git a/doc/mini-statusline.txt b/doc/mini-statusline.txt index ab20aa12..4da0a4e0 100644 --- a/doc/mini-statusline.txt +++ b/doc/mini-statusline.txt @@ -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`. diff --git a/lua/mini/statusline.lua b/lua/mini/statusline.lua index c8279223..24348b7f 100644 --- a/lua/mini/statusline.lua +++ b/lua/mini/statusline.lua @@ -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`. @@ -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() @@ -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 @@ -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() @@ -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 diff --git a/tests/test_statusline.lua b/tests/test_statusline.lua index 38deb993..fd6d5c3e 100644 --- a/tests/test_statusline.lua +++ b/tests/test_statusline.lua @@ -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 @@ -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