Skip to content

Commit

Permalink
feat: progress messages (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg authored Jun 23, 2023
1 parent 9975501 commit a68203b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
59 changes: 52 additions & 7 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ defmodule NextLS do
ServerCapabilities,
TextDocumentItem,
TextDocumentSyncOptions,
TextEdit
TextEdit,
WorkDoneProgressBegin,
WorkDoneProgressEnd
}

alias NextLS.Runtime
Expand Down Expand Up @@ -156,6 +158,10 @@ defmodule NextLS do

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

token = token()

progress_start(lsp, token, "Initializing NextLS runtime...")

{:ok, runtime} =
DynamicSupervisor.start_child(
lsp.assigns.dynamic_supervisor,
Expand All @@ -181,7 +187,11 @@ defmodule NextLS do
:ready
end)

{:noreply, assign(lsp, refresh_refs: Map.put(lsp.assigns.refresh_refs, task.ref, task.ref), runtime_task: task)}
{:noreply,
assign(lsp,
refresh_refs: Map.put(lsp.assigns.refresh_refs, task.ref, {token, "NextLS runtime has initialized!"}),
runtime_task: task
)}
end

def handle_notification(%TextDocumentDidSave{}, %{assigns: %{ready: false}} = lsp) do
Expand All @@ -197,6 +207,10 @@ defmodule NextLS do
},
%{assigns: %{ready: true}} = lsp
) do
token = token()

progress_start(lsp, token, "Compiling...")

task =
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
Runtime.compile(lsp.assigns.runtime)
Expand All @@ -205,7 +219,7 @@ defmodule NextLS do
{:noreply,
lsp
|> then(&put_in(&1.assigns.documents[uri], String.split(text, "\n")))
|> then(&put_in(&1.assigns.refresh_refs[task.ref], task.ref))}
|> then(&put_in(&1.assigns.refresh_refs[task.ref], {token, "Compiled!"}))}
end

def handle_notification(%TextDocumentDidChange{}, %{assigns: %{ready: false}} = lsp) do
Expand Down Expand Up @@ -268,20 +282,32 @@ defmodule NextLS do
{:noreply, lsp}
end

def handle_info({ref, resp}, %{assigns: %{refresh_refs: refs}} = lsp)
when is_map_key(refs, ref) do
def handle_info({ref, resp}, %{assigns: %{refresh_refs: refs}} = lsp) when is_map_key(refs, ref) do
Process.demonitor(ref, [:flush])
{_token, refs} = Map.pop(refs, ref)
{{token, msg}, refs} = Map.pop(refs, ref)

GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
params: %GenLSP.Structures.ProgressParams{
token: token,
value: %WorkDoneProgressEnd{
kind: "end",
message: msg
}
}
})

lsp =
case resp do
:ready ->
token = token()
progress_start(lsp, token, "Compiling...")

task =
Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn ->
Runtime.compile(lsp.assigns.runtime)
end)

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

_ ->
assign(lsp, refresh_refs: refs)
Expand Down Expand Up @@ -332,4 +358,23 @@ defmodule NextLS do
wait_until(n - 1, cb)
end
end

defp progress_start(lsp, token, msg) do
GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{
params: %GenLSP.Structures.ProgressParams{
token: token,
value: %WorkDoneProgressBegin{
kind: "begin",
title: msg
}
}
})
end

defp token() do
8
|> :crypto.strong_rand_bytes()
|> Base.url_encode64(padding: false)
|> binary_part(0, 8)
end
end
34 changes: 23 additions & 11 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,29 @@ defmodule NextLSTest do
}
)

# assert_notification("$/progress", %{"value" => %{"kind" => "begin"}})
assert_notification("$/progress", %{"value" => %{"kind" => "begin", "title" => "Initializing NextLS runtime..."}})

assert_notification(
"$/progress",
%{
"value" => %{
"kind" => "end",
"message" => "NextLS runtime has initialized!"
}
}
)

assert_notification("$/progress", %{"value" => %{"kind" => "begin", "title" => "Compiling..."}})

assert_notification(
"$/progress",
%{
"value" => %{
"kind" => "end",
"message" => "Compiled!"
}
}
)

for file <- ["bar.ex"] do
uri =
Expand Down Expand Up @@ -170,16 +192,6 @@ defmodule NextLSTest do
}
)
end

# assert_notification(
# "$/progress",
# %{
# "value" => %{
# "kind" => "end",
# "message" => "Found 5 issues"
# }
# }
# )
end

test "formats", %{client: client} do
Expand Down

0 comments on commit a68203b

Please sign in to comment.