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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
local Data = require "nvim-lsp-installer.data"
local path = require "nvim-lsp-installer.path"
local fs = require "nvim-lsp-installer.fs"
local settings = require "nvim-lsp-installer.settings"
local M = {}
-- By default the install dir will be the same as the server's name.
-- There are two cases when servers should install to a different location:
-- 1. Legacy reasons, where some servers were previously installed to a location different than their name
-- 2. There is a breaking change to a server that motivates changing its install dir (e.g. to "bust" existing installations).
local INSTALL_DIRS = {
["bashls"] = "bash",
["dockerls"] = "dockerfile",
["elixirls"] = "elixir",
["elmls"] = "elm",
["eslint"] = "vscode-eslint",
["gopls"] = "go",
["hls"] = "haskell",
["intelephense"] = "php",
["kotlin_language_server"] = "kotlin",
["phpactor"] = "phpactor-source",
["purescriptls"] = "purescript",
["pyright"] = "python",
["rust_analyzer"] = "rust",
["tailwindcss"] = "tailwindcss_npm",
["terraformls"] = "terraform",
["texlab"] = "latex",
["vimls"] = "vim",
["yamlls"] = "yaml",
}
local CORE_SERVERS = Data.set_of {
"angularls",
"ansiblels",
"arduino_language_server",
"bashls",
"bicep",
"ccls",
"clangd",
"clojure_lsp",
"cmake",
"codeqlls",
"csharp_ls",
"cssls",
"cssmodules_ls",
"dartls",
"denols",
"diagnosticls",
"dockerls",
"dotls",
"efm",
"elixirls",
"elmls",
"ember",
"emmet_ls",
"erlangls",
"esbonio",
"eslint",
"fortls",
"fsautocomplete",
"gopls",
"grammarly",
"graphql",
"groovyls",
"hls",
"html",
"intelephense",
"jdtls",
"jedi_language_server",
"jsonls",
"jsonnet_ls",
"kotlin_language_server",
"lemminx",
"ltex",
"ocamlls",
"omnisharp",
"phpactor",
"powershell_es",
"prismals",
"puppet",
"purescriptls",
"pylsp",
"pyright",
"quick_lint_js",
"rescriptls",
"rome",
"rust_analyzer",
"serve_d",
"solang",
"solargraph",
"solc",
"solidity_ls",
"sorbet",
"sourcekit",
"spectral",
"sqlls",
"sqls",
"stylelint_lsp",
"sumneko_lua",
"svelte",
"tailwindcss",
"taplo",
"terraformls",
"texlab",
"tflint",
"tsserver",
"vala_ls",
"vimls",
"volar",
"vuels",
"yamlls",
"zk",
"zls",
}
---@type table<string, Server>
local INITIALIZED_SERVERS = {}
local cached_server_roots
local function scan_server_roots()
if cached_server_roots then
return cached_server_roots
end
---@type string[]
local result = {}
local ok, entries = pcall(fs.readdir, settings.current.install_root_dir)
if not ok then
-- presume servers root dir has not been created yet (i.e., no servers installed)
return {}
end
for i = 1, #entries do
local entry = entries[i]
if entry.type == "directory" then
result[#result + 1] = entry.name
end
end
cached_server_roots = Data.set_of(result)
vim.schedule(function()
cached_server_roots = nil
end)
return cached_server_roots
end
---@param server_name string
---@return string
local function get_server_install_dir(server_name)
return INSTALL_DIRS[server_name] or server_name
end
function M.get_server_install_path(dirname)
return path.concat { settings.current.install_root_dir, dirname }
end
---@param server_name string
function M.is_server_installed(server_name)
local scanned_server_dirs = scan_server_roots()
local dirname = get_server_install_dir(server_name)
return scanned_server_dirs[dirname] or false
end
---@param server_identifier string @The server identifier to parse.
---@return string, string|nil @Returns a (server_name, requested_version) tuple, where requested_version may be nil.
function M.parse_server_identifier(server_identifier)
return unpack(vim.split(server_identifier, "@"))
end
---@param server_name string
---@return boolean, Server
function M.get_server(server_name)
if INITIALIZED_SERVERS[server_name] then
return true, INITIALIZED_SERVERS[server_name]
end
if not CORE_SERVERS[server_name] then
return false, ("Server %s does not exist."):format(server_name)
end
local ok, server_factory = pcall(require, ("nvim-lsp-installer.servers.%s"):format(server_name))
if ok then
INITIALIZED_SERVERS[server_name] = server_factory(
server_name,
M.get_server_install_path(get_server_install_dir(server_name))
)
return true, INITIALIZED_SERVERS[server_name]
end
return false,
(
"Unable to import server %s.\n\nThis is an unexpected error, please file an issue at %s with the following information:\n%s"
):format(server_name, "https://github.com/williamboman/nvim-lsp-installer", server_factory)
end
---@param server_names string[]
---@return Server[]
local function resolve_servers(server_names)
return Data.list_map(function(server_name)
local ok, server = M.get_server(server_name)
if not ok then
error(server)
end
return server
end, server_names)
end
---@return string[]
function M.get_available_server_names()
return vim.tbl_keys(vim.tbl_extend("force", CORE_SERVERS, INITIALIZED_SERVERS))
end
---@return string[]
function M.get_installed_server_names()
return vim.tbl_filter(function(server_name)
return M.is_server_installed(server_name)
end, M.get_available_server_names())
end
---@return string[]
function M.get_uninstalled_server_names()
return vim.tbl_filter(function(server_name)
return not M.is_server_installed(server_name)
end, M.get_available_server_names())
end
-- Expensive to call the first time - loads all server modules.
function M.get_available_servers()
return resolve_servers(M.get_available_server_names())
end
-- Somewhat expensive to call the first time (depends on how many servers are currently installed).
function M.get_installed_servers()
return resolve_servers(M.get_installed_server_names())
end
-- Expensive to call the first time (depends on how many servers are currently not installed).
function M.get_uninstalled_servers()
return resolve_servers(M.get_uninstalled_server_names())
end
---@param server Server @The server to register.
function M.register(server)
INSTALL_DIRS[server.name] = vim.fn.fnamemodify(server.root_dir, ":t")
INITIALIZED_SERVERS[server.name] = server
end
return M
|