-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathinit.lua
More file actions
128 lines (116 loc) · 3.9 KB
/
init.lua
File metadata and controls
128 lines (116 loc) · 3.9 KB
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
local utils = require("elixir.utils")
local M = {}
if not vim.uv then
vim.uv = vim.loop
end
M.default_bin = vim.env.HOME .. "/.cache/elixir-tools/nextls/bin/nextls"
function M.setup(opts)
local nextls_group = vim.api.nvim_create_augroup("elixir-tools.nextls", { clear = true })
vim.api.nvim_create_autocmd("User", {
pattern = "ElixirToolsNextLSActivate",
group = nextls_group,
callback = function(event)
local cmd = event.data.cmd
local auto_update = event.data.auto_update
local options = event.data.opts
local workspace_folders = event.data.workspace_folders
vim.lsp.start({
name = "NextLS",
cmd = cmd,
cmd_env = {
NEXTLS_VERSION = options.version,
NEXTLS_AUTO_UPDATE = auto_update,
},
init_options = options.init_options or vim.empty_dict(),
settings = {},
capabilities = options.capabilities or vim.lsp.protocol.make_client_capabilities(),
workspace_folders = workspace_folders,
on_attach = options.on_attach or function() end,
}, {
bufnr = 0,
reuse_client = function(client, config)
return client.name == config.name
and vim.iter(client.workspace_folders or {}):any(function(client_wf)
return vim.iter(config.workspace_folders):any(function(new_config_wf)
return new_config_wf.name == client_wf.name and new_config_wf.uri == client_wf.uri
end)
end)
end,
})
end,
})
vim.api.nvim_create_autocmd({ "FileType" }, {
group = nextls_group,
pattern = { "elixir", "eelixir", "heex", "surface" },
callback = function()
local lock_matches
local mix_exs_matches
local workspace_folders
if vim.g.workspace then
local uri = vim.uri_from_bufnr(0)
if
vim.iter(vim.g.workspace.folders):any(function(folder)
return vim.startswith(uri, folder.uri)
end)
then
workspace_folders = vim.g.workspace.folders
end
else
lock_matches = vim.fs.find({ "mix.lock" }, {
stop = vim.uv.os_homedir(),
upward = true,
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
})
mix_exs_matches = vim.fs.find({ "mix.exs" }, {
stop = vim.uv.os_homedir(),
upward = true,
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
})
local file = lock_matches[1] or mix_exs_matches[1]
if file then
local root_dir = vim.fs.dirname(file)
assert(type(root_dir) == "string", "expected root_dir to be a string")
workspace_folders = {
{ name = vim.fs.basename(root_dir), uri = vim.uri_from_fname(root_dir) },
}
end
end
if workspace_folders then
local cmd
if type(opts.port) == "number" then
cmd = vim.lsp.rpc.connect("127.0.0.1", opts.port)
else
cmd = { opts.cmd, "--stdio" }
end
local activate = function()
vim.api.nvim_exec_autocmds("User", {
pattern = "ElixirToolsNextLSActivate",
data = {
workspace_folders = workspace_folders,
cmd = cmd,
auto_update = opts.auto_update,
opts = opts,
},
})
end
if
not vim.b.elixir_tools_prompted_nextls_install
and type(opts.port) ~= "number"
and (opts.auto_update and not vim.uv.fs_stat(opts.cmd))
then
vim.ui.select({ "Yes", "No" }, { prompt = "Install Next LS?" }, function(choice)
if choice == "Yes" then
utils.download_nextls()
activate()
else
vim.b["elixir_tools_prompted_nextls_install"] = true
end
end)
else
vim.schedule_wrap(activate)()
end
end
end,
})
end
return M