1
- local lspconfig = require (" lspconfig" )
2
- local lsputil = require (" lspconfig.util" )
3
-
4
- local Job = require (" plenary.job" )
5
1
local Path = require (" plenary.path" )
6
2
local popup = require (" plenary.popup" )
7
3
@@ -10,10 +6,13 @@ local Download = require("elixir.language_server.download")
10
6
local Compile = require (" elixir.language_server.compile" )
11
7
local Utils = require (" elixir.utils" )
12
8
13
- local default_config = require (" lspconfig.server_configurations.elixirls" ).default_config
14
9
local capabilities = vim .lsp .protocol .make_client_capabilities ()
15
10
capabilities .textDocument .completion .completionItem .snippetSupport = true
16
11
12
+ local default_install_tag = " tags/v0.11.0"
13
+
14
+ local elixir_nvim_output_bufnr
15
+
17
16
local M = {}
18
17
19
18
local get_cursor_position = function ()
@@ -29,12 +28,10 @@ function M.open_floating_window(buf)
29
28
local lines = vim .o .lines
30
29
local width = math.ceil (columns * 0.8 )
31
30
local height = math.ceil (lines * 0.8 - 4 )
32
- -- local left = math.ceil((columns - width) * 0.5)
33
- -- local top = math.ceil((lines - height) * 0.5 - 1)
34
31
35
32
local bufnr = buf or vim .api .nvim_create_buf (false , true )
36
33
37
- local win_id = popup .create (bufnr , {
34
+ popup .create (bufnr , {
38
35
line = 0 ,
39
36
col = 0 ,
40
37
minwidth = width ,
@@ -118,9 +115,8 @@ local nil_buf_id = 999999
118
115
local term_buf_id = nil_buf_id
119
116
120
117
local function test (command )
121
- local row , col = get_cursor_position ()
118
+ local row , _col = get_cursor_position ()
122
119
local args = command .arguments [1 ]
123
- local current_buf_id = vim .api .nvim_get_current_buf ()
124
120
125
121
-- delete the current buffer if it's still open
126
122
if vim .api .nvim_buf_is_valid (term_buf_id ) then
@@ -219,7 +215,7 @@ local function install_elixir_ls(opts)
219
215
local source_path = Download .clone (tostring (download_dir :absolute ()), opts )
220
216
local bufnr = M .open_floating_window ()
221
217
222
- local result = Compile .compile (
218
+ Compile .compile (
223
219
download_dir :joinpath (source_path ):absolute (),
224
220
opts .install_path :absolute (),
225
221
vim .tbl_extend (" force" , opts , { bufnr = bufnr })
@@ -237,7 +233,7 @@ local function make_opts(opts)
237
233
if opts .repo then -- if we specified a repo in our conifg, then let's default to HEAD
238
234
ref = " HEAD"
239
235
else -- else, let's checkout the latest stable release
240
- ref = " tags/v0.10.0 "
236
+ ref = default_install_tag
241
237
end
242
238
end
243
239
@@ -253,52 +249,63 @@ function M.setup(opts)
253
249
vim .api .nvim_buf_set_name (elixir_nvim_output_bufnr , " ElixirLS Output Panel" )
254
250
end
255
251
256
- opts = opts or {}
257
- lspconfig .elixirls .setup (vim .tbl_extend (" keep" , {
258
- filetypes = { " elixir" , " eelixir" , " heex" , " surface" },
259
-
260
- on_init = lsputil .add_hook_after (default_config .on_init , function (client )
261
- client .commands [" elixir.lens.test.run" ] = test
262
- end ),
263
- on_new_config = function (new_config , new_root_dir )
264
- new_opts = make_opts (opts )
265
-
266
- if not opts [" cmd" ] then
267
- local cmd = M .command {
268
- path = tostring (install_dir ),
269
- repo = new_opts .repo ,
270
- ref = new_opts .ref ,
271
- versions = Version .get (),
272
- }
273
-
274
- if not cmd :exists () then
275
- vim .ui .select ({ " Yes" , " No" }, { prompt = " Install ElixirLS" }, function (choice )
276
- if choice == " Yes" then
277
- install_elixir_ls (vim .tbl_extend (" force" , new_opts , { install_path = cmd :parent () }))
278
- end
279
- end )
280
-
281
- return
282
- else
283
- local updated_config = new_config
284
- updated_config .cmd = { tostring (cmd ) }
285
-
286
- return updated_config
252
+ local elixir_group = vim .api .nvim_create_augroup (" elixirnvim" , { clear = true })
253
+
254
+ local start_elixir_ls = function (arg )
255
+ local fname = Path .new (arg .file ):absolute ()
256
+
257
+ local root_dir = opts .root_dir and opts .root_dir (fname ) or Utils .root_dir (fname )
258
+ local new_opts = make_opts (opts )
259
+
260
+ local cmd = M .command {
261
+ path = tostring (install_dir ),
262
+ repo = new_opts .repo ,
263
+ ref = new_opts .ref ,
264
+ versions = Version .get (),
265
+ }
266
+
267
+ if not cmd :exists () then
268
+ vim .ui .select ({ " Yes" , " No" }, { prompt = " Install ElixirLS" }, function (choice )
269
+ if choice == " Yes" then
270
+ install_elixir_ls (vim .tbl_extend (" force" , new_opts , { install_path = cmd :parent () }))
287
271
end
288
- end
289
- end ,
290
- handlers = {
291
- [" window/logMessage" ] = function (err , result , ...)
292
- message = vim .split (" [" .. vim .lsp .protocol .MessageType [result .type ] .. " ] " .. result .message , " \n " )
293
-
294
- vim .api .nvim_buf_set_lines (elixir_nvim_output_bufnr , - 1 , - 1 , false , message )
295
- end ,
296
- },
297
- settings = opts .settings or settings ,
298
- capabilities = opts .capabilities or capabilities ,
299
- root_dir = opts .root_dir or Utils .root_dir ,
300
- on_attach = lsputil .add_hook_before (opts .on_attach , M .on_attach ),
301
- }, opts ))
272
+ end )
273
+
274
+ return
275
+ elseif root_dir then
276
+ vim .lsp .start (vim .tbl_extend (" keep" , {
277
+ name = " ElixirLS" ,
278
+ cmd = { tostring (cmd ) },
279
+ commands = {
280
+ [" elixir.lens.test.run" ] = test ,
281
+ },
282
+ settings = opts .settings or M .settings {},
283
+ capabilities = opts .capabilities or capabilities ,
284
+ root_dir = root_dir ,
285
+ handlers = {
286
+ [" window/logMessage" ] = function (_err , result )
287
+ local message =
288
+ vim .split (" [" .. vim .lsp .protocol .MessageType [result .type ] .. " ] " .. result .message , " \n " )
289
+
290
+ vim .api .nvim_buf_set_lines (elixir_nvim_output_bufnr , - 1 , - 1 , false , message )
291
+ end ,
292
+ },
293
+ on_attach = function (...)
294
+ if opts .on_attach then
295
+ opts .on_attach (... )
296
+ end
297
+
298
+ M .on_attach (... )
299
+ end ,
300
+ }, opts ))
301
+ end
302
+ end
303
+
304
+ vim .api .nvim_create_autocmd ({ " FileType" }, {
305
+ group = elixir_group ,
306
+ pattern = { " elixir" , " eelixir" , " heex" , " surface" },
307
+ callback = start_elixir_ls ,
308
+ })
302
309
end
303
310
304
311
return M
0 commit comments