aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installer.lua
blob: bba50976aca02eb08332cd62d79313578af43a49 (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
local M = {}

local _INSTALLERS = {
    'bashls',
    'cssls',
    'dockerls',
    'eslintls',
    'graphql',
    'html',
    'jsonls',
    'solargraph',
    'sumneko_lua',
    'tsserver',
    'vimls',
    'yamlls',
}

local function escape_quotes(str)
    return string.format("%q", str)
end

local function get_server_installer(server)
    return require('nvim-lsp-installer.installers.' .. server)
end

function M.get_available_servers() return _INSTALLERS end

function M.get_installed_servers()
    local installed_servers = {}
    for _, server in ipairs(M.get_available_servers()) do
        local module = get_server_installer(server)
        if os.execute('test -d ' .. escape_quotes(module.root_dir)) == 0 then
            table.insert(installed_servers, module)
        end
    end
    return installed_servers
end

function M.get_uninstalled_servers()
    local installed_servers = M.get_installed_servers()
    return vim.tbl_filter(
        function (server)
            return not vim.tbl_contains(installed_servers, server)
        end,
        M.get_available_servers()
    )
end

local function _uninstall(server)
    local installer = get_server_installer(server)

    -- giggity
    if os.execute('rm -rf ' .. escape_quotes(installer.root_dir)) ~= 0 then
        error('Could not remove LSP server directory ' .. installer.root_dir)
    end
end

local function _install(server)
    local installer = get_server_installer(server)

    if installer.pre_install then
        installer.pre_install()
    end

    -- We run uninstall after pre_install because we don't want to
    -- unnecessarily uninstall a server should it no longer pass the
    -- pre_install check.
    _uninstall(server)

    if os.execute('mkdir -p ' .. escape_quotes(installer.root_dir)) ~= 0 then
        error('Could not create LSP server directory ' .. installer.root_dir)
    end

    local shell = vim.o.shell
    vim.o.shell = '/bin/bash'
    vim.cmd [[new]]
    vim.fn.termopen(
        'set -e;\n' .. installer.install_cmd,
        {
            cwd = installer.root_dir,
            on_exit = function (_, exit_code)
                if exit_code ~= 0 then
                    vim.api.nvim_err_writeln("Installer failed for " .. server .. ". Exit code: " .. exit_code)
                    _uninstall(server)
                else
                    print("Successfully installed " .. server)
                end

            end
        }
    )
    vim.o.shell = shell
    vim.cmd([[startinsert]]) -- so that the buffer tails the term log nicely
end

function M.install(server)
    local success, error = pcall(_install, server)
    if not success then
        pcall(_uninstall, server)
        vim.api.nvim_err_writeln("Failed to install " .. server .. ". Error=" .. vim.inspect(error))
    end
    return success
end

function M.uninstall(server)
    local success, error = pcall(_uninstall, server)
    if not success then
        vim.api.nvim_err_writeln('Unable to uninstall ' .. server .. '. Error=' .. vim.inspect(error))
        return success
    end
    print("Successfully uninstalled " .. server)
    return success
end

function M.get_server_root_path(server)
    return vim.fn.stdpath('data') .. "/lsp_servers/" .. server
end

function M.create_lsp_config_installer(module)
    return {
        name = module.name,
        install_cmd = module.install_cmd,
        root_dir = module.root_dir,
        pre_install = module.pre_install,
        setup = function(opts)
            -- We require the lspconfig server here in order to do it as late as possible.
            -- The reason for this is because once a lspconfig server has been imported, it's
            -- automatically registered with lspconfig and causes it to show up in :LspInfo and whatnot.
            require'lspconfig'[module.name].setup(
                vim.tbl_deep_extend('force', module.default_options, opts)
            )
        end,
    }
end

return M