Skip to content

Commit df331dc

Browse files
authoredJul 24, 2023
fix: start runtime under a supervisor (#124)
1 parent 639493c commit df331dc

File tree

3 files changed

+70
-49
lines changed

3 files changed

+70
-49
lines changed
 

‎lib/next_ls.ex

+26-49
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ defmodule NextLS do
2828
alias GenLSP.Structures.TextDocumentItem
2929
alias GenLSP.Structures.TextDocumentSyncOptions
3030
alias GenLSP.Structures.TextEdit
31-
alias GenLSP.Structures.WorkDoneProgressBegin
32-
alias GenLSP.Structures.WorkDoneProgressEnd
3331
alias NextLS.Definition
3432
alias NextLS.DiagnosticCache
33+
alias NextLS.Progress
3534
alias NextLS.Runtime
3635
alias NextLS.SymbolTable
3736

@@ -280,30 +279,32 @@ defmodule NextLS do
280279

281280
for %{uri: uri, name: name} <- lsp.assigns.workspace_folders do
282281
token = token()
283-
progress_start(lsp, token, "Initializing NextLS runtime for folder #{name}...")
282+
Progress.start(lsp, token, "Initializing NextLS runtime for folder #{name}...")
284283
parent = self()
285284

286285
{:ok, runtime} =
287286
DynamicSupervisor.start_child(
288287
lsp.assigns.dynamic_supervisor,
289-
{NextLS.Runtime,
290-
name: name,
291-
task_supervisor: lsp.assigns.runtime_task_supervisor,
292-
registry: lsp.assigns.registry,
293-
working_dir: URI.parse(uri).path,
294-
uri: uri,
295-
parent: self(),
296-
on_initialized: fn status ->
297-
if status == :ready do
298-
progress_end(lsp, token, "NextLS runtime for folder #{name} has initialized!")
299-
GenLSP.log(lsp, "[NextLS] Runtime for folder #{name} is ready...")
300-
send(parent, {:runtime_ready, name, self()})
301-
else
302-
progress_end(lsp, token)
303-
GenLSP.error(lsp, "[NextLS] Runtime for folder #{name} failed to initialize")
304-
end
305-
end,
306-
logger: lsp.assigns.logger}
288+
{NextLS.RuntimeSupervisor,
289+
runtime: [
290+
name: name,
291+
task_supervisor: lsp.assigns.runtime_task_supervisor,
292+
registry: lsp.assigns.registry,
293+
working_dir: URI.parse(uri).path,
294+
uri: uri,
295+
parent: parent,
296+
on_initialized: fn status ->
297+
if status == :ready do
298+
Progress.stop(lsp, token, "NextLS runtime for folder #{name} has initialized!")
299+
GenLSP.log(lsp, "[NextLS] Runtime for folder #{name} is ready...")
300+
send(parent, {:runtime_ready, name, self()})
301+
else
302+
Progress.stop(lsp, token)
303+
GenLSP.error(lsp, "[NextLS] Runtime for folder #{name} failed to initialize")
304+
end
305+
end,
306+
logger: lsp.assigns.logger
307+
]}
307308
)
308309

309310
ref = Process.monitor(runtime)
@@ -335,7 +336,7 @@ defmodule NextLS do
335336
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
336337
for {pid, %{name: name, uri: wuri}} <- entries, String.starts_with?(uri, wuri), into: %{} do
337338
token = token()
338-
progress_start(lsp, token, "Compiling...")
339+
Progress.start(lsp, token, "Compiling...")
339340

340341
task =
341342
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
@@ -418,7 +419,7 @@ defmodule NextLS do
418419

419420
def handle_info({:runtime_ready, name, runtime_pid}, lsp) do
420421
token = token()
421-
progress_start(lsp, token, "Compiling...")
422+
Progress.start(lsp, token, "Compiling...")
422423

423424
task =
424425
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
@@ -434,7 +435,7 @@ defmodule NextLS do
434435
Process.demonitor(ref, [:flush])
435436
{{token, msg}, refs} = Map.pop(refs, ref)
436437

437-
progress_end(lsp, token, msg)
438+
Progress.stop(lsp, token, msg)
438439

439440
{:noreply, assign(lsp, refresh_refs: refs)}
440441
end
@@ -443,7 +444,7 @@ defmodule NextLS do
443444
when is_map_key(refs, ref) do
444445
{{token, _}, refs} = Map.pop(refs, ref)
445446

446-
progress_end(lsp, token)
447+
Progress.stop(lsp, token)
447448

448449
{:noreply, assign(lsp, refresh_refs: refs)}
449450
end
@@ -462,30 +463,6 @@ defmodule NextLS do
462463
{:noreply, lsp}
463464
end
464465

465-
defp progress_start(lsp, token, msg) do
466-
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
467-
params: %GenLSP.Structures.ProgressParams{
468-
token: token,
469-
value: %WorkDoneProgressBegin{
470-
kind: "begin",
471-
title: msg
472-
}
473-
}
474-
})
475-
end
476-
477-
defp progress_end(lsp, token, msg \\ nil) do
478-
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
479-
params: %GenLSP.Structures.ProgressParams{
480-
token: token,
481-
value: %WorkDoneProgressEnd{
482-
kind: "end",
483-
message: msg
484-
}
485-
}
486-
})
487-
end
488-
489466
defp token do
490467
8
491468
|> :crypto.strong_rand_bytes()

‎lib/next_ls/progress.ex

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule NextLS.Progress do
2+
@moduledoc false
3+
def start(lsp, token, msg) do
4+
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
5+
params: %GenLSP.Structures.ProgressParams{
6+
token: token,
7+
value: %GenLSP.Structures.WorkDoneProgressBegin{
8+
kind: "begin",
9+
title: msg
10+
}
11+
}
12+
})
13+
end
14+
15+
def stop(lsp, token, msg \\ nil) do
16+
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
17+
params: %GenLSP.Structures.ProgressParams{
18+
token: token,
19+
value: %GenLSP.Structures.WorkDoneProgressEnd{
20+
kind: "end",
21+
message: msg
22+
}
23+
}
24+
})
25+
end
26+
end

‎lib/next_ls/runtime_supervisor.ex

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule NextLS.RuntimeSupervisor do
2+
@moduledoc false
3+
4+
use Supervisor
5+
6+
def start_link(init_arg) do
7+
Supervisor.start_link(__MODULE__, init_arg)
8+
end
9+
10+
@impl true
11+
def init(init_arg) do
12+
children = [
13+
{NextLS.Runtime, init_arg[:runtime]}
14+
]
15+
16+
Supervisor.init(children, strategy: :one_for_one)
17+
end
18+
end

0 commit comments

Comments
 (0)
Please sign in to comment.