-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
utils.ex
213 lines (183 loc) · 5.84 KB
/
utils.ex
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
defmodule NextLS.Support.Utils do
@moduledoc false
import ExUnit.Assertions
import ExUnit.Callbacks
import GenLSP.Test
alias GenLSP.Structures.Position
alias GenLSP.Structures.Range
alias GenLSP.Structures.TextEdit
def mix_exs do
"""
defmodule Project.MixProject do
use Mix.Project
def project do
[
app: :project,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[]
end
end
"""
end
def with_lsp(%{tmp_dir: tmp_dir} = context) do
root_paths =
for path <- context[:root_paths] || [""] do
Path.absname(Path.join(tmp_dir, path))
end
bundle_base = Path.join(tmp_dir, ".bundled")
mixhome = Path.join(tmp_dir, ".mix")
mixarchives = Path.join(mixhome, "archives")
File.mkdir_p!(bundle_base)
tvisor = start_supervised!(Supervisor.child_spec(Task.Supervisor, id: :one))
r_tvisor = start_supervised!(Supervisor.child_spec(Task.Supervisor, id: :two))
rvisor = start_supervised!({DynamicSupervisor, [strategy: :one_for_one]}, id: :three)
start_supervised!({Registry, [keys: :duplicate, name: context.module]}, id: :four)
extensions = [elixir: NextLS.ElixirExtension, credo: NextLS.CredoExtension]
cache = start_supervised!(NextLS.DiagnosticCache, id: :five)
init_options = context[:init_options] || %{}
pids = [
:one,
:two,
:three,
:four,
:five
]
server =
server(NextLS,
task_supervisor: tvisor,
runtime_task_supervisor: r_tvisor,
dynamic_supervisor: rvisor,
registry: context.module,
extensions: extensions,
cache: cache,
bundle_base: bundle_base,
mix_home: mixhome,
mix_archives: mixarchives
)
Process.link(server.lsp)
client = client(server)
assert :ok ==
request(client, %{
method: "initialize",
id: 1,
jsonrpc: "2.0",
params: %{
initializationOptions: init_options,
capabilities: %{
workspace: %{
workspaceFolders: true
},
window: %{
work_done_progress: false,
showMessage: %{}
}
},
workspaceFolders:
for(
path <- root_paths,
do: %{uri: "file://#{path}", name: "#{context.module}-#{Path.basename(path)}"}
)
}
})
[server: server, client: client, pids: pids]
end
defmacro assert_is_ready(
context,
name,
timeout \\ Application.get_env(:ex_unit, :assert_receive_timeout)
) do
quote do
message = "[Next LS] Runtime for folder #{unquote(context).module}-#{unquote(name)} is ready..."
assert_notification "window/logMessage", %{"message" => ^message}, unquote(timeout)
end
end
defmacro assert_compiled(
context,
name,
timeout \\ Application.get_env(:ex_unit, :assert_receive_timeout)
) do
quote do
message = "Compiled #{unquote(context).module}-#{unquote(name)}!"
assert_notification "$/progress",
%{
"value" => %{
"kind" => "end",
"message" => ^message
}
},
unquote(timeout)
end
end
def uri(path) when is_binary(path) do
URI.to_string(%URI{
scheme: "file",
host: "",
path: path
})
end
defmacro assert_result2(
id,
pattern,
timeout \\ Application.get_env(:ex_unit, :assert_receive_timeout)
) do
quote do
assert_receive %{
"jsonrpc" => "2.0",
"id" => unquote(id),
"result" => result
},
unquote(timeout)
assert result == unquote(pattern)
end
end
defmacro did_open(client, file_path, text) do
quote do
assert :ok ==
notify(unquote(client), %{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: %{
textDocument: %{
uri: uri(unquote(file_path)),
text: unquote(text),
languageId: "elixir",
version: 1
}
}
})
end
end
def apply_edit(code, edit) when is_binary(code), do: apply_edit(String.split(code, "\n"), edit)
def apply_edit(lines, %TextEdit{} = edit) when is_list(lines) do
text = edit.new_text
%Range{start: %Position{line: startl, character: startc}, end: %Position{line: endl, character: endc}} = edit.range
startl_text = Enum.at(lines, startl)
prefix = String.slice(startl_text, 0, startc)
endl_text = Enum.at(lines, endl)
suffix = String.slice(endl_text, endc, String.length(endl_text) - endc)
replacement = prefix <> text <> suffix
new_lines = Enum.slice(lines, 0, startl) ++ [replacement] ++ Enum.slice(lines, endl + 1, Enum.count(lines))
new_lines
|> Enum.join("\n")
|> String.trim()
end
defmacro assert_is_text_edit(code, edit, expected) do
quote do
actual = unquote(__MODULE__).apply_edit(unquote(code), unquote(edit))
assert actual == unquote(expected)
end
end
end