Skip to content

Commit

Permalink
fix(statusline): show diagnostics without lsp
Browse files Browse the repository at this point in the history
The diagnostic section was hidden behind a language server check, which
hides linter results.

Resolve echasnovski#687
  • Loading branch information
Matt Warner committed Apr 11, 2024
1 parent 8371930 commit aec449d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
36 changes: 25 additions & 11 deletions lua/mini/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,33 +281,46 @@ 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()
local severity, t = vim.diagnostic.severity, {}
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
Expand Down Expand Up @@ -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 })
Expand All @@ -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
Expand Down
23 changes: 2 additions & 21 deletions tests/test_statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({})'), '')
Expand Down

0 comments on commit aec449d

Please sign in to comment.