aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/server.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-04-06 00:25:50 +0200
committerGitHub <noreply@github.com>2022-04-06 00:25:50 +0200
commit999476c324b26223aaf409a84497215e18d74499 (patch)
tree323cc5b072471e5e78ee94b273157779bbcb9611 /lua/nvim-lsp-installer/server.lua
parentfix(taplo): use crate distribution (#576) (diff)
downloadmason-999476c324b26223aaf409a84497215e18d74499.tar
mason-999476c324b26223aaf409a84497215e18d74499.tar.gz
mason-999476c324b26223aaf409a84497215e18d74499.tar.bz2
mason-999476c324b26223aaf409a84497215e18d74499.tar.lz
mason-999476c324b26223aaf409a84497215e18d74499.tar.xz
mason-999476c324b26223aaf409a84497215e18d74499.tar.zst
mason-999476c324b26223aaf409a84497215e18d74499.zip
switch majority of installers to async implementation (#574)
Diffstat (limited to 'lua/nvim-lsp-installer/server.lua')
-rw-r--r--lua/nvim-lsp-installer/server.lua98
1 files changed, 62 insertions, 36 deletions
diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua
index 5597bba7..de6481a4 100644
--- a/lua/nvim-lsp-installer/server.lua
+++ b/lua/nvim-lsp-installer/server.lua
@@ -1,14 +1,17 @@
local dispatcher = require "nvim-lsp-installer.dispatcher"
local a = require "nvim-lsp-installer.core.async"
+local InstallContext = require "nvim-lsp-installer.core.installer.context"
local fs = require "nvim-lsp-installer.fs"
local log = require "nvim-lsp-installer.log"
local platform = require "nvim-lsp-installer.platform"
local settings = require "nvim-lsp-installer.settings"
local installers = require "nvim-lsp-installer.installers"
+local installer = require "nvim-lsp-installer.core.installer"
local servers = require "nvim-lsp-installer.servers"
local status_win = require "nvim-lsp-installer.ui.status-win"
local path = require "nvim-lsp-installer.path"
local receipt = require "nvim-lsp-installer.core.receipt"
+local Optional = require "nvim-lsp-installer.core.optional"
local M = {}
@@ -25,6 +28,7 @@ M.get_server_root_path = servers.get_server_install_path
---@field public deprecated ServerDeprecation|nil @The existence (not nil) of this field indicates this server is depracted.
---@field public languages string[]
---@field private _installer ServerInstallerFunction
+---@field private _async boolean
---@field private _on_ready_handlers fun(server: Server)[]
---@field private _default_options table @The server's default options. This is used in @see Server#setup.
M.Server = {}
@@ -38,9 +42,10 @@ function M.Server:new(opts)
root_dir = opts.root_dir,
homepage = opts.homepage,
deprecated = opts.deprecated,
+ _async = opts.async or false,
languages = opts.languages or {},
_on_ready_handlers = {},
- _installer = type(opts.installer) == "function" and opts.installer or installers.pipe(opts.installer),
+ _installer = opts.installer,
_default_options = opts.default_options,
}, M.Server)
end
@@ -208,47 +213,68 @@ end
---@param context ServerInstallContext
---@param callback ServerInstallCallback
function M.Server:install_attached(context, callback)
- a.run(
- function()
- context.receipt = receipt.InstallReceiptBuilder.new()
- context.receipt:with_start_time(vim.loop.gettimeofday())
-
- a.scheduler()
- self:_setup_install_context(context)
- local async_installer = a.promisify(function(server, context, callback)
- -- args are shifted
- return self._installer(server, callback, context)
- end)
- assert(async_installer(self, context), "Installation failed.")
-
+ if self._async then
+ a.run(function()
+ local install_context = InstallContext.new {
+ name = self.name,
+ boundary_path = settings.current.install_root_dir,
+ stdio_sink = context.stdio_sink,
+ destination_dir = self.root_dir,
+ requested_version = Optional.of_nilable(context.requested_server_version),
+ }
+ installer.execute(install_context, self._installer):get_or_throw()
a.scheduler()
- if not self:promote_install_dir(context.install_dir) then
- error(("Failed to promote the temporary installation directory %q."):format(context.install_dir))
+ dispatcher.dispatch_server_ready(self)
+ for _, on_ready_handler in ipairs(self._on_ready_handlers) do
+ on_ready_handler(self)
end
+ end, callback)
+ else
+ --- Deprecated
+ a.run(
+ function()
+ context.receipt = receipt.InstallReceiptBuilder.new()
+ context.receipt:with_start_time(vim.loop.gettimeofday())
- self:_write_receipt(context.receipt)
+ a.scheduler()
+ self:_setup_install_context(context)
+ local async_installer = a.promisify(function(server, context, callback)
+ local normalized_installer = type(self._installer) == "function" and self._installer
+ or installers.pipe(self._installer)
+ -- args are shifted
+ return normalized_installer(server, callback, context)
+ end)
+ assert(async_installer(self, context), "Installation failed.")
- -- Dispatch the server is ready
- vim.schedule(function()
- dispatcher.dispatch_server_ready(self)
- for _, on_ready_handler in ipairs(self._on_ready_handlers) do
- on_ready_handler(self)
+ a.scheduler()
+ if not self:promote_install_dir(context.install_dir) then
+ error(("Failed to promote the temporary installation directory %q."):format(context.install_dir))
end
+
+ self:_write_receipt(context.receipt)
+
+ -- Dispatch the server is ready
+ vim.schedule(function()
+ dispatcher.dispatch_server_ready(self)
+ for _, on_ready_handler in ipairs(self._on_ready_handlers) do
+ on_ready_handler(self)
+ end
+ end)
+ end,
+ vim.schedule_wrap(function(ok, result)
+ if not ok then
+ pcall(fs.rmrf, context.install_dir)
+ log.fmt_error("Server installation failed, server_name=%s, error=%s", self.name, result)
+ context.stdio_sink.stderr(tostring(result) .. "\n")
+ end
+ -- The tmp dir should in most cases have been "promoted" and already renamed to its final destination,
+ -- but we make sure to delete it should the installer modify the installation working directory during
+ -- installation.
+ pcall(fs.rmrf, self:get_tmp_install_dir())
+ callback(ok)
end)
- end,
- vim.schedule_wrap(function(ok, result)
- if not ok then
- pcall(fs.rmrf, context.install_dir)
- log.fmt_error("Server installation failed, server_name=%s, error=%s", self.name, result)
- context.stdio_sink.stderr(tostring(result) .. "\n")
- end
- -- The tmp dir should in most cases have been "promoted" and already renamed to its final destination,
- -- but we make sure to delete it should the installer modify the installation working directory during
- -- installation.
- pcall(fs.rmrf, self:get_tmp_install_dir())
- callback(ok)
- end)
- )
+ )
+ end
end
function M.Server:uninstall()