Skip to content

Commit

Permalink
fix: clear out references when saving a file (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jul 31, 2023
1 parent 1123cf3 commit d0e0340
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 44 deletions.
11 changes: 10 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,21 @@ defmodule NextLS do

refresh_refs =
dispatch(lsp.assigns.registry, :runtimes, fn entries ->
for {pid, %{name: name, uri: wuri}} <- entries, String.starts_with?(uri, wuri), into: %{} do
for {pid, %{name: name, uri: wuri, db: db}} <- entries, String.starts_with?(uri, wuri), into: %{} do
token = token()
Progress.start(lsp, token, "Compiling...")

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
85 changes: 44 additions & 41 deletions lib/next_ls/definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,57 @@ defmodule NextLS.Definition do
alias NextLS.DB

def fetch(file, {line, col}, db) do
[[_pk, identifier, _arity, _file, type, module, _start_l, _start_c, _end_l, _end_c]] =
DB.query(
db,
with [[_pk, identifier, _arity, _file, type, module, _start_l, _start_c, _end_l, _end_c]] <-
DB.query(
db,
~Q"""
SELECT
*
FROM
'references' AS refs
WHERE
refs.file = ?
AND refs.start_line <= ?
AND ? <= refs.end_line
AND refs.start_column <= ?
AND ? <= refs.end_column
ORDER BY refs.id desc
LIMIT 1;
""",
[file, line, line, col, col]
) do
query =
~Q"""
SELECT
*
FROM
'references' AS refs
symbols
WHERE
refs.file = ?
AND refs.start_line <= ?
AND ? <= refs.end_line
AND refs.start_column <= ?
AND ? <= refs.end_column
ORDER BY refs.id desc
LIMIT 1;
""",
[file, line, line, col, col]
)

query =
~Q"""
SELECT
*
FROM
symbols
WHERE
symbols.module = ?
AND symbols.name = ?;
"""

args =
case type do
"alias" ->
[module, module]

"function" ->
[module, identifier]

_ ->
nil
symbols.module = ?
AND symbols.name = ?;
"""

args =
case type do
"alias" ->
[module, module]

"function" ->
[module, identifier]

_ ->
nil
end

if args do
DB.query(db, query, args)
else
nil
end

if args do
DB.query(db, query, args)
else
nil
_ ->
nil
end
end
end
3 changes: 2 additions & 1 deletion lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ defmodule NextLS.Runtime do
task_supervisor = Keyword.fetch!(opts, :task_supervisor)
registry = Keyword.fetch!(opts, :registry)
on_initialized = Keyword.fetch!(opts, :on_initialized)
db = Keyword.fetch!(opts, :db)

Registry.register(registry, :runtimes, %{name: name, uri: uri})
Registry.register(registry, :runtimes, %{name: name, uri: uri, db: db})

pid =
cond do
Expand Down
2 changes: 1 addition & 1 deletion lib/next_ls/runtime/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule NextLS.Runtime.Supervisor do
children = [
{NextLS.DB, logger: logger, file: ~c"#{hidden_folder}/nextls.db", registry: registry, name: db_name},
{NextLS.Runtime.Sidecar, name: sidecar_name, db: db_name, symbol_table: symbol_table_name},
{NextLS.Runtime, init_arg[:runtime] ++ [name: name, registry: registry, parent: sidecar_name]}
{NextLS.Runtime, init_arg[:runtime] ++ [name: name, registry: registry, parent: sidecar_name, db: db_name]}
]

Supervisor.init(children, strategy: :one_for_one)
Expand Down
3 changes: 3 additions & 0 deletions test/next_ls/runtime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule NextLs.RuntimeTest do
uri: "file://#{cwd}",
parent: self(),
logger: logger,
db: :some_db,
registry: RuntimeTest.Registry}
)

Expand All @@ -83,6 +84,7 @@ defmodule NextLs.RuntimeTest do
uri: "file://#{cwd}",
parent: self(),
logger: logger,
db: :some_db,
registry: RuntimeTest.Registry}
)

Expand All @@ -107,6 +109,7 @@ defmodule NextLs.RuntimeTest do
uri: "file://#{cwd}",
parent: self(),
logger: logger,
db: :some_db,
registry: RuntimeTest.Registry}
)

Expand Down

0 comments on commit d0e0340

Please sign in to comment.