Skip to content

Commit

Permalink
implement source DAP request
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Jul 14, 2024
1 parent b19b8ce commit 8fb2b11
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/debug_adapter/lib/debug_adapter/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ defmodule ElixirLS.DebugAdapter.Protocol do
end
end

defmacro source_req(seq, args) do
quote do
request(unquote(seq), "source", unquote(args))
end
end

defmacro set_breakpoints_req(seq, source, breakpoints) do
quote do
request(unquote(seq), "setBreakpoints", %{
Expand Down
18 changes: 18 additions & 0 deletions apps/debug_adapter/lib/debug_adapter/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,24 @@ defmodule ElixirLS.DebugAdapter.Server do
{%{}, %{state | config: config}}
end

defp handle_request(
source_req(_, args),
state = %__MODULE__{}
) do
path = args["source"]["path"]

content =
if path == "replinput" do
# this is a special path that VSCode uses for debugger console
# return an empty string as we do not need anything there
""
else
File.read!(path)
end

{%{"content" => content}, state}
end

defp handle_request(
set_breakpoints_req(_, %{"path" => _path}, _breakpoints),
%__MODULE__{config: %{"noDebug" => true}}
Expand Down
39 changes: 39 additions & 0 deletions apps/debug_adapter/test/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3865,4 +3865,43 @@ defmodule ElixirLS.DebugAdapter.ServerTest do
assert Process.alive?(server)
end)
end

test "source", %{server: server} do
in_fixture(__DIR__, "mix_project", fn ->
Server.receive_packet(server, initialize_req(1, %{}))
assert_receive(response(_, 1, "initialize", _))

Server.receive_packet(
server,
%{
"arguments" => %{
"sourceReference" => 0,
"source" => %{"path" => "lib/crash.ex"}
},
"command" => "source",
"seq" => 1,
"type" => "request"
}
)

assert_receive(%{"body" => %{"content" => "defmodule MixProject.Crash do" <> _}}, 10000)

Server.receive_packet(
server,
%{
"arguments" => %{
"sourceReference" => 0,
"source" => %{"path" => "replinput"}
},
"command" => "source",
"seq" => 1,
"type" => "request"
}
)

assert_receive(%{"body" => %{"content" => ""}}, 10000)

assert Process.alive?(server)
end)
end
end

0 comments on commit 8fb2b11

Please sign in to comment.