Skip to content

Commit ff3674d

Browse files
committed
Format multiline inside interpolation on first run, closes #10996
1 parent 0460017 commit ff3674d

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ defmodule Code.Formatter do
257257
force_do_end_blocks: force_do_end_blocks,
258258
locals_without_parens: locals_without_parens,
259259
operand_nesting: 2,
260+
skip_eol: false,
260261
comments: comments
261262
}
262263
end
@@ -528,7 +529,7 @@ defmodule Code.Formatter do
528529
end
529530

530531
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
531-
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta))
532+
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta, state))
532533
end
533534

534535
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
@@ -777,7 +778,7 @@ defmodule Code.Formatter do
777778
concat(concat(group(left), op_string), group(right))
778779

779780
true ->
780-
eol? = eol?(meta)
781+
eol? = eol?(meta, state)
781782

782783
next_break_fits? =
783784
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and not eol?
@@ -919,7 +920,7 @@ defmodule Code.Formatter do
919920
{docs, comments?, state} =
920921
quoted_to_algebra_with_comments(operands, acc, min_line, max_line, state, fun)
921922

922-
if comments? or eol?(meta) do
923+
if comments? or eol?(meta, state) do
923924
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
924925
else
925926
{docs |> Enum.reduce(&glue(&2, &1)), state}
@@ -1155,7 +1156,7 @@ defmodule Code.Formatter do
11551156
end
11561157

11571158
args = if keyword?, do: left ++ right, else: left ++ [right]
1158-
many_eol? = match?([_, _ | _], args) and eol?(meta)
1159+
many_eol? = match?([_, _ | _], args) and eol?(meta, state)
11591160
no_generators? = no_generators?(args)
11601161
to_algebra_fun = &quoted_to_algebra(&1, context, &2)
11611162

@@ -1346,8 +1347,7 @@ defmodule Code.Formatter do
13461347

13471348
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
13481349
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
1349-
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, state)
1350-
doc = surround("\#{", doc, "}") |> interpolation_to_string()
1350+
{doc, state} = interpolation_to_string(quoted, state)
13511351
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
13521352
end
13531353

@@ -1363,15 +1363,20 @@ defmodule Code.Formatter do
13631363

13641364
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
13651365
{:"::", _, [{{:., _, [Kernel, :to_string]}, _meta, [quoted]}, {:binary, _, _}]} = entry
1366-
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, state)
1367-
doc = surround("\#{", doc, "}") |> interpolation_to_string()
1366+
{doc, state} = interpolation_to_string(quoted, state)
13681367
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
13691368
end
13701369

13711370
defp interpolation_to_algebra([], _escape, state, acc, last) do
13721371
{concat(acc, last), state}
13731372
end
13741373

1374+
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
1375+
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
1376+
doc = interpolation_to_string(surround("\#{", doc, "}"))
1377+
{doc, %{state | skip_eol: skip_eol}}
1378+
end
1379+
13751380
defp interpolation_to_string(doc) do
13761381
[head | tail] =
13771382
doc
@@ -1421,7 +1426,7 @@ defmodule Code.Formatter do
14211426

14221427
defp bitstring_to_algebra(meta, args, state) do
14231428
last = length(args) - 1
1424-
join = if eol?(meta), do: :line, else: :flex_break
1429+
join = if eol?(meta, state), do: :line, else: :flex_break
14251430
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
14261431

14271432
{args_doc, join, state} =
@@ -1489,7 +1494,7 @@ defmodule Code.Formatter do
14891494
## Literals
14901495

14911496
defp list_to_algebra(meta, args, state) do
1492-
join = if eol?(meta), do: :line, else: :break
1497+
join = if eol?(meta, state), do: :line, else: :break
14931498
fun = &quoted_to_algebra(&1, :parens_arg, &2)
14941499

14951500
{args_doc, _join, state} =
@@ -1499,7 +1504,7 @@ defmodule Code.Formatter do
14991504
end
15001505

15011506
defp map_to_algebra(meta, name_doc, [{:|, _, [left, right]}], state) do
1502-
join = if eol?(meta), do: :line, else: :break
1507+
join = if eol?(meta, state), do: :line, else: :break
15031508
fun = &quoted_to_algebra(&1, :parens_arg, &2)
15041509
{left_doc, state} = fun.(left, state)
15051510

@@ -1516,7 +1521,7 @@ defmodule Code.Formatter do
15161521
end
15171522

