aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2025-04-26 12:30:48 +0200
committerWilliam Boman <william@redwill.se>2025-04-26 12:34:35 +0200
commit963df050ea3f65005c8ee047a4ab5d76493bf5a8 (patch)
treed42363b511a98e5121e1415136bb0a812c5eab0d /lua
parentfix(command): only accept lspconfig names and filetypes in :LspInstall (diff)
downloadmason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar.gz
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar.bz2
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar.lz
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar.xz
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.tar.zst
mason-lspconfig-963df050ea3f65005c8ee047a4ab5d76493bf5a8.zip
feat: add more capabilities to `automatic_enable`
Diffstat (limited to 'lua')
-rw-r--r--lua/mason-lspconfig/automatic_enable.lua32
-rw-r--r--lua/mason-lspconfig/features/automatic_enable.lua54
-rw-r--r--lua/mason-lspconfig/features/ensure_installed.lua (renamed from lua/mason-lspconfig/ensure_installed.lua)0
-rw-r--r--lua/mason-lspconfig/init.lua4
-rw-r--r--lua/mason-lspconfig/mappings.lua7
-rw-r--r--lua/mason-lspconfig/settings.lua16
6 files changed, 72 insertions, 41 deletions
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/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/ensure_installed.lua b/lua/mason-lspconfig/features/ensure_installed.lua
index 583feec..583feec 100644
--- a/lua/mason-lspconfig/ensure_installed.lua
+++ b/lua/mason-lspconfig/features/ensure_installed.lua
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<string, string>
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,
}