Skip to content

Commit 78b9b15

Browse files
committed
feat(definition): go to imported function
1 parent 57ccc2e commit 78b9b15

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

lib/next_ls/symbol_table.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ defmodule NextLS.SymbolTable do
9797
module: _module
9898
} = reference
9999

100-
range = {{meta[:line], meta[:column]}, {meta[:line], meta[:column] + String.length(to_string(func))}}
100+
col = meta[:column] || 0
101+
range = {{meta[:line], col}, {meta[:line], col + String.length(to_string(func))}}
101102

102103
:dets.insert(state.reference_table, {
103104
{file, range},

priv/monkey/_next_ls_private_compiler.ex

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ defmodule NextLSPrivate.Tracer do
33
:ok
44
end
55

6-
def trace({:local_function, meta, func, arity}, env) do
6+
def trace({:remote_function, meta, module, func, arity}, env) do
77
parent = "NEXTLS_PARENT_PID" |> System.get_env() |> Base.decode64!() |> :erlang.binary_to_term()
88

9-
module =
10-
case Macro.Env.lookup_import(env, {func, arity}) do
11-
[{_, module}] -> module
12-
[] -> env.module
13-
end
14-
159
Process.send(
1610
parent,
1711
{{:tracer, :local_function},
@@ -28,6 +22,25 @@ defmodule NextLSPrivate.Tracer do
2822
:ok
2923
end
3024

25+
def trace({:local_function, meta, func, arity}, env) do
26+
parent = "NEXTLS_PARENT_PID" |> System.get_env() |> Base.decode64!() |> :erlang.binary_to_term()
27+
28+
Process.send(
29+
parent,
30+
{{:tracer, :local_function},
31+
%{
32+
meta: meta,
33+
func: func,
34+
arity: arity,
35+
file: env.file,
36+
module: env.module
37+
}},
38+
[]
39+
)
40+
41+
:ok
42+
end
43+
3144
def trace({:on_module, bytecode, _}, env) do
3245
parent = "NEXTLS_PARENT_PID" |> System.get_env() |> Base.decode64!() |> :erlang.binary_to_term()
3346

test/next_ls_test.exs

+60-8
Original file line numberDiff line numberDiff line change
@@ -468,26 +468,38 @@ defmodule NextLSTest do
468468

469469
describe "two" do
470470
setup %{cwd: cwd} do
471-
path = Path.join(cwd, "lib/bar.ex")
471+
imported = Path.join(cwd, "lib/imported.ex")
472472

473-
File.write!(path, """
473+
File.write!(imported, """
474+
defmodule Imported do
475+
def boom() do
476+
"💣"
477+
end
478+
end
479+
""")
480+
481+
bar = Path.join(cwd, "lib/bar.ex")
482+
483+
File.write!(bar, """
474484
defmodule Foo do
485+
import Imported
475486
def run() do
476487
process()
477488
end
478489
479490
defp process() do
491+
boom()
480492
:ok
481493
end
482494
end
483495
""")
484496

485-
[path: path]
497+
[bar: bar, imported: imported]
486498
end
487499

488500
setup :with_lsp
489501

490-
test "go to local function definition", %{client: client, path: path} do
502+
test "go to local function definition", %{client: client, bar: bar} do
491503
assert :ok ==
492504
notify(client, %{
493505
method: "initialized",
@@ -498,26 +510,66 @@ defmodule NextLSTest do
498510
assert_notification "window/logMessage", %{"message" => "[NextLS] Runtime ready..."}
499511
assert_notification "window/logMessage", %{"message" => "[NextLS] Compiled!"}
500512

501-
uri = uri(path)
513+
uri = uri(bar)
502514

503515
request(client, %{
504516
method: "textDocument/definition",
505517
id: 4,
506518
jsonrpc: "2.0",
507519
params: %{
508-
position: %{line: 2, character: 6},
520+
position: %{line: 3, character: 6},
509521
textDocument: %{uri: uri}
510522
}
511523
})
512524

513525
assert_result 4, %{
514526
"range" => %{
515527
"start" => %{
516-
"line" => 5,
528+
"line" => 6,
529+
"character" => 0
530+
},
531+
"end" => %{
532+
"line" => 6,
533+
"character" => 0
534+
}
535+
},
536+
"uri" => ^uri
537+
}
538+
end
539+
540+
test "go to imported function definition", %{client: client, bar: bar, imported: imported} do
541+
assert :ok ==
542+
notify(client, %{
543+
method: "initialized",
544+
jsonrpc: "2.0",
545+
params: %{}
546+
})
547+
548+
assert_notification "window/logMessage", %{"message" => "[NextLS] Runtime ready..."}
549+
assert_notification "window/logMessage", %{"message" => "[NextLS] Compiled!"}
550+
551+
uri = uri(bar)
552+
553+
request(client, %{
554+
method: "textDocument/definition",
555+
id: 4,
556+
jsonrpc: "2.0",
557+
params: %{
558+
position: %{line: 7, character: 5},
559+
textDocument: %{uri: uri}
560+
}
561+
})
562+
563+
uri = uri(imported)
564+
565+
assert_result 4, %{
566+
"range" => %{
567+
"start" => %{
568+
"line" => 1,
517569
"character" => 0
518570
},
519571
"end" => %{
520-
"line" => 5,
572+
"line" => 1,
521573
"character" => 0
522574
}
523575
},

0 commit comments

Comments
 (0)