From c6378585140f1267e2c32ea8752e88a9399c4dcf Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 30 Aug 2024 22:27:09 +0200 Subject: [PATCH] Adding the code --- lua/lualine/components/lsp-status.lua | 104 ++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lua/lualine/components/lsp-status.lua diff --git a/lua/lualine/components/lsp-status.lua b/lua/lualine/components/lsp-status.lua new file mode 100644 index 0000000..4072e4d --- /dev/null +++ b/lua/lualine/components/lsp-status.lua @@ -0,0 +1,104 @@ +local M = require('lualine.component'):extend() +local utils = require('lualine.utils.utils') + +local default_options = { + + -- true if the number of lsp clients connected should be shown. + show_count = true, + + -- true if icon should also be color coded. + colored = true, + + -- Colors used. + colors = { + -- Color used if there are one or more clients connected + active = { + fg = utils.extract_color_from_hllist( + 'fg', + { 'DiagnosticOk', 'DiagnosticSignOk' }, + '#89dceb' + ) + }, + -- Color used if there is zero clients connected + inactive = { + fg = utils.extract_color_from_hllist( + 'fg', + { 'DiagnosticError', 'DiagnosticSignError', 'Error' }, + '#f38ba8' + ) + }, + -- Color used for the count. + count = { + fg = utils.extract_color_from_hllist( + 'fg', + { 'StatusLineNormal' }, + '#ffffff' + ) + }, + }, + + -- Icon used. + icons = { + -- Icon used when there is one or more clients connected + active = "", + + -- Icon used when there is zero clients connected. + inactive = "" + } +} + + +function M:init(options) + options = vim.tbl_deep_extend('keep', options or {}, default_options) + M.super.init(self, options) + + if self.options.colored then + self.highlights = { + active = self:create_hl(self.options.colors.active, 'active'), + inactive = self:create_hl(self.options.colors.inactive, 'inactive'), + count = self:create_hl(self.options.colors.count, 'count'), + } + self.hl = {} + for name, highlight_name in pairs(self.highlights) do + self.hl[name] = self:format_hl(highlight_name) + end + end +end + +function M:format(icon, count) + if self.config.show_count then + return string.format("%s %s", icon, count) + end + return string.format("%s", icon) +end + +function M:update_status() + + local num_clients = #vim.tbl_keys(vim.lsp.get_clients()) + + local status = "inactive" + if num_clients > 0 then + status = "active" + end + + local segments = {} + + local icon = self.options.icons[status] + if self.options.colored then + table.insert(segments, self.hl[status] .. icon) + else + table.insert(segments, icon) + end + + if self.options.show_count then + if self.options.colored then + table.insert(segments, self.hl["count"] .. num_clients) + else + table.insert(segments, num_clients) + end + end + + return table.concat(segments, ' ') +end + +return M