From 07bcae45fd54ef9b8c2e663dd414b3384435ebe9 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 22 Nov 2021 08:32:40 +0100 Subject: fix(lsp): avoid installing an overridden server (#1981) --- lua/lvim/lsp/manager.lua | 50 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'lua/lvim/lsp/manager.lua') diff --git a/lua/lvim/lsp/manager.lua b/lua/lvim/lsp/manager.lua index dbb7b87f..ca4dc285 100644 --- a/lua/lvim/lsp/manager.lua +++ b/lua/lvim/lsp/manager.lua @@ -54,39 +54,41 @@ function M.setup(server_name, user_config) if lvim_lsp_utils.is_client_active(server_name) then return end - local servers = require "nvim-lsp-installer.servers" local config = resolve_config(server_name, user_config) + + local servers = require "nvim-lsp-installer.servers" local server_available, requested_server = servers.get_server(server_name) - if server_available then - local install_notification = false - - if not requested_server:is_installed() then - if lvim.lsp.automatic_servers_installation then - Log:debug "Automatic server installation detected" - requested_server:install() - install_notification = true - else - Log:debug(requested_server.name .. " is not managed by the automatic installer") - end - end + local is_overridden = vim.tbl_contains(lvim.lsp.override, server_name) - requested_server:on_ready(function() - if install_notification then - vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO) - end - install_notification = false - requested_server:setup(config) - end) - else - -- since it may not be installed, don't attempt to configure the LSP unless there is a custom provider - local has_custom_provider, _ = pcall(require, "lvim/lsp/providers/" .. server_name) - if has_custom_provider then + if not server_available or is_overridden then + pcall(function() require("lspconfig")[server_name].setup(config) buf_try_add(server_name) + end) + return + end + + local install_notification = false + + if not requested_server:is_installed() then + if lvim.lsp.automatic_servers_installation then + Log:debug "Automatic server installation detected" + requested_server:install() + install_notification = true + else + Log:debug(requested_server.name .. " is not managed by the automatic installer") end end + + requested_server:on_ready(function() + if install_notification then + vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO) + end + install_notification = false + requested_server:setup(config) + end) end return M -- cgit v1.2.3 From e4752692e5f547ef9780161a3ecfc4c3b4552cc5 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Wed, 8 Dec 2021 08:15:56 +0100 Subject: fix(lsp): prevent repeated setup call (#2048) Check if the manager `autocomd` has already been configured, since some servers can take a while to initialize. --- lua/lvim/lsp/manager.lua | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lua/lvim/lsp/manager.lua') diff --git a/lua/lvim/lsp/manager.lua b/lua/lvim/lsp/manager.lua index ca4dc285..7a35f1ff 100644 --- a/lua/lvim/lsp/manager.lua +++ b/lua/lvim/lsp/manager.lua @@ -45,13 +45,28 @@ local function buf_try_add(server_name, bufnr) require("lspconfig")[server_name].manager.try_add_wrapper(bufnr) end +-- check if the manager autocomd has already been configured since some servers can take a while to initialize +-- this helps guarding against a data-race condition where a server can get configured twice +-- which seems to occur only when attaching to single-files +local function client_is_configured(server_name, ft) + ft = ft or vim.bo.filetype + local active_autocmds = vim.split(vim.fn.execute("autocmd FileType " .. ft), "\n") + for _, result in ipairs(active_autocmds) do + if result:match(server_name) then + return true + end + end + return false +end + ---Setup a language server by providing a name ---@param server_name string name of the language server ---@param user_config table [optional] when available it will take predence over any default configurations function M.setup(server_name, user_config) vim.validate { name = { server_name, "string" } } - if lvim_lsp_utils.is_client_active(server_name) then + if lvim_lsp_utils.is_client_active(server_name) or client_is_configured(server_name) then + Log:debug(string.format("[%q] is already configured. Ignoring repeated setup call.", server_name)) return end -- cgit v1.2.3