| 1 |
local lualine_require = require('lualine_require') |
| 2 |
|
| 3 |
local M = lualine_require.require('lualine.component'):extend() |
| 4 |
|
| 5 |
local default_options = { |
| 6 |
icon = '', -- f013 |
| 7 |
symbols = { |
| 8 |
-- Use standard unicode characters for the spinner and done symbols: |
| 9 |
spinner = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }, |
| 10 |
done = '✓', |
| 11 |
separator = ' ', |
| 12 |
}, |
| 13 |
-- List of LSP names to ignore (e.g., `null-ls`): |
| 14 |
ignore_lsp = {}, |
| 15 |
show_name = true, |
| 16 |
} |
| 17 |
|
| 18 |
function M:init(options) |
| 19 |
-- Run `super()`. |
| 20 |
M.super.init(self, options) |
| 21 |
|
| 22 |
-- Apply default options. |
| 23 |
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options) |
| 24 |
|
| 25 |
-- Apply symbols. |
| 26 |
self.symbols = self.options.symbols or {} |
| 27 |
|
| 28 |
---The difference between the `begin` and `end` progress events for each LSP. |
| 29 |
--- |
| 30 |
---@type table<integer, integer> |
| 31 |
self.lsp_work_by_client_id = {} |
| 32 |
|
| 33 |
-- Listen to progress updates only if `nvim` supports the `LspProgress` event. |
| 34 |
pcall(vim.api.nvim_create_autocmd, 'LspProgress', { |
| 35 |
desc = 'Update the Lualine LSP status component with progress', |
| 36 |
group = vim.api.nvim_create_augroup('lualine_lsp_progress', {}), |
| 37 |
---@param event {data: {client_id: integer, params: lsp.ProgressParams}} |
| 38 |
callback = function(event) |
| 39 |
local kind = event.data.params.value.kind |
| 40 |
local client_id = event.data.client_id |
| 41 |
|
| 42 |
local work = self.lsp_work_by_client_id[client_id] or 0 |
| 43 |
local work_change = kind == 'begin' and 1 or (kind == 'end' and -1 or 0) |
| 44 |
|
| 45 |
self.lsp_work_by_client_id[client_id] = math.max(work + work_change, 0) |
| 46 |
|
| 47 |
-- Refresh Lualine to update the LSP status symbol if it changed. |
| 48 |
if (work == 0 and work_change > 0) or (work == 1 and work_change < 0) then |
| 49 |
require('lualine').refresh() |
| 50 |
end |
| 51 |
end, |
| 52 |
}) |
| 53 |
end |
| 54 |
|
| 55 |
function M:update_status() |
| 56 |
local result = {} |
| 57 |
local processed = {} |
| 58 |
|
| 59 |
-- Backwards-compatible function to get the active LSP clients. |
| 60 |
---@diagnostic disable-next-line: deprecated |
| 61 |
local get_lsp_clients = vim.lsp.get_clients or vim.lsp.get_active_clients |
| 62 |
local clients = get_lsp_clients { bufnr = vim.api.nvim_get_current_buf() } |
| 63 |
|
| 64 |
-- Backwards-compatible function to get the current time in nanoseconds. |
| 65 |
local hrtime = (vim.uv or vim.loop).hrtime |
| 66 |
-- Advance the spinner every 80ms only once, not for each client (otherwise the spinners will skip steps). |
| 67 |
-- NOTE: the spinner symbols table is 1-indexed. |
| 68 |
local spinner_symbol = self.symbols.spinner[math.floor(hrtime() / (1e6 * 80)) % #self.symbols.spinner + 1] |
| 69 |
|
| 70 |
for _, client in ipairs(clients) do |
| 71 |
local status |
| 72 |
local work = self.lsp_work_by_client_id[client.id] |
| 73 |
if work ~= nil and work > 0 then |
| 74 |
status = spinner_symbol |
| 75 |
elseif work ~= nil and work == 0 then |
| 76 |
status = self.symbols.done |
| 77 |
end |
| 78 |
|
| 79 |
-- Backwards-compatible function to check if a list contains a value. |
| 80 |
local list_contains = vim.list_contains or vim.tbl_contains |
| 81 |
-- Append the status to the LSP only if it supports progress reporting and is not ignored. |
| 82 |
if not processed[client.name] and not list_contains(self.options.ignore_lsp, client.name) then |
| 83 |
local status_display = ((status and status ~= '') and (' ' .. status) or '') |
| 84 |
if self.options.show_name then |
| 85 |
table.insert(result, client.name .. status_display) |
| 86 |
else |
| 87 |
table.insert(result, status_display) |
| 88 |
end |
| 89 |
processed[client.name] = true |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
return table.concat(result, self.symbols.separator) |
| 94 |
end |
| 95 |
|
| 96 |
return M |