Skip to content

Commit

Permalink
fix: handle change and save notifcations before runtime is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed May 22, 2024
1 parent 01e8139 commit 52772bd
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -970,35 +970,34 @@ defmodule NextLS do
{:noreply, assign(lsp, elixir_bin_path: elixir_bin_path)}
end

def handle_notification(%TextDocumentDidSave{}, %{assigns: %{ready: false}} = lsp) do
{:noreply, lsp}
end

# TODO: add some test cases for saving files in multiple workspaces
def handle_notification(
%TextDocumentDidSave{
params: %GenLSP.Structures.DidSaveTextDocumentParams{text: text, text_document: %{uri: uri}}
},
%{assigns: %{ready: true}} = lsp
) do
for task <- Task.Supervisor.children(lsp.assigns.task_supervisor) do
Process.exit(task, :kill)
end

refresh_refs =
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
for {pid, %{name: name, uri: wuri}} <- entries,
String.starts_with?(uri, wuri),
into: %{} do
token = Progress.token()
Progress.start(lsp, token, "Compiling #{name}...")
if lsp.assigns.ready do
for task <- Task.Supervisor.children(lsp.assigns.task_supervisor) do
Process.exit(task, :kill)
end

ref = make_ref()
Runtime.compile(pid, caller_ref: ref)
# dispatching to all workspaces
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
for {pid, %{name: name, uri: wuri}} <- entries,
String.starts_with?(uri, wuri),
into: %{} do
token = Progress.token()
Progress.start(lsp, token, "Compiling #{name}...")

{ref, {token, "Compiled #{name}!"}}
end
end)
ref = make_ref()
Runtime.compile(pid, caller_ref: ref)

{ref, {token, "Compiled #{name}!"}}
end
end)
end

lsp =
lsp
Expand All @@ -1008,16 +1007,14 @@ defmodule NextLS do
{:noreply, lsp}
end

def handle_notification(%TextDocumentDidChange{}, %{assigns: %{ready: false}} = lsp) do
{:noreply, lsp}
end

def handle_notification(
%TextDocumentDidChange{params: %{text_document: %{uri: uri}, content_changes: [%{text: text}]}},
lsp
) do
for task <- Task.Supervisor.children(lsp.assigns.task_supervisor) do
Process.exit(task, :kill)
if lsp.assigns.ready do
for task <- Task.Supervisor.children(lsp.assigns.task_supervisor) do
Process.exit(task, :kill)
end
end

lsp = put_in(lsp.assigns.documents[uri], String.split(text, "\n"))
Expand Down

0 comments on commit 52772bd

Please sign in to comment.