Skip to content

Commit a68203b

Browse files
authored
feat: progress messages (#20)
1 parent 9975501 commit a68203b

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

lib/next_ls.ex

+52-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ defmodule NextLS do
3333
ServerCapabilities,
3434
TextDocumentItem,
3535
TextDocumentSyncOptions,
36-
TextEdit
36+
TextEdit,
37+
WorkDoneProgressBegin,
38+
WorkDoneProgressEnd
3739
}
3840

3941
alias NextLS.Runtime
@@ -156,6 +158,10 @@ defmodule NextLS do
156158

157159
GenLSP.log(lsp, "[NextLS] Booting runime...")
158160

161+
token = token()
162+
163+
progress_start(lsp, token, "Initializing NextLS runtime...")
164+
159165
{:ok, runtime} =
160166
DynamicSupervisor.start_child(
161167
lsp.assigns.dynamic_supervisor,
@@ -181,7 +187,11 @@ defmodule NextLS do
181187
:ready
182188
end)
183189

184-
{:noreply, assign(lsp, refresh_refs: Map.put(lsp.assigns.refresh_refs, task.ref, task.ref), runtime_task: task)}
190+
{:noreply,
191+
assign(lsp,
192+
refresh_refs: Map.put(lsp.assigns.refresh_refs, task.ref, {token, "NextLS runtime has initialized!"}),
193+
runtime_task: task
194+
)}
185195
end
186196

187197
def handle_notification(%TextDocumentDidSave{}, %{assigns: %{ready: false}} = lsp) do
@@ -197,6 +207,10 @@ defmodule NextLS do
197207
},
198208
%{assigns: %{ready: true}} = lsp
199209
) do
210+
token = token()
211+
212+
progress_start(lsp, token, "Compiling...")
213+
200214
task =
201215
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
202216
Runtime.compile(lsp.assigns.runtime)
@@ -205,7 +219,7 @@ defmodule NextLS do
205219
{:noreply,
206220
lsp
207221
|> then(&put_in(&1.assigns.documents[uri], String.split(text, "\n")))
208-
|> then(&put_in(&1.assigns.refresh_refs[task.ref], task.ref))}
222+
|> then(&put_in(&1.assigns.refresh_refs[task.ref], {token, "Compiled!"}))}
209223
end
210224

211225
def handle_notification(%TextDocumentDidChange{}, %{assigns: %{ready: false}} = lsp) do
@@ -268,20 +282,32 @@ defmodule NextLS do
268282
{:noreply, lsp}
269283
end
270284

271-
def handle_info({ref, resp}, %{assigns: %{refresh_refs: refs}} = lsp)
272-
when is_map_key(refs, ref) do
285+
def handle_info({ref, resp}, %{assigns: %{refresh_refs: refs}} = lsp) when is_map_key(refs, ref) do
273286
Process.demonitor(ref, [:flush])
274-
{_token, refs} = Map.pop(refs, ref)
287+
{{token, msg}, refs} = Map.pop(refs, ref)
288+
289+
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
290+
params: %GenLSP.Structures.ProgressParams{
291+
token: token,
292+
value: %WorkDoneProgressEnd{
293+
kind: "end",
294+
message: msg
295+
}
296+
}
297+
})
275298

276299
lsp =
277300
case resp do
278301
:ready ->
302+
token = token()
303+
progress_start(lsp, token, "Compiling...")
304+
279305
task =
280306
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
281307
Runtime.compile(lsp.assigns.runtime)
282308
end)
283309

284-
assign(lsp, ready: true, refresh_refs: Map.put(refs, task.ref, task.ref))
310+
assign(lsp, ready: true, refresh_refs: Map.put(refs, task.ref, {token, "Compiled!"}))
285311

286312
_ ->
287313
assign(lsp, refresh_refs: refs)
@@ -332,4 +358,23 @@ defmodule NextLS do
332358
wait_until(n - 1, cb)
333359
end
334360
end
361+
362+
defp progress_start(lsp, token, msg) do
363+
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
364+
params: %GenLSP.Structures.ProgressParams{
365+
token: token,
366+
value: %WorkDoneProgressBegin{
367+
kind: "begin",
368+
title: msg
369+
}
370+
}
371+
})
372+
end
373+
374+
defp token() do
375+
8
376+
|> :crypto.strong_rand_bytes()
377+
|> Base.url_encode64(padding: false)
378+
|> binary_part(0, 8)
379+
end
335380
end

test/next_ls_test.exs

+23-11
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,29 @@ defmodule NextLSTest do
141141
}
142142
)
143143

144-
# assert_notification("$/progress", %{"value" => %{"kind" => "begin"}})
144+
assert_notification("$/progress", %{"value" => %{"kind" => "begin", "title" => "Initializing NextLS runtime..."}})
145+
146+
assert_notification(
147+
"$/progress",
148+
%{
149+
"value" => %{
150+
"kind" => "end",
151+
"message" => "NextLS runtime has initialized!"
152+
}
153+
}
154+
)
155+
156+
assert_notification("$/progress", %{"value" => %{"kind" => "begin", "title" => "Compiling..."}})
157+
158+
assert_notification(
159+
"$/progress",
160+
%{
161+
"value" => %{
162+
"kind" => "end",
163+
"message" => "Compiled!"
164+
}
165+
}
166+
)
145167

146168
for file <- ["bar.ex"] do
147169
uri =
@@ -170,16 +192,6 @@ defmodule NextLSTest do
170192
}
171193
)
172194
end
173-
174-
# assert_notification(
175-
# "$/progress",
176-
# %{
177-
# "value" => %{
178-
# "kind" => "end",
179-
# "message" => "Found 5 issues"
180-
# }
181-
# }
182-
# )
183195
end
184196

185197
test "formats", %{client: client} do

0 commit comments

Comments
 (0)