diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2017-10-09 23:55:40 +0200 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2017-10-09 23:55:49 +0200 |
commit | 9e5126256fab29747c829bd1cb25f0022f31050b (patch) | |
tree | 36888b440cfb2c8c009aba6fac7160f4780d384d | |
parent | 005c96d8668192af7cea46032317880bae1233e2 (diff) | |
download | elixir-9e5126256fab29747c829bd1cb25f0022f31050b.tar.gz |
Do not apply next_break_fits if eol is present
-rw-r--r-- | lib/elixir/lib/code/formatter.ex | 4 | ||||
-rw-r--r-- | lib/elixir/src/elixir_parser.yrl | 56 | ||||
-rw-r--r-- | lib/elixir/test/elixir/code_formatter/integration_test.exs | 7 |
3 files changed, 37 insertions, 30 deletions
diff --git a/lib/elixir/lib/code/formatter.ex b/lib/elixir/lib/code/formatter.ex index db6438ff7..130f4ece4 100644 --- a/lib/elixir/lib/code/formatter.ex +++ b/lib/elixir/lib/code/formatter.ex @@ -684,7 +684,9 @@ defmodule Code.Formatter do if op_info == parent_info, do: doc, else: group(nest(doc, :cursor)) true -> - with_next_break_fits(next_break_fits?(right_arg), right, fn right -> + next_break_fits? = next_break_fits?(right_arg) and not Keyword.get(meta, :eol, false) + + with_next_break_fits(next_break_fits?, right, fn right -> op_string = " " <> op_string concat(left, group(nest(glue(op_string, group(right)), nesting, :break))) end) diff --git a/lib/elixir/src/elixir_parser.yrl b/lib/elixir/src/elixir_parser.yrl index 2a918a857..88c40beba 100644 --- a/lib/elixir/src/elixir_parser.yrl +++ b/lib/elixir/src/elixir_parser.yrl @@ -378,48 +378,51 @@ close_curly -> eol '}' : '$2'. % Operators +unary_op_eol -> unary_op : '$1'. +unary_op_eol -> unary_op eol : '$1'. +unary_op_eol -> dual_op : '$1'. +unary_op_eol -> dual_op eol : '$1'. + +capture_op_eol -> capture_op : '$1'. +capture_op_eol -> capture_op eol : '$1'. + +at_op_eol -> at_op : '$1'. +at_op_eol -> at_op eol : '$1'. + add_op_eol -> add_op : '$1'. -add_op_eol -> add_op eol : '$1'. +add_op_eol -> add_op eol : next_is_eol('$1'). add_op_eol -> dual_op : '$1'. -add_op_eol -> dual_op eol : '$1'. +add_op_eol -> dual_op eol : next_is_eol('$1'). mult_op_eol -> mult_op : '$1'. -mult_op_eol -> mult_op eol : '$1'. +mult_op_eol -> mult_op eol : next_is_eol('$1'). two_op_eol -> two_op : '$1'. -two_op_eol -> two_op eol : '$1'. +two_op_eol -> two_op eol : next_is_eol('$1'). three_op_eol -> three_op : '$1'. -three_op_eol -> three_op eol : '$1'. +three_op_eol -> three_op eol : next_is_eol('$1'). pipe_op_eol -> pipe_op : '$1'. pipe_op_eol -> pipe_op eol : next_is_eol('$1'). -capture_op_eol -> capture_op : '$1'. -capture_op_eol -> capture_op eol : '$1'. - -unary_op_eol -> unary_op : '$1'. -unary_op_eol -> unary_op eol : '$1'. -unary_op_eol -> dual_op : '$1'. -unary_op_eol -> dual_op eol : '$1'. - match_op_eol -> match_op : '$1'. -match_op_eol -> match_op eol : '$1'. +match_op_eol -> match_op eol : next_is_eol('$1'). and_op_eol -> and_op : '$1'. -and_op_eol -> and_op eol : '$1'. +and_op_eol -> and_op eol : next_is_eol('$1'). or_op_eol -> or_op : '$1'. -or_op_eol -> or_op eol : '$1'. +or_op_eol -> or_op eol : next_is_eol('$1'). in_op_eol -> in_op : '$1'. -in_op_eol -> in_op eol : '$1'. +in_op_eol -> in_op eol : next_is_eol('$1'). in_match_op_eol -> in_match_op : '$1'. -in_match_op_eol -> in_match_op eol : '$1'. +in_match_op_eol -> in_match_op eol : next_is_eol('$1'). type_op_eol -> type_op : '$1'. -type_op_eol -> type_op eol : '$1'. +type_op_eol -> type_op eol : next_is_eol('$1'). when_op_eol -> when_op : '$1'. when_op_eol -> when_op eol : next_is_eol('$1'). @@ -427,14 +430,11 @@ when_op_eol -> when_op eol : next_is_eol('$1'). stab_op_eol -> stab_op : '$1'. stab_op_eol -> stab_op eol : next_is_eol('$1'). -at_op_eol -> at_op : '$1'. -at_op_eol -> at_op eol : '$1'. - comp_op_eol -> comp_op : '$1'. -comp_op_eol -> comp_op eol : '$1'. +comp_op_eol -> comp_op eol : next_is_eol('$1'). rel_op_eol -> rel_op : '$1'. -rel_op_eol -> rel_op eol : '$1'. +rel_op_eol -> rel_op eol : next_is_eol('$1'). arrow_op_eol -> arrow_op : '$1'. arrow_op_eol -> arrow_op eol : next_is_eol('$1'). @@ -674,12 +674,10 @@ build_op({_Kind, Location, 'in'}, {UOp, _, [Left]}, Right) when ?rearrange_uop(U %% TODO: Deprecate "not left in right" rearrangement on 1.7 {UOp, meta_from_location(Location), [{'in', meta_from_location(Location), [Left, Right]}]}; -build_op({EolOp, Location, Op} = Token, Left, Right) when EolOp == arrow_op; EolOp == stab_op; EolOp == pipe_op; EolOp == when_op -> - {Op, eol_op(Token) ++ meta_from_location(Location), [Left, Right]}; build_op({_Kind, Location, 'not in'}, Left, Right) -> {'not', meta_from_location(Location), [{'in', meta_from_location(Location), [Left, Right]}]}; build_op({_Kind, Location, Op}, Left, Right) -> - {Op, meta_from_location(Location), [Left, Right]}. + {Op, eol_op(Location) ++ meta_from_location(Location), [Left, Right]}. build_unary_op({_Kind, Location, Op}, Expr) -> {Op, meta_from_location(Location), [Expr]}. @@ -726,10 +724,10 @@ eol_pair(Left, Right) -> [] end. -eol_op(Token) -> +eol_op(Location) -> case ?formatter_metadata() of true -> - case ?location(Token) of + case Location of {_, _, eol} -> [{eol, true}]; _ -> [] end; diff --git a/lib/elixir/test/elixir/code_formatter/integration_test.exs b/lib/elixir/test/elixir/code_formatter/integration_test.exs index 0298d4096..5bf3e6c49 100644 --- a/lib/elixir/test/elixir/code_formatter/integration_test.exs +++ b/lib/elixir/test/elixir/code_formatter/integration_test.exs @@ -230,4 +230,11 @@ defmodule Code.Formatter.IntegrationTest do into: %{} """ end + + test "next break fits followed by inline tuple" do + assert_same """ + assert ExUnit.Filters.eval([line: "1"], [:line], %{line: 3, describe_line: 2}, tests) == + {:error, "due to line filter"} + """ + end end |