1
1
local uv = vim .loop
2
2
3
+ local lspconfig = require (" lspconfig" )
4
+ local lsputil = require (" lspconfig.util" )
5
+
3
6
local Job = require (" plenary.job" )
4
7
local Path = require (" plenary.path" )
5
8
local popup = require (" plenary.popup" )
@@ -9,6 +12,7 @@ local Download = require("elixir.download")
9
12
local Compile = require (" elixir.compile" )
10
13
local Utils = require (" elixir.utils" )
11
14
15
+ local default_config = require (" lspconfig.server_configurations.elixirls" ).default_config
12
16
local capabilities = vim .lsp .protocol .make_client_capabilities ()
13
17
capabilities .textDocument .completion .completionItem .snippetSupport = true
14
18
@@ -153,14 +157,15 @@ local function test(command)
153
157
end
154
158
155
159
local root_dir = function (fname )
156
- local child_or_root_path = vim .fs .dirname (vim .fs .find ({ " mix.exs" , " .git" }, { upward = true , path = fname })[1 ])
157
- local maybe_umbrella_path =
158
- vim .fs .dirname (vim .fs .find ({ " mix.exs" }, { upward = true , path = child_or_root_path })[1 ])
160
+ local path = lsputil .path
161
+ local child_or_root_path = lsputil .root_pattern ({ " mix.exs" , " .git" })(fname )
162
+ local maybe_umbrella_path = lsputil .root_pattern ({ " mix.exs" })(
163
+ uv .fs_realpath (path .join ({ child_or_root_path , " .." }))
164
+ )
159
165
160
- if maybe_umbrella_path then
161
- if not vim .startswith (child_or_root_path , Path :joinpath (maybe_umbrella_path , " apps" ):absolute ()) then
162
- maybe_umbrella_path = nil
163
- end
166
+ local has_ancestral_mix_exs_path = vim .startswith (child_or_root_path , path .join ({ maybe_umbrella_path , " apps" }))
167
+ if maybe_umbrella_path and not has_ancestral_mix_exs_path then
168
+ maybe_umbrella_path = nil
164
169
end
165
170
166
171
local path = maybe_umbrella_path or child_or_root_path or vim .loop .os_homedir ()
@@ -180,8 +185,13 @@ M.settings = function(opts)
180
185
end
181
186
182
187
function M .command (params )
183
- local install_path =
184
- Path :new (params .path , params .repo , Utils .safe_path (params .ref ), params .versions , " language_server.sh" )
188
+ local install_path = Path :new (
189
+ params .path ,
190
+ params .repo ,
191
+ Utils .safe_path (params .ref ),
192
+ params .versions ,
193
+ " language_server.sh"
194
+ )
185
195
186
196
return install_path
187
197
end
@@ -237,55 +247,40 @@ end
237
247
238
248
function M .setup (opts )
239
249
opts = opts or {}
240
- local elixir_group = vim .api .nvim_create_augroup (" elixirnvim" , { clear = true })
241
-
242
- local start_elixir_ls = function (arg )
243
- fname = Path .new (arg .file ):absolute ()
244
-
245
- local root_dir = opts .root_dir and opts .root_dir (fname ) or root_dir (fname )
246
- local new_opts = make_opts (opts )
247
-
248
- local cmd = M .command ({
249
- path = tostring (install_dir ),
250
- repo = new_opts .repo ,
251
- ref = new_opts .ref ,
252
- versions = Version .get (),
253
- })
254
-
255
- if not cmd :exists () then
256
- vim .ui .select ({ " Yes" , " No" }, { prompt = " Install ElixirLS" }, function (choice )
257
- if choice == " Yes" then
258
- install_elixir_ls (vim .tbl_extend (" force" , new_opts , { install_path = cmd :parent () }))
259
- end
260
- end )
261
-
262
- return
263
- elseif root_dir then
264
- vim .lsp .start (vim .tbl_extend (" keep" , {
265
- name = " ElixirLS" ,
266
- cmd = { tostring (cmd ) },
267
- commands = {
268
- [" elixir.lens.test.run" ] = test ,
269
- },
270
- settings = opts .settings or settings ,
271
- capabilities = opts .capabilities or capabilities ,
272
- root_dir = root_dir ,
273
- on_attach = function (...)
274
- if opts .on_attach then
275
- opts .on_attach (... )
250
+ lspconfig .elixirls .setup (vim .tbl_extend (" keep" , {
251
+ on_init = lsputil .add_hook_after (default_config .on_init , function (client )
252
+ client .commands [" elixir.lens.test.run" ] = test
253
+ end ),
254
+ on_new_config = function (new_config , new_root_dir )
255
+ new_opts = make_opts (opts )
256
+
257
+ local cmd = M .command ({
258
+ path = tostring (install_dir ),
259
+ repo = new_opts .repo ,
260
+ ref = new_opts .ref ,
261
+ versions = Version .get (),
262
+ })
263
+
264
+ if not cmd :exists () then
265
+ vim .ui .select ({ " Yes" , " No" }, { prompt = " Install ElixirLS" }, function (choice )
266
+ if choice == " Yes" then
267
+ install_elixir_ls (vim .tbl_extend (" force" , new_opts , { install_path = cmd :parent () }))
276
268
end
269
+ end )
277
270
278
- M .on_attach (... )
279
- end ,
280
- }, opts ))
281
- end
282
- end
271
+ return
272
+ else
273
+ local updated_config = new_config
274
+ updated_config .cmd = { tostring (cmd ) }
283
275
284
- vim .api .nvim_create_autocmd ({ " FileType" }, {
285
- group = elixir_group ,
286
- pattern = { " elixir" },
287
- callback = start_elixir_ls ,
288
- })
276
+ return updated_config
277
+ end
278
+ end ,
279
+ settings = opts .settings or settings ,
280
+ capabilities = opts .capabilities or capabilities ,
281
+ root_dir = opts .root_dir or root_dir ,
282
+ on_attach = lsputil .add_hook_before (opts .on_attach , M .on_attach ),
283
+ }, opts ))
289
284
end
290
285
291
286
return M
0 commit comments