Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions src/els_dap_general_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -454,29 +454,44 @@ handle_request(
handle_request(
{<<"evaluate">>,
#{
<<"context">> := Context,
<<"context">> := <<"watch">>,
<<"frameId">> := FrameId,
<<"expression">> := Input
} = _Params},
#{
threads := Threads,
project_node := ProjectNode
} = State
) when Context =:= <<"watch">> orelse Context =:= <<"repl">> ->
%% repl and watch can use whole expressions,
) ->
%% watch can use whole expressions,
%% but we still want structured variable scopes
case pid_by_frame_id(FrameId, maps:values(Threads)) of
undefined ->
{#{<<"result">> => <<"not available">>}, State};
Pid ->
Update =
case Context of
<<"watch">> -> no_update;
<<"repl">> -> update
end,
Return = safe_eval(ProjectNode, Pid, Input, Update),
Return = safe_eval(ProjectNode, Pid, Input, no_update),
build_evaluate_response(Return, State)
end;
handle_request(
{<<"evaluate">>,
#{
<<"context">> := <<"repl">>,
<<"expression">> := Input
} = _Params},
#{
project_node := ProjectNode
} = State
) ->
Command = els_dap_utils:to_list(Input),
Cmd =
case lists:reverse(Command) of
[$.|_] -> Command;
T -> lists:reverse([$.|T])
end,
{ok, Ts, _} = erl_scan:string(Cmd),
{ok, Expr} = erl_parse:parse_exprs(Ts),
{value, Return, _} = rpc:call(ProjectNode, erl_eval, exprs, [Expr, erl_eval:new_bindings()]),
build_evaluate_response(Return, State);
handle_request(
{<<"variables">>, #{<<"variablesReference">> := Ref} = _Params},
#{scope_bindings := AllBindings} = State
Expand Down Expand Up @@ -592,24 +607,16 @@ evaluate_hitcond(Breakpt, HitCount, Module, Line, ProjectNode, ThreadPid, Cwd) -
-spec check_stop(
els_dap_breakpoints:line_breaks(),
boolean(),
module(),
integer(),
atom(),
pid(),
binary()
pid()
) -> boolean().
check_stop(Breakpt, IsHit, Module, Line, ProjectNode, ThreadPid, Cwd) ->
check_stop(Breakpt, IsHit, ProjectNode, ThreadPid) ->
case Breakpt of
#{logexpr := LogExpr} ->
case IsHit of
true ->
Return = safe_eval(ProjectNode, ThreadPid, LogExpr, no_update),
LogMessage = unicode:characters_to_binary(
io_lib:format(
"~s:~b - ~p~n",
[source(Module, ProjectNode, Cwd), Line, Return]
)
),
Message = handle_logexpr(ProjectNode, ThreadPid, LogExpr),
LogMessage = unicode:characters_to_binary(Message),
els_dap_server:send_event(
<<"output">>,
#{
Expand All @@ -625,6 +632,27 @@ check_stop(Breakpt, IsHit, Module, Line, ProjectNode, ThreadPid, Cwd) ->
IsHit
end.

-spec handle_logexpr(atom(), pid(), binary()) -> string().
handle_logexpr(ProjectNode, ThreadPid, LogExpr) ->
case re:run(LogExpr, <<"\\{([^{}]+)\\}">>, [{capture, all_but_first, list}, global]) of
{match, Captured} ->
LogExpr2 = iolist_to_binary(["[", string:join(lists:map(fun([Cap]) ->
case lists:member($,, Cap) of
true ->
io_lib:format("{~s}", [Cap]);
false -> Cap
end
end, Captured), ","), "]"]),
Format = re:replace(LogExpr, <<"\\{+[^{}]+\\}+">>, <<"~p">>, [global, {return, list}]),
Return = safe_eval(ProjectNode, ThreadPid, LogExpr2, no_update),
case is_list(Return) of
true ->
io_lib:format(Format ++ "~n", Return);
_ -> io_lib:format("~p~n", [Return])
end;
_ -> io_lib:format("~s~n", [LogExpr])
end.

-spec debug_stop(thread_id()) -> mode().
debug_stop(ThreadId) ->
els_dap_server:send_event(
Expand Down Expand Up @@ -679,7 +707,7 @@ handle_info(
Condition andalso
evaluate_hitcond(Breakpt, HitCount, Module, Line, ProjectNode, ThreadPid, Cwd),
%% finally, either stop or log
Stop = check_stop(Breakpt, IsHit, Module, Line, ProjectNode, ThreadPid, Cwd),
Stop = check_stop(Breakpt, IsHit, ProjectNode, ThreadPid),
Mode1 =
case Stop of
true -> debug_stop(ThreadId);
Expand Down Expand Up @@ -1043,6 +1071,8 @@ safe_eval(ProjectNode, Debugged, Expression, Update) ->
case Update of
update ->
ok;
_ when Return == 'Parse error' ->
ok;
no_update ->
receive
{int_cb, Debugged} -> ok
Expand Down
9 changes: 8 additions & 1 deletion src/els_dap_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ noop_group_leader() ->
case Message of
{io_request, From, ReplyAs, getopts} ->
From ! {io_reply, ReplyAs, []};
{io_request, From, ReplyAs, _} ->
{io_request, From, ReplyAs, Converted} ->
case Converted of
{_, _, _, _, [Format, Args]} ->
Output = unicode:characters_to_binary(io_lib:format(Format, Args)),
els_dap_server:send_event(<<"output">>,
#{<<"output">> => Output, <<"category">> => <<"stdout">>});
_ -> ok
end,
From ! {io_reply, ReplyAs, ok};
_ ->
ok
Expand Down