1+ local utils = require (" elixir.utils" )
12local M = {}
23
34if not vim .uv then
@@ -8,9 +9,44 @@ if not vim.iter then
89 vim .iter = require (" elixir.iter" )
910end
1011
12+ M .default_bin = vim .env .HOME .. " /.cache/elixir-tools/nextls/bin/nextls"
13+
1114function M .setup (opts )
1215 local nextls_group = vim .api .nvim_create_augroup (" elixir-tools.nextls" , { clear = true })
1316
17+ vim .api .nvim_create_autocmd (" User" , {
18+ pattern = " ElixirToolsNextLSActivate" ,
19+ group = nextls_group ,
20+ callback = function (event )
21+ local cmd = event .data .cmd
22+ local options = event .data .opts
23+ local root_dir = event .data .root_dir
24+ vim .lsp .start ({
25+ name = " NextLS" ,
26+ cmd = cmd ,
27+ cmd_env = {
28+ NEXTLS_VERSION = options .version ,
29+ },
30+ settings = {},
31+ capabilities = options .capabilities or vim .lsp .protocol .make_client_capabilities (),
32+ workspace_folders = {
33+ { name = root_dir , uri = vim .uri_from_fname (root_dir ) },
34+ },
35+ on_attach = options .on_attach or function () end ,
36+ }, {
37+ bufnr = 0 ,
38+ reuse_client = function (client , config )
39+ return client .name == config .name
40+ and vim .iter (client .workspace_folders or {}):any (function (client_wf )
41+ return vim .iter (config .workspace_folders ):any (function (new_config_wf )
42+ return new_config_wf .name == client_wf .name and new_config_wf .uri == client_wf .uri
43+ end )
44+ end )
45+ end ,
46+ })
47+ end ,
48+ })
49+
1450 vim .api .nvim_create_autocmd ({ " FileType" }, {
1551 group = nextls_group ,
1652 pattern = { " elixir" , " eelixir" , " heex" , " surface" },
@@ -33,30 +69,33 @@ function M.setup(opts)
3369
3470 local root_dir = vim .fs .dirname (file )
3571 assert (type (root_dir ) == " string" , " expected root_dir to be a string" )
72+ local activate = function ()
73+ vim .api .nvim_exec_autocmds (" User" , {
74+ pattern = " ElixirToolsNextLSActivate" ,
75+ data = {
76+ root_dir = root_dir ,
77+ cmd = cmd ,
78+ opts = opts ,
79+ },
80+ })
81+ end
3682
37- vim .lsp .start ({
38- name = " NextLS" ,
39- cmd = cmd ,
40- cmd_env = {
41- NEXTLS_VERSION = opts .version ,
42- },
43- settings = {},
44- capabilities = opts .capabilities or vim .lsp .protocol .make_client_capabilities (),
45- workspace_folders = {
46- { name = root_dir , uri = vim .uri_from_fname (root_dir ) },
47- },
48- on_attach = opts .on_attach or function () end ,
49- }, {
50- bufnr = 0 ,
51- reuse_client = function (client , config )
52- return client .name == config .name
53- and vim .iter (client .workspace_folders or {}):any (function (client_wf )
54- return vim .iter (config .workspace_folders ):any (function (new_config_wf )
55- return new_config_wf .name == client_wf .name and new_config_wf .uri == client_wf .uri
56- end )
57- end )
58- end ,
59- })
83+ if
84+ not vim .b .elixir_tools_prompted_nextls_install
85+ and type (opts .port ) ~= " number"
86+ and not vim .uv .fs_stat (opts .cmd )
87+ then
88+ vim .ui .select ({ " Yes" , " No" }, { prompt = " Install Next LS?" }, function (choice )
89+ if choice == " Yes" then
90+ utils .download_nextls ()
91+ activate ()
92+ else
93+ vim .b .elixir_tools_prompted_nextls_install = true
94+ end
95+ end )
96+ else
97+ vim .schedule_wrap (activate )()
98+ end
6099 end
61100 end ,
62101 })
0 commit comments