1
1
local uv = vim .loop
2
2
3
- local lspconfig = require (" lspconfig" )
4
- local lsputil = require (" lspconfig.util" )
5
-
6
3
local Job = require (" plenary.job" )
7
4
local Path = require (" plenary.path" )
8
5
local popup = require (" plenary.popup" )
@@ -12,7 +9,6 @@ local Download = require("elixir.download")
12
9
local Compile = require (" elixir.compile" )
13
10
local Utils = require (" elixir.utils" )
14
11
15
- local default_config = require (" lspconfig.server_configurations.elixirls" ).default_config
16
12
local capabilities = vim .lsp .protocol .make_client_capabilities ()
17
13
capabilities .textDocument .completion .completionItem .snippetSupport = true
18
14
@@ -157,15 +153,14 @@ local function test(command)
157
153
end
158
154
159
155
local root_dir = function (fname )
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
- )
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 ])
165
159
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
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
169
164
end
170
165
171
166
local path = maybe_umbrella_path or child_or_root_path or vim .loop .os_homedir ()
@@ -185,13 +180,8 @@ M.settings = function(opts)
185
180
end
186
181
187
182
function M .command (params )
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
- )
183
+ local install_path =
184
+ Path :new (params .path , params .repo , Utils .safe_path (params .ref ), params .versions , " language_server.sh" )
195
185
196
186
return install_path
197
187
end
@@ -247,40 +237,55 @@ end
247
237
248
238
function M .setup (opts )
249
239
opts = opts or {}
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 () }))
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 (... )
268
276
end
269
- end )
270
277
271
- return
272
- else
273
- local updated_config = new_config
274
- updated_config .cmd = { tostring (cmd ) }
278
+ M .on_attach (... )
279
+ end ,
280
+ }, opts ))
281
+ end
282
+ end
275
283
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 ))
284
+ vim .api .nvim_create_autocmd ({ " FileType" }, {
285
+ group = elixir_group ,
286
+ pattern = { " elixir" },
287
+ callback = start_elixir_ls ,
288
+ })
284
289
end
285
290
286
291
return M
0 commit comments