Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lua/lualine/components/lsp_status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ local default_options = {
show_name = true,
}

---The difference between the `begin` and `end` progress events for each LSP.
---@type table<integer, integer>
M.lsp_work_by_client_id = {}

function M:init(options)
-- Run `super()`.
M.super.init(self, options)
Expand All @@ -25,11 +29,6 @@ function M:init(options)
-- Apply symbols.
self.symbols = self.options.symbols or {}

---The difference between the `begin` and `end` progress events for each LSP.
---
---@type table<integer, integer>
self.lsp_work_by_client_id = {}

-- Listen to progress updates only if `nvim` supports the `LspProgress` event.
pcall(vim.api.nvim_create_autocmd, 'LspProgress', {
desc = 'Update the Lualine LSP status component with progress',
Expand All @@ -39,10 +38,10 @@ function M:init(options)
local kind = event.data.params.value.kind
local client_id = event.data.client_id

local work = self.lsp_work_by_client_id[client_id] or 0
local work = M.lsp_work_by_client_id[client_id] or 0
local work_change = kind == 'begin' and 1 or (kind == 'end' and -1 or 0)

self.lsp_work_by_client_id[client_id] = math.max(work + work_change, 0)
M.lsp_work_by_client_id[client_id] = math.max(work + work_change, 0)

-- Refresh Lualine to update the LSP status symbol if it changed.
if (work == 0 and work_change > 0) or (work == 1 and work_change < 0) then
Expand All @@ -69,7 +68,7 @@ function M:update_status()

for _, client in ipairs(clients) do
local status
local work = self.lsp_work_by_client_id[client.id]
local work = M.lsp_work_by_client_id[client.id]
if work ~= nil and work > 0 then
status = spinner_symbol
elseif work ~= nil and work == 0 then
Expand Down
6 changes: 3 additions & 3 deletions tests/spec/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -957,12 +957,12 @@ describe('lsp_status component', function()
it('shows LSP done when supported', function()
vim.cmd('edit ' .. file)
stub(vim.lsp, 'get_clients')
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } }
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 3, name = 'lua_ls' } }

local ok = pcall(vim.api.nvim_exec_autocmds, 'LspProgress', {
data = { client_id = 2, params = { value = { kind = 'begin' } } },
data = { client_id = 3, params = { value = { kind = 'begin' } } },
}) and pcall(vim.api.nvim_exec_autocmds, 'LspProgress', {
data = { client_id = 2, params = { value = { kind = 'end' } } },
data = { client_id = 3, params = { value = { kind = 'end' } } },
})

-- Skip assertion if LSP progress updates are not supported by the current `nvim` version.
Expand Down