Skip to content

Commit

Permalink
feat: go to definition when aliases modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Aug 24, 2023
1 parent 8d0a3ef commit 8c4423f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
7 changes: 6 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ defmodule NextLS do
result =
dispatch(lsp.assigns.registry, :databases, fn entries ->
for {pid, _} <- entries do
case Definition.fetch(URI.parse(uri).path, {position.line + 1, position.character + 1}, pid) do
case Definition.fetch(
URI.parse(uri).path,
{position.line + 1, position.character + 1},
pid,
Enum.join(lsp.assigns.documents[uri], "\n")
) do
nil ->
nil

Expand Down
68 changes: 49 additions & 19 deletions lib/next_ls/definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ defmodule NextLS.Definition do

alias NextLS.DB

def fetch(file, {line, col}, db) do
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 ? BETWEEN refs.start_line AND refs.end_line
AND ? BETWEEN refs.start_column AND refs.end_column
ORDER BY refs.id asc
LIMIT 1;
""",
[file, line, col]
) do
def fetch(file, {line, col}, db, text) do
reference =
with nil <- fetch_from_db(db, {file, line, col}),
nil <- fetch_from_context(text, {line, col}) do
nil
end

dbg(reference)

with %{identifier: identifier, type: type, module: module} <- reference do
query =
~Q"""
SELECT
Expand Down Expand Up @@ -50,7 +42,45 @@ defmodule NextLS.Definition do
else
nil
end
else
end
end

defp fetch_from_db(db, {file, line, col}) do
rows =
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 asc
LIMIT 1;
""",
[file, line, line, col, col]
)

case rows do
[[_pk, identifier, _arity, _file, type, module, _start_l, _start_c, _end_l, _end_c | _]] ->
%{identifier: identifier, type: type, module: module}

[] ->
nil
end
end

defp fetch_from_context(text, {line, col}) do
case Code.Fragment.surround_context(text, {line, col}) do
%{context: {:alias, alias}} ->
module = to_string(alias)
%{identifier: module, type: "alias", module: module}

_ ->
nil
end
Expand Down
4 changes: 4 additions & 0 deletions lib/next_ls/runtime/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ defmodule NextLS.Runtime.Sidecar do

def handle_info({{:tracer, :start}, filename}, state) do
DB.clean_references(state.db, filename)
{:noreply, state}
end

def handle_info({{:tracer, :dbg}, payload}, state) do
dbg(payload)
{:noreply, state}
end
end
11 changes: 9 additions & 2 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ defmodule NextLSPrivate.Tracer do
:ok
end

def trace({type, meta, module, func, arity}, env) when type in [:remote_function, :remote_macro, :imported_macro] do
def trace({type, meta, module, func, arity} = it, env)
when type in [:remote_function, :remote_macro, :imported_macro] do
parent = parent_pid()

if type == :remote_macro && meta[:closing][:line] != meta[:line] do
Expand All @@ -111,7 +112,7 @@ defmodule NextLSPrivate.Tracer do
:ok
end

def trace({type, meta, func, arity}, env) when type in [:local_function, :local_macro] do
def trace({type, meta, func, arity} = it, env) when type in [:local_function, :local_macro] do
parent = parent_pid()

Process.send(
Expand Down Expand Up @@ -163,6 +164,12 @@ defmodule NextLSPrivate.Tracer do
:ok
end

def trace(it, env) do
parent = parent_pid()
Process.send(parent, {{:tracer, :dbg}, {it, env.aliases}}, [])
:ok
end

def trace(_event, _env) do
:ok
end
Expand Down

0 comments on commit 8c4423f

Please sign in to comment.