From e172dd599cff2ae5267871b5e33fb42934c4df43 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 7 Mar 2024 11:48:43 +0000 Subject: refactor: general cleanup --- lua/lspconfig/manager.lua | 30 +++++++++++++++--------------- lua/lspconfig/ui/lspinfo.lua | 8 ++++---- lua/lspconfig/util.lua | 19 ++++++++++++++++--- 3 files changed, 35 insertions(+), 22 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/manager.lua b/lua/lspconfig/manager.lua index ae648a11..9940d2a2 100644 --- a/lua/lspconfig/manager.lua +++ b/lua/lspconfig/manager.lua @@ -23,7 +23,7 @@ local function check_in_workspace(client, root_dir) end --- @class lspconfig.Manager ---- @field _clients table +--- @field _clients table --- @field config lspconfig.Config --- @field make_config fun(root_dir: string): lspconfig.Config local M = {} @@ -42,9 +42,12 @@ function M.new(config, make_config) end --- @private -function M:_get_client_from_cache(root_dir, client_name) - local clients = self._clients - if vim.tbl_count(clients) == 0 then +--- @param clients table +--- @param root_dir string +--- @param client_name string +--- @return vim.lsp.Client? +local function get_client(clients, root_dir, client_name) + if vim.tbl_isempty(clients) then return end @@ -57,15 +60,12 @@ function M:_get_client_from_cache(root_dir, client_name) end end - local all_client_ids = {} - vim.tbl_map(function(val) - vim.list_extend(all_client_ids, { unpack(val) }) - end, clients) - - for _, id in ipairs(all_client_ids) do - local client = lsp.get_client_by_id(id) - if client and client.name == client_name then - return client + for _, ids in pairs(clients) do + for _, id in ipairs(ids) do + local client = lsp.get_client_by_id(id) + if client and client.name == client_name then + return client + end end end end @@ -181,7 +181,7 @@ end --- @param client vim.lsp.Client --- @param single_file boolean function M:_attach_after_client_initialized(bufnr, new_config, root_dir, client, single_file) - local timer = assert(vim.loop.new_timer()) + local timer = assert(uv.new_timer()) timer:start( 0, 10, @@ -201,7 +201,7 @@ end function M:add(root_dir, single_file, bufnr) root_dir = util.path.sanitize(root_dir) local new_config = self.make_config(root_dir) - local client = self:_get_client_from_cache(root_dir, new_config.name) + local client = get_client(self._clients, root_dir, new_config.name) ---If single_file_mode is false then root_dir should match client otherwise start a new client if not client or (not single_file and client.config.root_dir and client.config.root_dir ~= root_dir) then diff --git a/lua/lspconfig/ui/lspinfo.lua b/lua/lspconfig/ui/lspinfo.lua index d57c1cb8..704c344c 100644 --- a/lua/lspconfig/ui/lspinfo.lua +++ b/lua/lspconfig/ui/lspinfo.lua @@ -120,7 +120,7 @@ local function make_config_info(config, bufnr) return lines end ----@param client table +---@param client vim.lsp.Client ---@param fname string local function make_client_info(client, fname) local client_info = {} @@ -200,7 +200,7 @@ return function() local win_info = windows.percentage_range_window(0.8, 0.7) local bufnr, win_id = win_info.bufnr, win_info.win_id - api.nvim_buf_set_option(bufnr, 'bufhidden', 'wipe') + vim.bo.bufhidden = 'wipe' local buf_lines = {} @@ -274,8 +274,8 @@ return function() local fmt_buf_lines = indent_lines(buf_lines, ' ') api.nvim_buf_set_lines(bufnr, 0, -1, true, fmt_buf_lines) - api.nvim_buf_set_option(bufnr, 'modifiable', false) - api.nvim_buf_set_option(bufnr, 'filetype', 'lspinfo') + vim.bo.modifiable = false + vim.bo.filetype = 'lspinfo' local augroup = api.nvim_create_augroup('lspinfo', { clear = false }) diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 176365d1..073b0e12 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -100,6 +100,8 @@ M.path = (function() return path:gsub('([%[%]%