Skip to content

Commit ed67d6b

Browse files
authored
implement dbg for code blocks (#13619)
1 parent 91feadd commit ed67d6b

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

lib/elixir/lib/macro.ex

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,18 @@ defmodule Macro do
25992599
quote do
26002600
unquote(acc_var) = []
26012601
unquote(dbg_boolean_tree(ast, acc_var, result_var))
2602-
{:logic_op, Enum.reverse(unquote(acc_var))}
2602+
{:logic_op, Enum.reverse(unquote(acc_var)), unquote(result_var)}
2603+
end
2604+
end
2605+
2606+
defp dbg_ast_to_debuggable({:__block__, _meta, exprs} = ast, _env) when exprs != [] do
2607+
acc_var = unique_var(:acc, __MODULE__)
2608+
result_var = unique_var(:result, __MODULE__)
2609+
2610+
quote do
2611+
unquote(acc_var) = []
2612+
unquote(dbg_block(ast, acc_var, result_var))
2613+
{:block, Enum.reverse(unquote(acc_var)), unquote(result_var)}
26032614
end
26042615
end
26052616

@@ -2693,6 +2704,18 @@ defmodule Macro do
26932704
end
26942705
end
26952706

2707+
defp dbg_block({:__block__, meta, exprs}, acc_var, result_var) do
2708+
modified_exprs =
2709+
Enum.map(exprs, fn expr ->
2710+
quote do
2711+
unquote(result_var) = unquote(expr)
2712+
unquote(acc_var) = [{unquote(escape(expr)), unquote(result_var)} | unquote(acc_var)]
2713+
end
2714+
end)
2715+
2716+
{:__block__, meta, modified_exprs}
2717+
end
2718+
26962719
# Made public to be called from Macro.dbg/3, so that we generate as little code
26972720
# as possible and call out into a function as soon as we can.
26982721
@doc false
@@ -2730,15 +2753,27 @@ defmodule Macro do
27302753
{[first_formatted | rest_formatted], result}
27312754
end
27322755

2733-
defp dbg_format_ast_to_debug({:logic_op, components}, options) do
2734-
{_ast, final_value} = List.last(components)
2735-
2756+
defp dbg_format_ast_to_debug({:logic_op, components, value}, options) do
27362757
formatted =
27372758
Enum.map(components, fn {ast, value} ->
27382759
[dbg_format_ast(to_string_with_colors(ast, options)), " ", inspect(value, options), ?\n]
27392760
end)
27402761

2741-
{formatted, final_value}
2762+
{formatted, value}
2763+
end
2764+
2765+
defp dbg_format_ast_to_debug({:block, components, value}, options) do
2766+
formatted =
2767+
[
2768+
dbg_maybe_underline("Code block", options),
2769+
":\n(\n",
2770+
Enum.map(components, fn {ast, value} ->
2771+
[" ", dbg_format_ast_with_value(ast, value, options)]
2772+
end),
2773+
")\n"
2774+
]
2775+
2776+
{formatted, value}
27422777
end
27432778

27442779
defp dbg_format_ast_to_debug({:case, ast, expr_value, clause_index, value}, options) do

lib/elixir/test/elixir/macro_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,30 @@ defmodule MacroTest do
453453
"""
454454
end
455455

456+
test "with block of code" do
457+
{result, formatted} =
458+
dbg_format(
459+
(
460+
a = 1
461+
b = a + 2
462+
a + b
463+
)
464+
)
465+
466+
assert result == 4
467+
468+
assert formatted =~ "macro_test.exs"
469+
470+
assert formatted =~ """
471+
Code block:
472+
(
473+
a = 1 #=> 1
474+
b = a + 2 #=> 3
475+
a + b #=> 4
476+
)
477+
"""
478+
end
479+
456480
test "with case" do
457481
list = [1, 2, 3]
458482

0 commit comments

Comments
 (0)