aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-lspconfig/init.lua
blob: ad177be66837bcc8671dfb13d999a014d53510b3 (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
local log = require "mason.log"
local Package = require "mason.core.package"
local Optional = require "mason.core.optional"
local _ = require "mason.core.functional"
local settings = require "mason-lspconfig.settings"
local server_mapping = require "mason-lspconfig.server-mapping"

local M = {}

---@param lspconfig_server_name string
function M.resolve_package(lspconfig_server_name)
    return Optional.of_nilable(server_mapping.lspconfig_to_package[lspconfig_server_name]):map(function(package_name)
        local ok, pkg = pcall(require, ("mason.packages.%s"):format(package_name))
        if ok then
            return pkg
        end
    end)
end

---@param lspconfig_server_name string
function M.resolve_server_config_factory(lspconfig_server_name)
    local ok, server_config =
        pcall(require, ("mason.adapters.lspconfig.server_configurations.%s"):format(lspconfig_server_name))
    if ok then
        return Optional.of(server_config)
    end
    return Optional.empty()
end

---@param t1 table
---@param t2 table
local function merge_in_place(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

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

local function setup_lspconfig_hook()
    local util = require "lspconfig.util"
    util.on_setup = util.add_hook_before(util.on_setup, function(config)
        M.resolve_package(config.name):if_present(
            ---@param pkg Package
            function(pkg)
                if pkg:is_installed() then
                    M.resolve_server_config_factory(config.name):if_present(function(config_factory)
                        merge_in_place(config, config_factory(pkg:get_install_path()))
                    end)
                else
                    if should_auto_install(config.name) then
                        pkg:install()
                    end
                end
            end
        )
    end)
end

local function ensure_installed()
    for _, server_identifier in ipairs(settings.current.ensure_installed) do
        local server_name, version = Package.Parse(server_identifier)
        M.resolve_package(server_name):if_present(
            ---@param pkg Package
            function(pkg)
                if not pkg:is_installed() then
                    pkg:install {
                        version = version,
                    }
                end
            end
        )
    end
end

---@param config MasonLspconfigSettings | nil
function M.setup(config)
    if config then
        settings.set(config)
    end

    setup_lspconfig_hook()
    ensure_installed()
end

---@param handlers table<string, fun(server_name: string)>
function M.setup_handlers(handlers)
    local default_handler = Optional.of_nilable(handlers[1])
    local indexer = require "mason.core.package.indexer"

    ---@param pkg_name string
    local function get_server_name(pkg_name)
        return Optional.of_nilable(server_mapping.package_to_lspconfig[pkg_name])
    end

    local function call_handler(server_name)
        log.fmt_trace("Checking handler for %s", server_name)
        Optional.of_nilable(handlers[server_name]):or_(_.always(default_handler)):if_present(function(handler)
            log.fmt_trace("Calling handler for %s", server_name)
            local ok, err = pcall(handler, server_name)
            if not ok then
                vim.notify(err, vim.log.levels.ERROR)
            end
        end)
    end

    local installed_servers = _.filter_map(get_server_name, indexer.get_installed_package_names())
    _.each(call_handler, installed_servers)
    indexer:on(
        "package:install:success",
        vim.schedule_wrap(function(pkg)
            get_server_name(pkg.name):if_present(call_handler)
        end)
    )
end

return M