Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 9902378

Browse files
committed
feat: go to definition when aliases modules
1 parent 70d52dc commit 9902378

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

lib/next_ls.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ defmodule NextLS do
127127
result =
128128
dispatch(lsp.assigns.registry, :databases, fn entries ->
129129
for {pid, _} <- entries do
130-
case Definition.fetch(URI.parse(uri).path, {position.line + 1, position.character + 1}, pid) do
130+
case Definition.fetch(
131+
URI.parse(uri).path,
132+
{position.line + 1, position.character + 1},
133+
pid,
134+
Enum.join(lsp.assigns.documents[uri], "\n")
135+
) do
131136
nil ->
132137
nil
133138

lib/next_ls/definition.ex

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@ defmodule NextLS.Definition do
44

55
alias NextLS.DB
66

7-
def fetch(file, {line, col}, db) do
8-
with [[_pk, identifier, _arity, _file, type, module, _start_l, _start_c, _end_l, _end_c | _]] <-
9-
DB.query(
10-
db,
11-
~Q"""
12-
SELECT
13-
*
14-
FROM
15-
'references' AS refs
16-
WHERE
17-
refs.file = ?
18-
AND ? BETWEEN refs.start_line AND refs.end_line
19-
AND ? BETWEEN refs.start_column AND refs.end_column
20-
ORDER BY refs.id asc
21-
LIMIT 1;
22-
""",
23-
[file, line, col]
24-
) do
7+
def fetch(file, {line, col}, db, text) do
8+
reference =
9+
with nil <- fetch_from_db(db, {file, line, col}),
10+
nil <- fetch_from_context(text, {line, col}) do
11+
nil
12+
end
13+
14+
dbg(reference)
15+
16+
with %{identifier: identifier, type: type, module: module} <- reference do
2517
query =
2618
~Q"""
2719
SELECT
@@ -50,7 +42,45 @@ defmodule NextLS.Definition do
5042
else
5143
nil
5244
end
53-
else
45+
end
46+
end
47+
48+
defp fetch_from_db(db, {file, line, col}) do
49+
rows =
50+
DB.query(
51+
db,
52+
~Q"""
53+
SELECT
54+
*
55+
FROM
56+
'references' AS refs
57+
WHERE
58+
refs.file = ?
59+
AND refs.start_line <= ?
60+
AND ? <= refs.end_line
61+
AND refs.start_column <= ?
62+
AND ? <= refs.end_column
63+
ORDER BY refs.id asc
64+
LIMIT 1;
65+
""",
66+
[file, line, line, col, col]
67+
)
68+
69+
case rows do
70+
[[_pk, identifier, _arity, _file, type, module, _start_l, _start_c, _end_l, _end_c | _]] ->
71+
%{identifier: identifier, type: type, module: module}
72+
73+
[] ->
74+
nil
75+
end
76+
end
77+
78+
defp fetch_from_context(text, {line, col}) do
79+
case Code.Fragment.surround_context(text, {line, col}) do
80+
%{context: {:alias, alias}} ->
81+
module = to_string(alias)
82+
%{identifier: module, type: "alias", module: module}
83+
5484
_ ->
5585
nil
5686
end

lib/next_ls/runtime/sidecar.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ defmodule NextLS.Runtime.Sidecar do
2525

2626
{:noreply, state}
2727
end
28+
29+
def handle_info({{:tracer, :dbg}, payload}, state) do
30+
dbg(payload)
31+
32+
{:noreply, state}
33+
end
2834
end

priv/monkey/_next_ls_private_compiler.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ defmodule NextLSPrivate.Tracer do
7878
:ok
7979
end
8080

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

8485
if type == :remote_macro && meta[:closing][:line] != meta[:line] do
@@ -105,7 +106,7 @@ defmodule NextLSPrivate.Tracer do
105106
:ok
106107
end
107108

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

111112
Process.send(
@@ -157,6 +158,12 @@ defmodule NextLSPrivate.Tracer do
157158
:ok
158159
end
159160

161+
def trace(it, env) do
162+
parent = parent_pid()
163+
Process.send(parent, {{:tracer, :dbg}, {it, env.aliases}}, [])
164+
:ok
165+
end
166+
160167
def trace(_event, _env) do
161168
:ok
162169
end

0 commit comments

Comments
 (0)