diff options
| author | William Boman <william@redwill.se> | 2022-03-06 21:48:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-06 21:48:29 +0100 |
| commit | dc39ce90f99a77699317bd31d95ce970690a4624 (patch) | |
| tree | 901e89bacca9b0d370c694fcd5a88cf2e1ae768e /lua/nvim-lsp-installer/server.lua | |
| parent | fix(fetch): shift args to put callback arg last (diff) | |
| download | mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.gz mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.bz2 mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.lz mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.xz mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.zst mason-dc39ce90f99a77699317bd31d95ce970690a4624.zip | |
run server installation in async execution context (#525)
Diffstat (limited to 'lua/nvim-lsp-installer/server.lua')
| -rw-r--r-- | lua/nvim-lsp-installer/server.lua | 81 |
1 files changed, 36 insertions, 45 deletions
diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index 948d11db..5597bba7 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -1,4 +1,5 @@ local dispatcher = require "nvim-lsp-installer.dispatcher" +local a = require "nvim-lsp-installer.core.async" local fs = require "nvim-lsp-installer.fs" local log = require "nvim-lsp-installer.log" local platform = require "nvim-lsp-installer.platform" @@ -207,57 +208,47 @@ end ---@param context ServerInstallContext ---@param callback ServerInstallCallback function M.Server:install_attached(context, callback) - context.receipt = receipt.InstallReceiptBuilder.new() - context.receipt:with_start_time(vim.loop.gettimeofday()) - local context_ok, context_err = pcall(self._setup_install_context, self, context) - if not context_ok then - log.error("Failed to setup installation context.", context_err) - callback(false) - return - end - local install_ok, install_err = pcall( - self._installer, - self, - vim.schedule_wrap(function(success) - if success then - if not self:promote_install_dir(context.install_dir) then - context.stdio_sink.stderr( - ("Failed to promote the temporary installation directory %q.\n"):format(context.install_dir) - ) - pcall(fs.rmrf, self:get_tmp_install_dir()) - pcall(fs.rmrf, context.install_dir) - callback(false) - return - end + a.run( + function() + context.receipt = receipt.InstallReceiptBuilder.new() + context.receipt:with_start_time(vim.loop.gettimeofday()) - -- 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()) + 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.") - self:_write_receipt(context.receipt) + 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 - -- 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) - callback(true) - else - pcall(fs.rmrf, self:get_tmp_install_dir()) + 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) - callback(false) + log.fmt_error("Server installation failed, server_name=%s, error=%s", self.name, result) + context.stdio_sink.stderr(tostring(result) .. "\n") end - end), - context + -- 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) ) - if not install_ok then - log.error("Installer raised an unexpected error.", install_err) - context.stdio_sink.stderr(tostring(install_err) .. "\n") - callback(false) - end end function M.Server:uninstall() |
