From 9e7b3d434828c1f8d2ddb5656f5361b67a2e7f5f Mon Sep 17 00:00:00 2001 From: robin Date: Tue, 24 Feb 2026 20:25:24 +0100 Subject: [PATCH] refactor(utils): move lsp clients fn to separate components module --- README.md | 10 +++++----- lua/lylla/components.lua | 28 ++++++++++++++++++++++++++++ lua/lylla/utils.lua | 14 -------------- 3 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 lua/lylla/components.lua diff --git a/README.md b/README.md index 019be46..beddd27 100644 --- a/README.md +++ b/README.md @@ -207,18 +207,18 @@ example: only show diagnostics if `vim.diagnostic` is loaded: ### lsp information -lylla utils has a builtin helper for getting the current lsp client. +lylla components has a builtin helper for getting the current lsp client. ```lua -local utils = require("lylla.utils") +local components = require("lylla.components") { lylla.component(function() - local client = utils.get_client() - return client and { + local clients = components.lsp_clients() + return clients and { { { "lsp :: " }, { client } }, } - end, { events = { "FileType" } }), + end, { events = { "FileType", "LspAttach" } }), } ``` diff --git a/lua/lylla/components.lua b/lua/lylla/components.lua new file mode 100644 index 0000000..05cb39d --- /dev/null +++ b/lua/lylla/components.lua @@ -0,0 +1,28 @@ +local components = {} + +---@param props { buffer?: integer, limit?: integer, sep?: string, filter?: fun(client: vim.lsp.Client): boolean } +---@return string? +function components.lsp_clients(props) + props = props or {} + local buffer = props.buffer or 0 + + local clients = vim + .iter(vim.lsp.get_clients({ bufnr = buffer })) + :map(function( + client --[[@as vim.lsp.Client]] + ) + if props.filter and not props.filter(client) then + return + end + return client.config.name + end) + :totable() + + if #clients == 0 then + return + end + + return table.concat(clients, props.sep or " + ", 1, vim.F.if_nil(props.limit)) +end + +return components diff --git a/lua/lylla/utils.lua b/lua/lylla/utils.lua index 133de1a..8a50c32 100644 --- a/lua/lylla/utils.lua +++ b/lua/lylla/utils.lua @@ -1,19 +1,5 @@ local utils = {} -function utils.get_client() - local buf_ft = vim.api.nvim_get_option_value("filetype", { buf = 0 }) - local result = vim.iter(vim.lsp.get_clients({ bufnr = 0 })):find(function( - client --[[@as vim.lsp.Client]] - ) - return vim.iter(ipairs(client.config.filetypes)):any(function(_, ft) - return ft == buf_ft - end) - end) - if result then - return result.config.name - end -end - --- flatten list so all children have level of depth ---@param lst table ---@param maxdepth integer -- 2.51.2