Skip to content

Commit

Permalink
feat(definition): go to imported function
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jul 3, 2023
1 parent 57ccc2e commit 78b9b15
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/next_ls/symbol_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ defmodule NextLS.SymbolTable do
module: _module
} = reference

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

:dets.insert(state.reference_table, {
{file, range},
Expand Down
27 changes: 20 additions & 7 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ defmodule NextLSPrivate.Tracer do
:ok
end

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

module =
case Macro.Env.lookup_import(env, {func, arity}) do
[{_, module}] -> module
[] -> env.module
end

Process.send(
parent,
{{:tracer, :local_function},
Expand All @@ -28,6 +22,25 @@ defmodule NextLSPrivate.Tracer do
:ok
end

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

Process.send(
parent,
{{:tracer, :local_function},
%{
meta: meta,
func: func,
arity: arity,
file: env.file,
module: env.module
}},
[]
)

:ok
end

def trace({:on_module, bytecode, _}, env) do
parent = "NEXTLS_PARENT_PID" |> System.get_env() |> Base.decode64!() |> :erlang.binary_to_term()

Expand Down
68 changes: 60 additions & 8 deletions test/next_ls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -468,26 +468,38 @@ defmodule NextLSTest do

describe "two" do
setup %{cwd: cwd} do
path = Path.join(cwd, "lib/bar.ex")
imported = Path.join(cwd, "lib/imported.ex")

File.write!(path, """
File.write!(imported, """
defmodule Imported do
def boom() do
"💣"
end
end
""")

bar = Path.join(cwd, "lib/bar.ex")

File.write!(bar, """
defmodule Foo do
import Imported
def run() do
process()
end
defp process() do
boom()
:ok
end
end
""")

[path: path]
[bar: bar, imported: imported]
end

setup :with_lsp

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

uri = uri(path)
uri = uri(bar)

request(client, %{
method: "textDocument/definition",
id: 4,
jsonrpc: "2.0",
params: %{
position: %{line: 2, character: 6},
position: %{line: 3, character: 6},
textDocument: %{uri: uri}
}
})

assert_result 4, %{
"range" => %{
"start" => %{
"line" => 5,
"line" => 6,
"character" => 0
},
"end" => %{
"line" => 6,
"character" => 0
}
},
"uri" => ^uri
}
end

test "go to imported function definition", %{client: client, bar: bar, imported: imported} do
assert :ok ==
notify(client, %{
method: "initialized",
jsonrpc: "2.0",
params: %{}
})

assert_notification "window/logMessage", %{"message" => "[NextLS] Runtime ready..."}
assert_notification "window/logMessage", %{"message" => "[NextLS] Compiled!"}

uri = uri(bar)

request(client, %{
method: "textDocument/definition",
id: 4,
jsonrpc: "2.0",
params: %{
position: %{line: 7, character: 5},
textDocument: %{uri: uri}
}
})

uri = uri(imported)

assert_result 4, %{
"range" => %{
"start" => %{
"line" => 1,
"character" => 0
},
"end" => %{
"line" => 5,
"line" => 1,
"character" => 0
}
},
Expand Down

0 comments on commit 78b9b15

Please sign in to comment.