diff options
| author | William Boman <william@redwill.se> | 2021-04-05 02:47:57 +0200 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2021-04-05 02:48:17 +0200 |
| commit | 12291707e5838dc90eeb5643dd16424e23937b82 (patch) | |
| tree | b949b257b67994770d28757eb576267433bd1257 /lua/nvim-lsp-installer/installer.lua | |
| parent | remove capabilities (diff) | |
| download | mason-12291707e5838dc90eeb5643dd16424e23937b82.tar mason-12291707e5838dc90eeb5643dd16424e23937b82.tar.gz mason-12291707e5838dc90eeb5643dd16424e23937b82.tar.bz2 mason-12291707e5838dc90eeb5643dd16424e23937b82.tar.lz mason-12291707e5838dc90eeb5643dd16424e23937b82.tar.xz mason-12291707e5838dc90eeb5643dd16424e23937b82.tar.zst mason-12291707e5838dc90eeb5643dd16424e23937b82.zip | |
use metatables, more error handling
Diffstat (limited to 'lua/nvim-lsp-installer/installer.lua')
| -rw-r--r-- | lua/nvim-lsp-installer/installer.lua | 138 |
1 files changed, 80 insertions, 58 deletions
diff --git a/lua/nvim-lsp-installer/installer.lua b/lua/nvim-lsp-installer/installer.lua index bba50976..a6789d7d 100644 --- a/lua/nvim-lsp-installer/installer.lua +++ b/lua/nvim-lsp-installer/installer.lua @@ -20,18 +20,23 @@ local function escape_quotes(str) end local function get_server_installer(server) - return require('nvim-lsp-installer.installers.' .. server) + return pcall(require, 'nvim-lsp-installer.installers.' .. server) end function M.get_available_servers() return _INSTALLERS end function M.get_installed_servers() local installed_servers = {} - for _, server in ipairs(M.get_available_servers()) do - local module = get_server_installer(server) - if os.execute('test -d ' .. escape_quotes(module.root_dir)) == 0 then + for _, server in pairs(M.get_available_servers()) do + local ok, module = get_server_installer(server) + if not ok then + vim.api.nvim_err_writeln("Unable to find installer for " .. server) + goto continue + end + if module:is_installed() then table.insert(installed_servers, module) end + ::continue:: end return installed_servers end @@ -46,44 +51,93 @@ function M.get_uninstalled_servers() ) end -local function _uninstall(server) - local installer = get_server_installer(server) +function M.install(server) + local ok, installer = get_server_installer(server) + if not ok then + return vim.api.nvim_err_writeln("Unable to find installer for " .. server) + end + local success, error = pcall(installer.install, installer) + if not success then + pcall(installer.uninstall, installer) + return vim.api.nvim_err_writeln("Failed to install " .. server .. ". Error=" .. vim.inspect(error)) + end +end - -- giggity - if os.execute('rm -rf ' .. escape_quotes(installer.root_dir)) ~= 0 then - error('Could not remove LSP server directory ' .. installer.root_dir) +function M.uninstall(server) + local ok, installer = get_server_installer(server) + if not ok then + return vim.api.nvim_err_writeln("Unable to find installer for " .. server) + end + local success, error = pcall(installer.uninstall, installer) + if not success then + vim.api.nvim_err_writeln('Unable to uninstall ' .. server .. '. Error=' .. vim.inspect(error)) + return success end + print("Successfully uninstalled " .. server) +end + +function M.get_server_root_path(server) + return vim.fn.stdpath('data') .. "/lsp_servers/" .. server end -local function _install(server) - local installer = get_server_installer(server) +M.Installer = {} +M.Installer.__index = M.Installer - if installer.pre_install then - installer.pre_install() +---@class Installer +function M.Installer:new(opts) + return setmetatable({ + name = opts.name, + _install_cmd = opts.install_cmd, + _root_dir = opts.root_dir, + _default_options = opts.default_options, + _pre_install = opts.pre_install, + }, M.Installer) +end + +function M.Installer:setup(opts) + -- We require the lspconfig server here in order to do it as late as possible. + -- The reason for this is because once a lspconfig server has been imported, it's + -- automatically registered with lspconfig and causes it to show up in :LspInfo and whatnot. + require'lspconfig'[self.name].setup( + vim.tbl_deep_extend('force', self._default_options, opts) + ) +end + +function M.Installer:is_installed() + return os.execute('test -d ' .. escape_quotes(self._root_dir)) == 0 +end + +function M.Installer:create_root_dir() + if os.execute('mkdir -p ' .. escape_quotes(self._root_dir)) ~= 0 then + error('Could not create LSP server directory ' .. self._root_dir) + end +end + +function M.Installer:install() + if self._pre_install then + self._pre_install() end -- We run uninstall after pre_install because we don't want to -- unnecessarily uninstall a server should it no longer pass the -- pre_install check. - _uninstall(server) + self:uninstall() - if os.execute('mkdir -p ' .. escape_quotes(installer.root_dir)) ~= 0 then - error('Could not create LSP server directory ' .. installer.root_dir) - end + self:create_root_dir() local shell = vim.o.shell vim.o.shell = '/bin/bash' vim.cmd [[new]] vim.fn.termopen( - 'set -e;\n' .. installer.install_cmd, + 'set -e;\n' .. self._install_cmd, { - cwd = installer.root_dir, + cwd = self._root_dir, on_exit = function (_, exit_code) if exit_code ~= 0 then - vim.api.nvim_err_writeln("Installer failed for " .. server .. ". Exit code: " .. exit_code) - _uninstall(server) + vim.api.nvim_err_writeln("Installer failed for " .. self.name .. ". Exit code: " .. exit_code) + self:uninstall() else - print("Successfully installed " .. server) + print("Successfully installed " .. self.name) end end @@ -93,44 +147,12 @@ local function _install(server) vim.cmd([[startinsert]]) -- so that the buffer tails the term log nicely end -function M.install(server) - local success, error = pcall(_install, server) - if not success then - pcall(_uninstall, server) - vim.api.nvim_err_writeln("Failed to install " .. server .. ". Error=" .. vim.inspect(error)) - end - return success -end - -function M.uninstall(server) - local success, error = pcall(_uninstall, server) - if not success then - vim.api.nvim_err_writeln('Unable to uninstall ' .. server .. '. Error=' .. vim.inspect(error)) - return success +function M.Installer:uninstall() + -- giggity + if os.execute('rm -rf ' .. escape_quotes(self._root_dir)) ~= 0 then + error('Could not remove LSP server directory ' .. self._root_dir) end - print("Successfully uninstalled " .. server) - return success -end -function M.get_server_root_path(server) - return vim.fn.stdpath('data') .. "/lsp_servers/" .. server -end - -function M.create_lsp_config_installer(module) - return { - name = module.name, - install_cmd = module.install_cmd, - root_dir = module.root_dir, - pre_install = module.pre_install, - setup = function(opts) - -- We require the lspconfig server here in order to do it as late as possible. - -- The reason for this is because once a lspconfig server has been imported, it's - -- automatically registered with lspconfig and causes it to show up in :LspInfo and whatnot. - require'lspconfig'[module.name].setup( - vim.tbl_deep_extend('force', module.default_options, opts) - ) - end, - } end return M |
