Skip to content

Commit cba11d6

Browse files
author
José Valim
committed
Ensure -> does not pass line lengths, closes #7279
1 parent 966f8f7 commit cba11d6

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,10 +1549,16 @@ defmodule Code.Formatter do
15491549
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
15501550
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
15511551

1552+
head =
1553+
args_doc
1554+
|> ungroup_if_group()
1555+
|> concat(" ->")
1556+
|> nest(:cursor)
1557+
|> group()
1558+
15521559
doc =
15531560
"fn "
1554-
|> concat(group(nest(args_doc, :cursor)))
1555-
|> concat(" ->")
1561+
|> concat(head)
15561562
|> glue(body_doc)
15571563
|> nest(2)
15581564
|> glue("end")
@@ -1598,15 +1604,12 @@ defmodule Code.Formatter do
15981604
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
15991605
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
16001606

1601-
clause_doc =
1602-
" ->"
1603-
|> glue(body_doc)
1604-
|> nest(2)
1605-
16061607
doc =
16071608
args_doc
1609+
|> ungroup_if_group()
1610+
|> concat(" ->")
16081611
|> group()
1609-
|> concat(clause_doc)
1612+
|> concat(break() |> concat(body_doc) |> nest(2))
16101613
|> wrap_in_parens()
16111614
|> maybe_force_clauses(clauses)
16121615
|> group()
@@ -1674,7 +1677,15 @@ defmodule Code.Formatter do
16741677

16751678
state = %{state | operand_nesting: nesting}
16761679
{body_doc, state} = block_to_algebra(body, min_line, end_line(meta), state)
1677-
{concat(args_doc, " ->" |> glue(body_doc) |> nest(2)), state}
1680+
1681+
doc =
1682+
args_doc
1683+
|> ungroup_if_group()
1684+
|> concat(" ->")
1685+
|> group()
1686+
|> concat(break() |> concat(body_doc) |> nest(2))
1687+
1688+
{doc, state}
16781689
end
16791690

16801691
defp add_max_line_to_last_clause([{op, meta, args}], max_line) do

lib/elixir/lib/gen_event/stream.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ defimpl Enumerable, for: GenEvent.Stream do
161161

162162
defp flush_events(ref) do
163163
receive do
164-
{_from, {_pid, ^ref}, {notify, _event}} when notify in [:notify, :ack_notify, :sync_notify] ->
164+
{_from, {_pid, ^ref}, {notify, _event}}
165+
when notify in [:notify, :ack_notify, :sync_notify] ->
165166
flush_events(ref)
166167
after
167168
0 -> :ok

lib/elixir/test/elixir/code_formatter/general_test.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,46 @@ defmodule Code.Formatter.GeneralTest do
329329
end
330330
"""
331331
end
332+
333+
test "with -> on line limit" do
334+
bad = """
335+
fn ab, cd ->
336+
ab + cd
337+
end
338+
"""
339+
340+
good = """
341+
fn ab,
342+
cd ->
343+
ab + cd
344+
end
345+
"""
346+
347+
assert_format bad, good, @short_length
348+
349+
bad = """
350+
fn
351+
ab, cd ->
352+
1
353+
xy, zw ->
354+
2
355+
end
356+
"""
357+
358+
good = """
359+
fn
360+
ab,
361+
cd ->
362+
1
363+
364+
xy,
365+
zw ->
366+
2
367+
end
368+
"""
369+
370+
assert_format bad, good, @short_length
371+
end
332372
end
333373

334374
describe "anonymous functions types" do

0 commit comments

Comments
 (0)