From 963df050ea3f65005c8ee047a4ab5d76493bf5a8 Mon Sep 17 00:00:00 2001 From: William Boman Date: Sat, 26 Apr 2025 12:30:48 +0200 Subject: feat: add more capabilities to `automatic_enable` --- lua/mason-lspconfig/automatic_enable.lua | 32 -------------- lua/mason-lspconfig/ensure_installed.lua | 41 ----------------- lua/mason-lspconfig/features/automatic_enable.lua | 54 +++++++++++++++++++++++ lua/mason-lspconfig/features/ensure_installed.lua | 41 +++++++++++++++++ lua/mason-lspconfig/init.lua | 4 +- lua/mason-lspconfig/mappings.lua | 7 --- lua/mason-lspconfig/settings.lua | 16 +++++++ 7 files changed, 113 insertions(+), 82 deletions(-) delete mode 100644 lua/mason-lspconfig/automatic_enable.lua delete mode 100644 lua/mason-lspconfig/ensure_installed.lua create mode 100644 lua/mason-lspconfig/features/automatic_enable.lua create mode 100644 lua/mason-lspconfig/features/ensure_installed.lua (limited to 'lua') diff --git a/lua/mason-lspconfig/automatic_enable.lua b/lua/mason-lspconfig/automatic_enable.lua deleted file mode 100644 index b1f0046..0000000 --- a/lua/mason-lspconfig/automatic_enable.lua +++ /dev/null @@ -1,32 +0,0 @@ -local _ = require "mason-core.functional" -local mappings = require "mason-lspconfig.mappings" -local registry = require "mason-registry" -local settings = require "mason-lspconfig.settings" - ----@param mason_pkg string -local function setup_server(mason_pkg) - local lspconfig_name = mappings.get_mason_map().package_to_lspconfig[mason_pkg] - if not lspconfig_name then - return - end - - -- We don't provide LSP configurations in the lsp/ directory because it risks overriding configurations in a way the - -- user doesn't want. Instead we only override LSP configurations for servers that are installed via Mason. - local ok, config = pcall(require, ("mason-lspconfig.lsp.%s"):format(lspconfig_name)) - if ok then - vim.lsp.config(lspconfig_name, config) - end - - if settings.current.automatic_enable then - vim.lsp.enable(lspconfig_name) - end -end - -_.each(setup_server, registry.get_installed_package_names()) - -registry:on( - "package:install:success", - vim.schedule_wrap(function(pkg) - setup_server(pkg.name) - end) -) diff --git a/lua/mason-lspconfig/ensure_installed.lua b/lua/mason-lspconfig/ensure_installed.lua deleted file mode 100644 index 583feec..0000000 --- a/lua/mason-lspconfig/ensure_installed.lua +++ /dev/null @@ -1,41 +0,0 @@ -local notify = require "mason-lspconfig.notify" -local registry = require "mason-registry" -local settings = require "mason-lspconfig.settings" - ----@param lspconfig_server_name string -local function resolve_package(lspconfig_server_name) - local Optional = require "mason-core.optional" - local server_mapping = require("mason-lspconfig.mappings").get_mason_map() - - return Optional.of_nilable(server_mapping.lspconfig_to_package[lspconfig_server_name]):map(function(package_name) - local ok, pkg = pcall(registry.get_package, package_name) - if ok then - return pkg - end - end) -end - -return function() - for _, server_identifier in ipairs(settings.current.ensure_installed) do - local Package = require "mason-core.package" - - local server_name, version = Package.Parse(server_identifier) - resolve_package(server_name) - :if_present( - ---@param pkg Package - function(pkg) - if not pkg:is_installed() and not pkg:is_installing() then - require("mason-lspconfig.install").install(pkg, version) - end - end - ) - :if_not_present(function() - notify( - ("[mason-lspconfig.nvim] Server %q is not a valid entry in ensure_installed. Make sure to only provide lspconfig server names."):format( - server_name - ), - vim.log.levels.WARN - ) - end) - end -end diff --git a/lua/mason-lspconfig/features/automatic_enable.lua b/lua/mason-lspconfig/features/automatic_enable.lua new file mode 100644 index 0000000..34ae265 --- /dev/null +++ b/lua/mason-lspconfig/features/automatic_enable.lua @@ -0,0 +1,54 @@ +local _ = require "mason-core.functional" +local mappings = require "mason-lspconfig.mappings" +local registry = require "mason-registry" +local settings = require "mason-lspconfig.settings" + +---@param mason_pkg string | Package +local function enable_server(mason_pkg) + if type(mason_pkg) ~= "string" then + mason_pkg = mason_pkg.name + end + local lspconfig_name = mappings.get_mason_map().package_to_lspconfig[mason_pkg] + if not lspconfig_name then + return + end + + local automatic_enable = settings.current.automatic_enable + + if type(automatic_enable) == "table" then + local exclude = automatic_enable.exclude + if exclude then + if _.any(_.equals(lspconfig_name), exclude) then + -- This server is explicitly excluded. + return + end + else + if not _.any(_.equals(lspconfig_name), automatic_enable) then + -- This server is not explicitly enabled. + return + end + end + elseif automatic_enable == false then + return + end + + -- We don't provide LSP configurations in the lsp/ directory because it risks overriding configurations in a way the + -- user doesn't want. Instead we only override LSP configurations for servers that are installed via Mason. + local ok, config = pcall(require, ("mason-lspconfig.lsp.%s"):format(lspconfig_name)) + if ok then + vim.lsp.config(lspconfig_name, config) + end + + vim.lsp.enable(lspconfig_name) +end + +local enable_server_scheduled = vim.schedule_wrap(enable_server) + +return function() + _.each(enable_server, registry.get_installed_package_names()) + + -- We deregister the event handler primarily for testing purposes where .setup() is called multiple times in the + -- same instance + registry:off("package:install:success", enable_server_scheduled) + registry:on("package:install:success", enable_server_scheduled) +end diff --git a/lua/mason-lspconfig/features/ensure_installed.lua b/lua/mason-lspconfig/features/ensure_installed.lua new file mode 100644 index 0000000..583feec --- /dev/null +++ b/lua/mason-lspconfig/features/ensure_installed.lua @@ -0,0 +1,41 @@ +local notify = require "mason-lspconfig.notify" +local registry = require "mason-registry" +local settings = require "mason-lspconfig.settings" + +---@param lspconfig_server_name string +local function resolve_package(lspconfig_server_name) + local Optional = require "mason-core.optional" + local server_mapping = require("mason-lspconfig.mappings").get_mason_map() + + return Optional.of_nilable(server_mapping.lspconfig_to_package[lspconfig_server_name]):map(function(package_name) + local ok, pkg = pcall(registry.get_package, package_name) + if ok then + return pkg + end + end) +end + +return function() + for _, server_identifier in ipairs(settings.current.ensure_installed) do + local Package = require "mason-core.package" + + local server_name, version = Package.Parse(server_identifier) + resolve_package(server_name) + :if_present( + ---@param pkg Package + function(pkg) + if not pkg:is_installed() and not pkg:is_installing() then + require("mason-lspconfig.install").install(pkg, version) + end + end + ) + :if_not_present(function() + notify( + ("[mason-lspconfig.nvim] Server %q is not a valid entry in ensure_installed. Make sure to only provide lspconfig server names."):format( + server_name + ), + vim.log.levels.WARN + ) + end) + end +end diff --git a/lua/mason-lspconfig/init.lua b/lua/mason-lspconfig/init.lua index ff37463..631a78f 100644 --- a/lua/mason-lspconfig/init.lua +++ b/lua/mason-lspconfig/init.lua @@ -27,9 +27,9 @@ function M.setup(config) local registry = require "mason-registry" registry.refresh(vim.schedule_wrap(function() if not platform.is_headless and #settings.current.ensure_installed > 0 then - require "mason-lspconfig.ensure_installed"() + require "lua.mason-lspconfig.features.ensure_installed"() end - require "mason-lspconfig.automatic_enable" + require "lua.mason-lspconfig.features.automatic_enable"() registry.register_package_aliases(_.map(function(server_name) return { server_name } end, require("mason-lspconfig.mappings").get_mason_map().package_to_lspconfig)) diff --git a/lua/mason-lspconfig/mappings.lua b/lua/mason-lspconfig/mappings.lua index 98cc8c4..2523aed 100644 --- a/lua/mason-lspconfig/mappings.lua +++ b/lua/mason-lspconfig/mappings.lua @@ -4,13 +4,6 @@ local registry = require "mason-registry" local M = {} function M.get_mason_map() - if not registry.get_all_package_specs then - return { - lspconfig_to_package = {}, - package_to_lspconfig = {}, - } - end - ---@type table local package_to_lspconfig = {} for _, pkg_spec in ipairs(registry.get_all_package_specs()) do diff --git a/lua/mason-lspconfig/settings.lua b/lua/mason-lspconfig/settings.lua index cadc31e..1c6b3ec 100644 --- a/lua/mason-lspconfig/settings.lua +++ b/lua/mason-lspconfig/settings.lua @@ -7,6 +7,22 @@ local DEFAULT_SETTINGS = { ensure_installed = {}, -- Whether installed servers should automatically be enabled via `:h vim.lsp.enable()`. + -- + -- To exclude certain servers from being automatically enabled: + -- ```lua + -- automatic_enable = { + -- exclude = { "rust_analyzer", "ts_ls" } + -- } + -- ``` + -- + -- To only enable certain servers to be automatically enabled: + -- ```lua + -- automatic_enable = { + -- "lua_ls", + -- "vimls" + -- } + -- ``` + ---@type boolean | string[] | { exclude: string[] } automatic_enable = true, } -- cgit v1.2.3-70-g09d2