From aec449daf38ed61b02f83d4b6196eb1f8b4a57c3 Mon Sep 17 00:00:00 2001 From: Matt Warner Date: Thu, 11 Apr 2024 12:25:41 -0700 Subject: [PATCH] fix(statusline): show diagnostics without lsp The diagnostic section was hidden behind a language server check, which hides linter results. Resolve #687 --- lua/mini/statusline.lua | 36 +++++++++++++++++++++++++----------- tests/test_statusline.lua | 23 ++--------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/lua/mini/statusline.lua b/lua/mini/statusline.lua index 9fc290c5..8a294ece 100644 --- a/lua/mini/statusline.lua +++ b/lua/mini/statusline.lua @@ -281,20 +281,34 @@ MiniStatusline.section_git = function(args) return string.format('%s %s %s', icon, head, signs) end +--- Section for Neovim's builtin lsp +--- +--- Shows nothing if there is no attached lsp +--- Uses LspAttach and LspDetach events to determine if an LSP is attached. +--- +---@param args __statusline_args Use `args.icon` to supply your own icon. +--- +---@return __statusline_section +MiniStatusline.section_lsp = 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 then return '' end + + local icon = args.icon or (H.get_config().use_icons and '' or 'LSP') + return string.format('%s', icon) +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 for short output. +--- Uses builtin Neovim capabilities 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`. --- ----@param args __statusline_args Use `args.icon` to supply your own icon. ---- ---@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 + local dont_show = MiniStatusline.is_truncated(args.trunc_width) or H.isnt_normal_buffer() or H.diagnostic_is_disabled() + if dont_show then return '' end -- Construct string parts local count = H.diagnostic_get_count() @@ -302,12 +316,11 @@ MiniStatusline.section_diagnostics = function(args) 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, string.format(' %s%s', level.sign, n)) end + if n > 0 then table.insert(t, string.format('%s%s', level.sign, n)) end end - local icon = args.icon or (H.get_config().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, '')) + if vim.tbl_count(t) == 0 then return '' end + return string.format('%s', table.concat(t, ' ')) end --- Section for file name @@ -552,6 +565,7 @@ H.default_content_active = function() -- stylua: ignore start local mode, mode_hl = MiniStatusline.section_mode({ trunc_width = 120 }) local git = MiniStatusline.section_git({ trunc_width = 75 }) + local lsp = MiniStatusline.section_lsp({ trunc_width = 75 }) local diagnostics = MiniStatusline.section_diagnostics({ trunc_width = 75 }) local filename = MiniStatusline.section_filename({ trunc_width = 140 }) local fileinfo = MiniStatusline.section_fileinfo({ trunc_width = 120 }) @@ -563,7 +577,7 @@ H.default_content_active = function() -- sections, etc.) return MiniStatusline.combine_groups({ { hl = mode_hl, strings = { mode } }, - { hl = 'MiniStatuslineDevinfo', strings = { git, diagnostics } }, + { hl = 'MiniStatuslineDevinfo', strings = { git, lsp, diagnostics } }, '%<', -- Mark general truncate point { hl = 'MiniStatuslineFilename', strings = { filename } }, '%=', -- End left alignment diff --git a/tests/test_statusline.lua b/tests/test_statusline.lua index c8f9bae5..58cec7e5 100644 --- a/tests/test_statusline.lua +++ b/tests/test_statusline.lua @@ -295,41 +295,22 @@ T['active()/inactive()']['respects `vim.{g,b}.ministatusline_disable`'] = new_se 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') + 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 - 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({})'), '') end T['section_diagnostics()']['respects `args.trunc_width`'] = function() set_width(100) - eq(child.lua_get('MiniStatusline.section_diagnostics({ trunc_width = 100 })'), ' E4 W3 I2 H1') + eq(child.lua_get('MiniStatusline.section_diagnostics({ trunc_width = 100 })'), 'E4 W3 I2 H1') set_width(99) eq(child.lua_get('MiniStatusline.section_diagnostics({ trunc_width = 100 })'), '') end -T['section_diagnostics()']['respects `args.icon`'] = function() - eq(child.lua_get([[MiniStatusline.section_diagnostics({icon = 'A'})]]), 'A E4 W3 I2 H1') - eq(child.lua_get([[MiniStatusline.section_diagnostics({icon = 'AAA'})]]), 'AAA E4 W3 I2 H1') -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') - - -- 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() child.cmd('help') eq(child.lua_get('MiniStatusline.section_diagnostics({})'), '')