aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/server.lua
blob: 847e54e1642b7cac73f14a37491bc107b094dc26 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
local fs = require("nvim-lsp-installer.fs")
local path = require("nvim-lsp-installer.path")

local M = {}

-- :'<,'>!sort
local _SERVERS = {
    "bashls",
    "clangd",
    "cssls",
    "denols",
    "dockerls",
    "eslintls",
    "gopls",
    "graphql",
    "html",
    "jsonls",
    "rust_analyzer",
    "solargraph",
    "sumneko_lua",
    "texlab",
    "tsserver",
    "vimls",
    "vuels",
    "yamlls",
}

local function get_server(server_name)
    return pcall(require, ("nvim-lsp-installer.servers.%s"):format(server_name))
end

local function get_servers(server_names)
    local result = {}
    for _, server_name in pairs(server_names) do
        local ok, server = get_server(server_name)
        if not ok then
            vim.api.nvim_err_writeln(("Unable to find LSP server %s. Error=%s"):format(server_name, server))
            goto continue
        end
        result[server_name] = server
        ::continue::
    end
    return result
end

function M.get_available_servers()
    return vim.tbl_values(get_servers(_SERVERS))
end

function M.get_installed_servers()
    return vim.tbl_filter(
        function (server)
            return server:is_installed()
        end,
        M.get_available_servers()
    )
end

function M.get_uninstalled_servers()
    return vim.tbl_filter(
        function (server)
            return not server:is_installed()
        end,
        M.get_available_servers()
    )
end

function M.install(server_name)
    local ok, server = get_server(server_name)
    if not ok then
        return vim.api.nvim_err_writeln(("Unable to find LSP server %s. Error=%s"):format(server_name, server))
    end
    local success, error = pcall(server.install, server)
    if not success then
        pcall(server.uninstall, server)
        return vim.api.nvim_err_writeln(("Failed to install %s. Error=%s"):format(server_name, vim.inspect(error)))
    end
end

function M.uninstall(server_name)
    local ok, server = get_server(server_name)
    if not ok then
        return vim.api.nvim_err_writeln(("Unable to find LSP server %s. Error=%s"):format(server_name, server))
    end
    local success, error = pcall(server.uninstall, server)
    if not success then
        vim.api.nvim_err_writeln(("Unable to uninstall %s. Error=%s"):format(server_name, vim.inspect(error)))
        return success
    end
    print(("Successfully uninstalled %s"):format(server_name))
end

function M.get_server_root_path(server)
    return path.concat { vim.fn.stdpath("data"), "lsp_servers", server }
end

M.Server = {}
M.Server.__index = M.Server

---@class Server
--@param opts table
-- @field name (string)                  The name of the LSP server. This MUST correspond with lspconfig's naming.
--
-- @field root_dir (string)              The root directory of the installation. Most servers will make use of server.get_server_root_path() to produce its root_dir path.
--
-- @field install_cmd (string)           The shell script that installs the LSP. Make sure to exit with an error code (e.g. exit 1) on failures.
--                                       The shell script is executed with "set -e" (exits the script on first non-successful command) by default.
--
-- @field default_options (table)        The default options to be passed to lspconfig's .setup() function. Each server should provide at least the `cmd` field.
--
-- @field pre_install_check (function)   An optional function to be executed before the install_cmd. This allows ensuring that any prerequisites are fulfilled.
--                                       This could for example be verifying that required build tools are installed.
function M.Server:new(opts)
    return setmetatable({
        name = opts.name,
        _install_cmd = opts.install_cmd,
        _root_dir = opts.root_dir,
        _default_options = opts.default_options,
        _pre_install_check = opts.pre_install_check,
    }, M.Server)
end

function M.Server:setup(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")[self.name].setup(
        vim.tbl_deep_extend("force", self._default_options, opts)
    )
end

function M.Server:is_installed()
    return fs.dir_exists(self._root_dir)
end

function M.Server:create_root_dir()
    fs.mkdirp(self._root_dir)
end

function M.Server:install()
    if self._pre_install_check then
        self._pre_install_check()
    end

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

    self:create_root_dir()

    self._install_cmd(self, function (_, exit_code)
        if exit_code ~= 0 then
            vim.api.nvim_err_writeln(("Server installation failed for %s. Exit code: %d"):format(self.name, exit_code))
            pcall(self.uninstall, self)
        else
            print(("Successfully installed %s"):format(self.name))
        end
    end)
end

function M.Server:uninstall()
    fs.rmrf(self._root_dir)
end

return M