15181523
defp map_to_algebra(meta, name_doc, args, state) do
1519-
join = if eol?(meta), do: :line, else: :break
1524+
join = if eol?(meta, state), do: :line, else: :break
15201525
fun = &quoted_to_algebra(&1, :parens_arg, &2)
15211526

15221527
{args_doc, _join, state} =
@@ -1527,7 +1532,7 @@ defmodule Code.Formatter do
15271532
end
15281533

15291534
defp tuple_to_algebra(meta, args, join, state) do
1530-
join = if eol?(meta), do: :line, else: join
1535+
join = if eol?(meta, state), do: :line, else: join
15311536
fun = &quoted_to_algebra(&1, :parens_arg, &2)
15321537

15331538
{args_doc, join, state} =
@@ -1709,7 +1714,7 @@ defmodule Code.Formatter do
17091714
|> glue(body_doc)
17101715
|> nest(2)
17111716
|> glue("end")
1712-
|> maybe_force_clauses(clauses)
1717+
|> maybe_force_clauses(clauses, state)
17131718
|> group()
17141719

17151720
{doc, state}
@@ -1743,7 +1748,7 @@ defmodule Code.Formatter do
17431748
|> glue(body_doc)
17441749
|> nest(2)
17451750
|> glue("end")
1746-
|> maybe_force_clauses(clauses)
1751+
|> maybe_force_clauses(clauses, state)
17471752
|> group()
17481753

17491754
{doc, state}
@@ -1771,7 +1776,7 @@ defmodule Code.Formatter do
17711776
"(() -> "
17721777
|> concat(nest(body_doc, :cursor))
17731778
|> concat(")")
1774-
|> maybe_force_clauses(clauses)
1779+
|> maybe_force_clauses(clauses, state)
17751780
|> group()
17761781

17771782
{doc, state}
@@ -1792,7 +1797,7 @@ defmodule Code.Formatter do
17921797
|> group()
17931798
|> concat(break() |> concat(body_doc) |> nest(2))
17941799
|> wrap_in_parens()
1795-
|> maybe_force_clauses(clauses)
1800+
|> maybe_force_clauses(clauses, state)
17961801
|> group()
17971802

17981803
{doc, state}
@@ -1811,8 +1816,8 @@ defmodule Code.Formatter do
18111816

18121817
## Clauses
18131818

1814-
defp maybe_force_clauses(doc, clauses) do
1815-
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta) end) do
1819+
defp maybe_force_clauses(doc, clauses, state) do
1820+
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
18161821
force_unfit(doc)
18171822
else
18181823
doc
@@ -1835,7 +1840,7 @@ defmodule Code.Formatter do
18351840
{doc_acc, state_acc}
18361841
end)
18371842

1838-
{clauses_doc |> maybe_force_clauses([clause | clauses]) |> group(), state}
1843+
{clauses_doc |> maybe_force_clauses([clause | clauses], state) |> group(), state}
18391844
end
18401845

18411846
defp clauses_to_algebra(other, min_line, max_line, state) do
@@ -2176,8 +2181,8 @@ defmodule Code.Formatter do
21762181
false
21772182
end
21782183

2179-
defp eol_or_comments?(meta, %{comments: comments}) do
2180-
eol?(meta) or
2184+
defp eol_or_comments?(meta, %{comments: comments} = state) do
2185+
eol?(meta, state) or
21812186
(
21822187
min_line = line(meta)
21832188
max_line = closing_line(meta)
@@ -2264,9 +2269,8 @@ defmodule Code.Formatter do
22642269
defp keyword_key?(_),
22652270
do: false
22662271

2267-
defp eol?(meta) do
2268-
Keyword.get(meta, :newlines, 0) > 0
2269-
end
2272+
defp eol?(_meta, %{skip_eol: true}), do: false
2273+
defp eol?(meta, _state), do: Keyword.get(meta, :newlines, 0) > 0
22702274

22712275
defp meta?(meta, key) do
22722276
is_list(meta[key])

lib/elixir/test/elixir/code_formatter/integration_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,16 @@ defmodule Code.Formatter.IntegrationTest do
548548
"""
549549
end
550550

551+
test "multiline expression inside interpolation" do
552+
bad = """
553+
Logger.info("Example: #{inspect(%{a: 1, b: 2})}")
554+
"""
555+
556+
assert_format bad, """
557+
Logger.info("Example: #{inspect(%{a: 1, b: 2})}")
558+
"""
559+
end
560+
551561
test "comment inside operator with when" do
552562
bad = """
553563
raise function(x) ::

0 commit comments

Comments
 (0)