Skip to content

Commit

Permalink
fix: only purge references when actually recompiling (#187)
Browse files Browse the repository at this point in the history
Closes #154
  • Loading branch information
crbelaus committed Aug 17, 2023
1 parent 08313a2 commit 481acc4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
11 changes: 1 addition & 10 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions lib/next_ls/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions lib/next_ls/runtime/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 481acc4

Please sign in to comment.