Skip to content

Commit 4dc176a

Browse files
committed
(mini.statusline) Track LSP attach/detach manually.
1 parent d041a9a commit 4dc176a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

lua/mini/statusline.lua

+18-5
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,9 @@ end
293293
---
294294
---@return __statusline_section
295295
MiniStatusline.section_diagnostics = function(args)
296-
-- Assumption: there are no attached clients if table
297-
-- `vim.lsp.buf_get_clients()` is empty
298-
local hasnt_attached_client = next(vim.lsp.buf_get_clients()) == nil
299-
local dont_show_lsp = MiniStatusline.is_truncated(args.trunc_width) or H.isnt_normal_buffer() or hasnt_attached_client
300-
if dont_show_lsp then return '' end
296+
_G.n_attached_lsp = H.n_attached_lsp
297+
local dont_show = MiniStatusline.is_truncated(args.trunc_width) or H.isnt_normal_buffer() or H.has_no_lsp_attached()
298+
if dont_show then return '' end
301299

302300
-- Construct string parts
303301
local counts = H.get_diagnostic_count()
@@ -426,6 +424,9 @@ H.diagnostic_levels = {
426424
{ name = 'HINT', sign = 'H' },
427425
}
428426

427+
-- Count of attached LSP clients per buffer id
428+
H.n_attached_lsp = {}
429+
429430
-- Helper functionality =======================================================
430431
-- Settings -------------------------------------------------------------------
431432
H.setup_config = function(config)
@@ -470,6 +471,14 @@ H.create_autocommands = function()
470471

471472
local set_inactive = function() vim.wo.statusline = '%!v:lua.MiniStatusline.inactive()' end
472473
au({ 'WinLeave', 'BufLeave' }, '*', set_inactive, 'Set inactive statusline')
474+
475+
if vim.fn.has('nvim-0.8') == 1 then
476+
local make_track_lsp = function(increment)
477+
return function(data) H.n_attached_lsp[data.buf] = (H.n_attached_lsp[data.buf] or 0) + increment end
478+
end
479+
au('LspAttach', '*', make_track_lsp(1), 'Track LSP clients')
480+
au('LspDetach', '*', make_track_lsp(-1), 'Track LSP clients')
481+
end
473482
end
474483

475484
--stylua: ignore
@@ -583,6 +592,10 @@ H.get_filetype_icon = function()
583592
return devicons.get_icon(file_name, file_ext, { default = true })
584593
end
585594

595+
H.has_no_lsp_attached = function() return (H.n_attached_lsp[vim.api.nvim_get_current_buf()] or 0) == 0 end
596+
597+
if vim.fn.has('nvim-0.8') == 0 then H.has_no_lsp_attached = function() return #vim.lsp.buf_get_clients() == 0 end end
598+
586599
H.get_diagnostic_count = function()
587600
local res = {}
588601
for _, d in ipairs(vim.diagnostic.get(0)) do

tests/dir-statusline/mock-diagnostics.lua

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
vim.lsp.buf_get_clients = function() return { 'mock client' } end
2+
if vim.fn.has('nvim-0.8') == 1 then
3+
vim.api.nvim_exec_autocmds('LspAttach', { data = { client_id = 1 } })
4+
_G.detach_lsp = function() vim.api.nvim_exec_autocmds('LspDetach', { data = { client_id = 1 } }) end
5+
end
26

37
vim.diagnostic.get = function(_, _)
48
local s = vim.diagnostic.severity

tests/test_statusline.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ T['section_diagnostics()']['works'] = function()
267267

268268
-- Should return empty string if no LSP client attached
269269
child.lua('vim.lsp.buf_get_clients = function() return {} end')
270+
if child.fn.has('nvim-0.8') == 1 then child.lua('_G.detach_lsp()') end
270271
eq(child.lua_get('MiniStatusline.section_diagnostics({})'), '')
271272
end
272273

@@ -575,12 +576,12 @@ T['Default content']['active'] = new_set({
575576

576577
mock_devicons()
577578
mock_gitsigns()
578-
mock_diagnostics()
579579
mock_file(10)
580580

581581
-- Mock filename section to use relative path for consistent screenshots
582582
child.lua([[MiniStatusline.section_filename = function() return '%f%m%r' end]])
583583
child.cmd('edit ' .. vim.fn.fnamemodify(mocked_filepath, ':.'))
584+
mock_diagnostics()
584585
type_keys('/a', '<CR>')
585586
end,
586587
post_case = unmock_file,

0 commit comments

Comments
 (0)