aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/middleware.lua
blob: 44af9f0f7e474b648057f5e67962f796792a0816 (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
local util = require "lspconfig.util"
local notify = require "nvim-lsp-installer.notify"
local servers = require "nvim-lsp-installer.servers"
local settings = require "nvim-lsp-installer.settings"
local functional = require "nvim-lsp-installer.core.functional"

local memoize, set_of = functional.memoize, functional.set_of

local M = {}

---@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

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)
            elseif should_auto_install(server.name) then
                notify("(automatic installation) Installing LSP server: " .. server.name)
                server:install()
            end
        end
    end)
end

return M