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 15, 2023
1 parent ddd28de commit d56b392
Show file tree
Hide file tree
Showing 4 changed files with 70 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 @@ -126,7 +126,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
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, :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 @@ -78,7 +78,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 @@ -105,7 +106,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 @@ -157,6 +158,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 d56b392

Please sign in to comment.