-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathinit.lua
More file actions
141 lines (128 loc) · 4.4 KB
/
init.lua
File metadata and controls
141 lines (128 loc) · 4.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
local utils = require("elixir.utils")
local M = {}
if not vim.uv then
vim.uv = vim.loop
end
M.default_bin = vim.g.next_ls_default_bin or (utils.cache_dir() .. "/elixir-tools/nextls/bin/nextls")
M.default_data = vim.g.next_ls_data_dir or (utils.data_dir() .. "/elixir-tools/nextls")
local function bufname_valid(bufname)
if
bufname:match("^/")
or bufname:match("^[a-zA-Z]:")
or bufname:match("^zipfile://")
or bufname:match("^tarfile:")
then
return true
end
return false
end
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 = "Next LS",
cmd = cmd,
cmd_env = {
NEXTLS_VERSION = options.version,
NEXTLS_SPITFIRE_ENABLED = options.spitfire and 1 or 0,
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
local buf_name = vim.api.nvim_buf_get_name(0)
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
elseif bufname_valid(buf_name) then
local buf_uri = vim.uri_from_bufnr(0)
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(),
limit = 2,
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
vim.fn.mkdir(M.default_data, "p")
local force_download_file = M.default_data .. "/.next-ls-force-update-v1"
local force_download = not vim.uv.fs_stat(force_download_file)
if
(force_download and opts.auto_update)
or (type(opts.port) ~= "number" and (opts.auto_update and not vim.uv.fs_stat(opts.cmd)))
then
utils.download_nextls()
vim.fn.writefile({ "" }, force_download_file)
end
vim.schedule_wrap(activate)()
end
end,
})
return opts
end
return M