From 2cbd5cee83be745b7a7dfd2ca1ccb4358758c464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 10 Feb 2018 20:37:40 +0100 Subject: [PATCH] Ensure -> does not pass line lengths, closes #7279 --- lib/elixir/lib/code/formatter.ex | 29 +++++++++----- lib/elixir/lib/gen_event/stream.ex | 3 +- .../elixir/code_formatter/general_test.exs | 40 +++++++++++++++++++ 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/lib/elixir/lib/code/formatter.ex b/lib/elixir/lib/code/formatter.ex index 6a2485d59d..1d9ce1d292 100644 --- a/lib/elixir/lib/code/formatter.ex +++ b/lib/elixir/lib/code/formatter.ex @@ -1599,10 +1599,16 @@ defmodule Code.Formatter do {args_doc, state} = clause_args_to_algebra(args, min_line, state) {body_doc, state} = block_to_algebra(body, min_line, max_line, state) + head = + args_doc + |> ungroup_if_group() + |> concat(" ->") + |> nest(:cursor) + |> group() + doc = "fn " - |> concat(group(nest(args_doc, :cursor))) - |> concat(" ->") + |> concat(head) |> glue(body_doc) |> nest(2) |> glue("end") @@ -1648,15 +1654,12 @@ defmodule Code.Formatter do {args_doc, state} = clause_args_to_algebra(args, min_line, state) {body_doc, state} = block_to_algebra(body, min_line, max_line, state) - clause_doc = - " ->" - |> glue(body_doc) - |> nest(2) - doc = args_doc + |> ungroup_if_group() + |> concat(" ->") |> group() - |> concat(clause_doc) + |> concat(break() |> concat(body_doc) |> nest(2)) |> wrap_in_parens() |> maybe_force_clauses(clauses) |> group() @@ -1724,7 +1727,15 @@ defmodule Code.Formatter do state = %{state | operand_nesting: nesting} {body_doc, state} = block_to_algebra(body, min_line, end_line(meta), state) - {concat(args_doc, " ->" |> glue(body_doc) |> nest(2)), state} + + doc = + args_doc + |> ungroup_if_group() + |> concat(" ->") + |> group() + |> concat(break() |> concat(body_doc) |> nest(2)) + + {doc, state} end defp add_max_line_to_last_clause([{op, meta, args}], max_line) do diff --git a/lib/elixir/lib/gen_event/stream.ex b/lib/elixir/lib/gen_event/stream.ex index b967db77f0..0ff94bd524 100644 --- a/lib/elixir/lib/gen_event/stream.ex +++ b/lib/elixir/lib/gen_event/stream.ex @@ -161,7 +161,8 @@ defimpl Enumerable, for: GenEvent.Stream do defp flush_events(ref) do receive do - {_from, {_pid, ^ref}, {notify, _event}} when notify in [:notify, :ack_notify, :sync_notify] -> + {_from, {_pid, ^ref}, {notify, _event}} + when notify in [:notify, :ack_notify, :sync_notify] -> flush_events(ref) after 0 -> :ok diff --git a/lib/elixir/test/elixir/code_formatter/general_test.exs b/lib/elixir/test/elixir/code_formatter/general_test.exs index e810525649..52e593f248 100644 --- a/lib/elixir/test/elixir/code_formatter/general_test.exs +++ b/lib/elixir/test/elixir/code_formatter/general_test.exs @@ -329,6 +329,46 @@ defmodule Code.Formatter.GeneralTest do end """ end + + test "with -> on line limit" do + bad = """ + fn ab, cd -> + ab + cd + end + """ + + good = """ + fn ab, + cd -> + ab + cd + end + """ + + assert_format bad, good, @short_length + + bad = """ + fn + ab, cd -> + 1 + xy, zw -> + 2 + end + """ + + good = """ + fn + ab, + cd -> + 1 + + xy, + zw -> + 2 + end + """ + + assert_format bad, good, @short_length + end end describe "anonymous functions types" do