From 310d5325e094b8de43a12b654d37ba5b057eff1c Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Mon, 26 Jun 2023 23:21:57 -0400 Subject: [PATCH] fix: cancel current progress messages when changing/saving file Fixes #40 --- lib/next_ls.ex | 31 +++++++++++++++++++++---------- test/next_ls_test.exs | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index b81c5ed4..5dc9072b 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -252,6 +252,11 @@ defmodule NextLS do }, %{assigns: %{ready: true}} = lsp ) do + for task <- Task.Supervisor.children(lsp.assigns.task_supervisor), + task != lsp.assigns.runtime_task.pid do + Process.exit(task, :kill) + end + token = token() progress_start(lsp, token, "Compiling...") @@ -339,15 +344,7 @@ defmodule NextLS do Process.demonitor(ref, [:flush]) {{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 - } - } - }) + progress_end(lsp, token, msg) lsp = case resp do @@ -371,7 +368,9 @@ defmodule NextLS do def handle_info({:DOWN, ref, :process, _pid, _reason}, %{assigns: %{refresh_refs: refs}} = lsp) when is_map_key(refs, ref) do - {_token, refs} = Map.pop(refs, ref) + {{token, _}, refs} = Map.pop(refs, ref) + + progress_end(lsp, token) {:noreply, assign(lsp, refresh_refs: refs)} end @@ -425,6 +424,18 @@ defmodule NextLS do }) end + defp progress_end(lsp, token, msg \\ nil) do + GenLSP.notify(lsp, %GenLSP.Notifications.DollarProgress{ + params: %GenLSP.Structures.ProgressParams{ + token: token, + value: %WorkDoneProgressEnd{ + kind: "end", + message: msg + } + } + }) + end + defp token() do 8 |> :crypto.strong_rand_bytes() diff --git a/test/next_ls_test.exs b/test/next_ls_test.exs index b00db59a..e41c7c65 100644 --- a/test/next_ls_test.exs +++ b/test/next_ls_test.exs @@ -158,7 +158,7 @@ defmodule NextLSTest do path: Path.join([cwd, "lib", file]) }) - char = if Version.match?(System.version(), ">= 1.15.0"), do: 11, else: 0 + char = if Version.match?(System.version(), ">= 1.15.0"), do: 10, else: 0 assert_notification "textDocument/publishDiagnostics", %{ "uri" => ^uri,