-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
lspconfig_hook.lua
91 lines (82 loc) · 3.22 KB
/
lspconfig_hook.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local log = require "mason-core.log"
local _ = require "mason-core.functional"
local path = require "mason-core.path"
local platform = require "mason-core.platform"
local notify = require "mason-core.notify"
local memoized_set = _.memoize(_.set_of)
---@param server_name string
local function should_auto_install(server_name)
local settings = require "mason-lspconfig.settings"
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
---@param lspconfig_server_name string
local function resolve_server_config_factory(lspconfig_server_name)
local Optional = require "mason-core.optional"
local ok, server_config = pcall(require, ("mason-lspconfig.server_configurations.%s"):format(lspconfig_server_name))
if ok then
return Optional.of(server_config)
end
return Optional.empty()
end
local merge_in_place
---@param t1 table
---@param t2 table
merge_in_place = _.curryN(function(t1, t2)
for k, v in pairs(t2) do
if type(v) == "table" then
if type(t1[k]) == "table" and not vim.tbl_islist(t1[k]) then
merge_in_place(t1[k], v)
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end, 2)
return function()
local util = require "lspconfig.util"
local server_mapping = require "mason-lspconfig.mappings.server"
local registry = require "mason-registry"
util.on_setup = util.add_hook_before(util.on_setup, function(config, user_config)
local pkg_name = server_mapping.lspconfig_to_package[config.name]
if not pkg_name then
return
end
if registry.is_installed(pkg_name) then
resolve_server_config_factory(config.name):if_present(function(config_factory)
local mason_config = config_factory(path.package_prefix(pkg_name), config)
local merge_configs_in_place = _.compose(merge_in_place(config), merge_in_place(mason_config))
merge_configs_in_place(user_config or {})
end)
if platform.is.win and (config.cmd and config.cmd[1] ~= "cmd.exe") then
local exepath = vim.fn.exepath(config.cmd[1])
if exepath ~= "" then
config.cmd[1] = exepath
else
log.error("Failed to expand cmd path", config.name, config.cmd)
end
end
elseif should_auto_install(config.name) then
local pkg = registry.get_package(pkg_name)
notify(("[mason-lspconfig.nvim] automatically installing %s"):format(pkg.name))
pkg:install():once(
"closed",
vim.schedule_wrap(function()
if pkg:is_installed() then
notify(("[mason-lspconfig.nvim] %s was automatically installed"):format(pkg.name))
-- reload config
require("lspconfig")[config.name].setup(config)
end
end)
)
end
end)
end