diff options
| author | William Boman <william@redwill.se> | 2022-04-30 15:44:37 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-30 15:44:37 +0200 |
| commit | cd4dac02a3bb5431979b0812a80dadf6ee088f5e (patch) | |
| tree | 17eef2c3f9a11818accc93e98029ded1baf92998 | |
| parent | Update CUSTOM_SERVERS.md (diff) | |
| download | mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar.gz mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar.bz2 mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar.lz mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar.xz mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.tar.zst mason-cd4dac02a3bb5431979b0812a80dadf6ee088f5e.zip | |
feat: allow excluding servers from automatic installation (#643)
| -rw-r--r-- | README.md | 9 | ||||
| -rw-r--r-- | doc/nvim-lsp-installer.txt | 9 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/data.lua | 12 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/middleware.lua | 19 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/settings.lua | 5 | ||||
| -rw-r--r-- | tests/helpers/lua/test_helpers.lua | 5 | ||||
| -rw-r--r-- | tests/setup/automatic_installation_exclude_spec.lua | 44 | ||||
| -rw-r--r-- | tests/setup/automatic_installation_spec.lua | 43 | ||||
| -rw-r--r-- | tests/setup/ensure_installed_spec.lua | 39 |
9 files changed, 173 insertions, 12 deletions
@@ -114,8 +114,8 @@ Example: ```lua require("nvim-lsp-installer").setup({ - ensure_installed = { "rust_analyzer", "sumneko_lua" }, - automatic_installation = true, + ensure_installed = { "rust_analyzer", "sumneko_lua" }, -- ensure these servers are always installed + automatic_installation = true, -- automatically detect which servers to install (based on which servers are set up via lspconfig) ui = { icons = { server_installed = "✓", @@ -271,6 +271,11 @@ local DEFAULT_SETTINGS = { -- A list of servers to automatically install if they're not already installed. Example: { "rust_analyzer", "sumneko_lua" } ensure_installed = {}, -- Whether servers that are set up (via lspconfig) should be automatically installed if they're not already installed. + -- Can either be: + -- - false: Servers are not automatically installed. + -- - true: All servers set up via lspconfig are automatically installed. + -- - { exclude: string[] }: All servers set up via lspconfig, except the ones provided in the list, are automatically installed. + -- Example: automatic_installation = { exclude = { "rust_analyzer", "solargraph" } } automatic_installation = false, ui = { icons = { diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index b9b05e55..68f865d8 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -158,8 +158,8 @@ Refer to the |nvim-lsp-installer-default-settings| for all available settings. Example: > require("nvim-lsp-installer").setup({ - ensure_installed = { "rust_analyzer", "sumneko_lua" }, - automatic_installation = true, + ensure_installed = { "rust_analyzer", "sumneko_lua" }, -- ensure these servers are always installed + automatic_installation = true, -- automatically detect which servers to install (based on which servers are set up via lspconfig) ui = { icons = { server_installed = "✓", @@ -177,6 +177,11 @@ The following settings are applied by default. > -- A list of servers to automatically install if they're not already installed. Example: { "rust_analyzer", "sumneko_lua" } ensure_installed = {}, -- Whether servers that are set up (via lspconfig) should be automatically installed if they're not already installed. + -- Can either be: + -- - false: Servers are not automatically installed. + -- - true: All servers set up via lspconfig are automatically installed. + -- - { exclude: string[] }: All servers set up via lspconfig, except the ones provided in the list, are automatically installed. + -- Example: automatic_installation = { exclude = { "rust_analyzer", "solargraph" } } automatic_installation = false, ui = { icons = { diff --git a/lua/nvim-lsp-installer/data.lua b/lua/nvim-lsp-installer/data.lua index 264fda80..268d3aec 100644 --- a/lua/nvim-lsp-installer/data.lua +++ b/lua/nvim-lsp-installer/data.lua @@ -1,3 +1,5 @@ +-- TODO: rename this to functional.lua, long overdue + local Data = {} ---@generic T : string @@ -122,14 +124,16 @@ function Data.list_any(list, predicate) return false end +function Data.identity(a) + return a +end + ---@generic T : fun(...) ---@param fn T ----@param cache_key_generator fun(...): string | nil +---@param cache_key_generator (fun(...): string | nil)|nil ---@return T function Data.memoize(fn, cache_key_generator) - cache_key_generator = cache_key_generator or function(a) - return a - end + cache_key_generator = cache_key_generator or Data.identity local cache = {} return function(...) local key = cache_key_generator(...) diff --git a/lua/nvim-lsp-installer/middleware.lua b/lua/nvim-lsp-installer/middleware.lua index 7def4276..b09b51ac 100644 --- a/lua/nvim-lsp-installer/middleware.lua +++ b/lua/nvim-lsp-installer/middleware.lua @@ -1,6 +1,9 @@ local util = require "lspconfig.util" local servers = require "nvim-lsp-installer.servers" local settings = require "nvim-lsp-installer.settings" +local Data = require "nvim-lsp-installer.data" + +local memoize, set_of = Data.memoize, Data.set_of local M = {} @@ -21,14 +24,26 @@ local function merge_in_place(t1, t2) return t1 end +local memoized_set = memoize(set_of) + +---@param server_name string +local function should_auto_install(server_name) + if settings.current.automatic_installation == true then + return true + end + if type(settings.current.automatic_installation) == "table" then + return not memoized_set(settings.current.automatic_installation.exclude)[server_name] + end + return false +end + function M.register_lspconfig_hook() util.on_setup = util.add_hook_before(util.on_setup, function(config) local ok, server = servers.get_server(config.name) if ok then if server:is_installed() then merge_in_place(config, server._default_options) - end - if settings.current.automatic_installation and not server:is_installed() then + elseif should_auto_install(server.name) then server:install() end end diff --git a/lua/nvim-lsp-installer/settings.lua b/lua/nvim-lsp-installer/settings.lua index 44c739db..5cfdae4f 100644 --- a/lua/nvim-lsp-installer/settings.lua +++ b/lua/nvim-lsp-installer/settings.lua @@ -7,6 +7,11 @@ local DEFAULT_SETTINGS = { -- A list of servers to automatically install. Example: { "rust_analyzer", "sumneko_lua" } ensure_installed = {}, -- Whether servers that are set up (via lspconfig) should be automatically installed if they're not already installed. + -- Can either be: + -- - false: Servers are not automatically installed. + -- - true: All servers set up via lspconfig are automatically installed. + -- - { exclude: string[] }: All servers set up via lspconfig, except the ones provided in the list, are automatically installed. + -- Example: automatic_installation = { exclude = { "rust_analyzer", "solargraph" } } automatic_installation = false, ui = { icons = { diff --git a/tests/helpers/lua/test_helpers.lua b/tests/helpers/lua/test_helpers.lua index 206337e1..a9a2426f 100644 --- a/tests/helpers/lua/test_helpers.lua +++ b/tests/helpers/lua/test_helpers.lua @@ -32,10 +32,11 @@ mockx = { } function ServerGenerator(opts) + local name = opts.name or "dummy" return server.Server:new(vim.tbl_deep_extend("force", { - name = "dummy", + name = name, languages = { "dummylang" }, - root_dir = server.get_server_root_path "dummy", + root_dir = server.get_server_root_path(name), homepage = "https://dummylang.org", async = true, installer = function(ctx) diff --git a/tests/setup/automatic_installation_exclude_spec.lua b/tests/setup/automatic_installation_exclude_spec.lua new file mode 100644 index 00000000..bfa5c81b --- /dev/null +++ b/tests/setup/automatic_installation_exclude_spec.lua @@ -0,0 +1,44 @@ +local spy = require "luassert.spy" +local lspconfig = require "lspconfig" +local configs = require "lspconfig.configs" +local servers = require "nvim-lsp-installer.servers" + +describe("automatic_installation_exclude", function() + it( + "should install servers set up via lspconfig", + async_test(function() + local server1_installer_spy = spy.new() + local server2_installer_spy = spy.new() + local server1 = ServerGenerator { + name = "automatic_installation_exclude1", + installer = function() + server1_installer_spy() + end, + } + local server2 = ServerGenerator { + name = "automatic_installation_exclude2", + installer = function() + server2_installer_spy() + end, + } + + servers.register(server1) + servers.register(server2) + + configs[server1.name] = { default_config = {} } + configs[server2.name] = { default_config = {} } + + require("nvim-lsp-installer").setup { + automatic_installation = { exclude = { server2.name } }, + } + + lspconfig[server1.name].setup {} + lspconfig[server2.name].setup {} + + assert.wait_for(function() + assert.spy(server1_installer_spy).was_called(1) + assert.spy(server2_installer_spy).was_called(0) + end) + end) + ) +end) diff --git a/tests/setup/automatic_installation_spec.lua b/tests/setup/automatic_installation_spec.lua new file mode 100644 index 00000000..ed35a63a --- /dev/null +++ b/tests/setup/automatic_installation_spec.lua @@ -0,0 +1,43 @@ +local spy = require "luassert.spy" +local lspconfig = require "lspconfig" +local configs = require "lspconfig.configs" +local servers = require "nvim-lsp-installer.servers" + +describe("automatic_installation", function() + it( + "should install servers set up via lspconfig", + async_test(function() + local server1_installer_spy = spy.new() + local server2_installer_spy = spy.new() + local server1 = ServerGenerator { + name = "automatic_installation1", + installer = function() + server1_installer_spy() + end, + } + local server2 = ServerGenerator { + name = "automatic_installation2", + installer = function() + server2_installer_spy() + end, + } + + servers.register(server1) + servers.register(server2) + + configs[server1.name] = { default_config = {} } + configs[server2.name] = { default_config = {} } + + require("nvim-lsp-installer").setup { + automatic_installation = true, + } + + lspconfig[server1.name].setup {} + + assert.wait_for(function() + assert.spy(server1_installer_spy).was_called(1) + assert.spy(server2_installer_spy).was_called(0) + end) + end) + ) +end) diff --git a/tests/setup/ensure_installed_spec.lua b/tests/setup/ensure_installed_spec.lua new file mode 100644 index 00000000..19bb1c8e --- /dev/null +++ b/tests/setup/ensure_installed_spec.lua @@ -0,0 +1,39 @@ +local spy = require "luassert.spy" +local configs = require "lspconfig.configs" +local servers = require "nvim-lsp-installer.servers" + +describe("ensure_installed", function() + it( + "should install servers", + async_test(function() + local server1_installer_spy = spy.new() + local server2_installer_spy = spy.new() + local server1 = ServerGenerator { + name = "ensure_installed1", + installer = function() + server1_installer_spy() + end, + } + local server2 = ServerGenerator { + name = "ensure_installed2", + installer = function() + server2_installer_spy() + end, + } + + servers.register(server1) + servers.register(server2) + + configs[server1.name] = { default_config = {} } + configs[server2.name] = { default_config = {} } + + require("nvim-lsp-installer").setup { + ensure_installed = { server1.name, server2.name }, + } + assert.wait_for(function() + assert.spy(server1_installer_spy).was_called(1) + assert.spy(server2_installer_spy).was_called(1) + end) + end) + ) +end) |
