From 481acc4203e47ffaa30e9b3127a82953ffe2c121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20=C3=81lvarez=20Belaustegui?= Date: Thu, 17 Aug 2023 17:11:45 +0200 Subject: [PATCH] fix: only purge references when actually recompiling (#187) Closes #154 --- lib/next_ls.ex | 11 +---------- lib/next_ls/db.ex | 19 +++++++++++++++++++ lib/next_ls/runtime/sidecar.ex | 6 ++++++ priv/monkey/_next_ls_private_compiler.ex | 8 +++++++- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 4b9f1cf0..ee7044e3 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -454,21 +454,12 @@ defmodule NextLS do refresh_refs = dispatch(lsp.assigns.registry, :runtimes, fn entries -> - for {pid, %{name: name, uri: wuri, db: db}} <- entries, String.starts_with?(uri, wuri), into: %{} do + for {pid, %{name: name, uri: wuri}} <- entries, String.starts_with?(uri, wuri), into: %{} do token = Progress.token() Progress.start(lsp, token, "Compiling #{name}...") task = Task.Supervisor.async_nolink(lsp.assigns.task_supervisor, fn -> - DB.query( - db, - ~Q""" - DELETE FROM 'references' - WHERE file = ?; - """, - [URI.parse(uri).path] - ) - {name, Runtime.compile(pid)} end) diff --git a/lib/next_ls/db.ex b/lib/next_ls/db.ex index 749069b6..293e9a00 100644 --- a/lib/next_ls/db.ex +++ b/lib/next_ls/db.ex @@ -19,6 +19,9 @@ defmodule NextLS.DB do @spec insert_reference(pid(), map()) :: :ok def insert_reference(server, payload), do: GenServer.cast(server, {:insert_reference, payload}) + @spec clean_references(pid(), String.t()) :: :ok + def clean_references(server, filename), do: GenServer.cast(server, {:clean_references, filename}) + def init(args) do file = Keyword.fetch!(args, :file) registry = Keyword.fetch!(args, :registry) @@ -137,6 +140,22 @@ defmodule NextLS.DB do {:noreply, s} end + def handle_cast({:clean_references, filename}, %{conn: conn} = s) do + {:message_queue_len, count} = Process.info(self(), :message_queue_len) + NextLS.DB.Activity.update(s.activity, count) + + __query__( + {conn, s.logger}, + ~Q""" + DELETE FROM 'references' + WHERE file = ?; + """, + [filename] + ) + + {:noreply, s} + end + def __query__({conn, logger}, query, args) do args = Enum.map(args, &cast/1) diff --git a/lib/next_ls/runtime/sidecar.ex b/lib/next_ls/runtime/sidecar.ex index c0b0d062..64ec9626 100644 --- a/lib/next_ls/runtime/sidecar.ex +++ b/lib/next_ls/runtime/sidecar.ex @@ -25,4 +25,10 @@ defmodule NextLS.Runtime.Sidecar do {:noreply, state} end + + def handle_info({{:tracer, :start}, filename}, state) do + DB.clean_references(state.db, filename) + + {:noreply, state} + end end diff --git a/priv/monkey/_next_ls_private_compiler.ex b/priv/monkey/_next_ls_private_compiler.ex index 63e59164..e382a96e 100644 --- a/priv/monkey/_next_ls_private_compiler.ex +++ b/priv/monkey/_next_ls_private_compiler.ex @@ -52,7 +52,13 @@ defmodule NextLSPrivate.Tracer do @source "user" - def trace(:start, _env) do + def trace(:start, env) do + Process.send( + parent_pid(), + {{:tracer, :start}, env.file}, + [] + ) + :ok end