Skip to content

fix: clear out references when saving a file #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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