From 8603cdc1692f2c3078e328a2ed9554cf9047594d Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 28 Apr 2022 17:14:21 +0200 Subject: [PATCH] feat: add ensure_installed to setup table (#637) --- README.md | 3 +++ doc/nvim-lsp-installer.txt | 2 ++ lua/nvim-lsp-installer.lua | 19 +++++++++++++++++++ lua/nvim-lsp-installer/settings.lua | 2 ++ 4 files changed, 26 insertions(+) diff --git a/README.md b/README.md index acde37e9a..d22b6c12d 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Example: ```lua require("nvim-lsp-installer").setup({ + ensure_installed = { "sumneko_lua", "rust_analyzer" }, ui = { icons = { server_installed = "✓", @@ -266,6 +267,8 @@ Illustrations in the logo are derived from [@Kaligule](https://schauderbasis.de/ ```lua local DEFAULT_SETTINGS = { + -- A list of servers to automatically install. Example: { "rust_analyzer", "sumneko_lua" } + ensure_installed = {}, ui = { icons = { -- The list icon to use for installed servers. diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index 68e1ec5bf..c7c5e28bc 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -172,6 +172,8 @@ Example: > The following settings are applied by default. > local DEFAULT_SETTINGS = { + -- A list of servers to automatically install. Example: { "rust_analyzer", "sumneko_lua" } + ensure_installed = {}, ui = { icons = { -- The list icon to use for installed servers. diff --git a/lua/nvim-lsp-installer.lua b/lua/nvim-lsp-installer.lua index a910440e8..ab566551a 100644 --- a/lua/nvim-lsp-installer.lua +++ b/lua/nvim-lsp-installer.lua @@ -14,6 +14,22 @@ local M = {} M.settings = settings.set +---@param server_identifiers string[] +local function ensure_installed(server_identifiers) + local candidates = {} + for _, server_identifier in ipairs(server_identifiers) do + local server_name, version = servers.parse_server_identifier(server_identifier) + local ok, server = servers.get_server(server_name) + if ok and not server:is_installed() then + table.insert(candidates, server_name) + server:install(version) + end + end + if #candidates > 0 then + notify("Installing LSP servers: " .. table.concat(candidates, ", ")) + end +end + ---@param config table function M.setup(config) if config then @@ -21,6 +37,9 @@ function M.setup(config) end settings.uses_new_setup = true require("nvim-lsp-installer.middleware").register_lspconfig_hook() + vim.schedule(function() + ensure_installed(settings.current.ensure_installed) + end) end M.info_window = { diff --git a/lua/nvim-lsp-installer/settings.lua b/lua/nvim-lsp-installer/settings.lua index 87499354a..7a36e2178 100644 --- a/lua/nvim-lsp-installer/settings.lua +++ b/lua/nvim-lsp-installer/settings.lua @@ -4,6 +4,8 @@ local M = {} ---@class LspInstallerSettings local DEFAULT_SETTINGS = { + -- A list of servers to automatically install. Example: { "rust_analyzer", "sumneko_lua" } + ensure_installed = {}, ui = { icons = { -- The list icon to use for installed servers.