Skip to content

Commit

Permalink
partially reverts #262 for equal and dolon to better support next bre…
Browse files Browse the repository at this point in the history
…ak fits in future (#265)

* fix equal in operator chain

* small readability fix

* add test for different operators

* another test

* any op chain

* fmt

* two more test cases

* update tests to be next break fits even when in a chain

* only support rewrite_assoc for operators that are not = or ::

* remove op chain
  • Loading branch information
awalterschulze authored Mar 29, 2021
1 parent 309babd commit 245f40c
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 25 deletions.
17 changes: 15 additions & 2 deletions src/erlfmt_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ binary_op_to_algebra(Op, Meta0, Left0, Right0) ->
{op, _Meta, Op, Left, Right} = rewrite_assoc(Op, Meta, Left0, Right0),
binary_op_to_algebra(Op, Meta, Left, Right, ?INDENT).

rewrite_assoc('=' = Op, Meta, Left, Right) ->
{op, Meta, Op, Left, Right};
rewrite_assoc('::' = Op, Meta, Left, Right) ->
{op, Meta, Op, Left, Right};
rewrite_assoc(Op, MetaABC0, A, {op, MetaBC0, Op, B0, C} = BC) ->
case erlfmt_scan:get_anno(parens, MetaBC0, false) of
true ->
Expand All @@ -333,9 +337,18 @@ update_meta_location(Meta, First, Last) ->
end_location := erlfmt_scan:get_anno(end_location, Last)
}.

binary_operand_to_algebra(Op, {op, Meta, Op, Left, Right}, Indent) ->
%% Same operator, no parens, means correct side and no repeated nesting
case erlfmt_scan:get_anno(parens, Meta, false) of
false -> binary_op_to_algebra(Op, Meta, Left, Right, Indent);
_ -> binary_op_to_algebra(Op, Meta, Left, Right, ?INDENT)
end;
binary_operand_to_algebra(_ParentOp, Expr, _Indent) ->
expr_to_algebra(Expr).

binary_op_to_algebra(Op, Meta, Left, Right, Indent) ->
LeftD = expr_to_algebra(Left),
RightD = expr_to_algebra(Right),
LeftD = binary_operand_to_algebra(Op, Left, Indent),
RightD = binary_operand_to_algebra(Op, Right, 0),
Doc =
case Op of
'::' ->
Expand Down
225 changes: 202 additions & 23 deletions test/erlfmt_format_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -667,29 +667,30 @@ binary_operator_more(Config) when is_list(Config) ->
" _ -> g()\n"
" end\n"
),
?assertSame(
"A = {B, C} =\n"
" case X of\n"
" true -> f();\n"
" _ -> g()\n"
" end.\n"
),
?assertSame(
"A = {{B1, B2}, C} = {B, C} =\n"
" case X of\n"
" true -> f();\n"
" _ -> g()\n"
" end.\n"
),
?assertSame(
"A =\n"
" ({{B1, B2}, C} =\n"
" ({B, C} =\n"
" case X of\n"
" true -> f();\n"
" _ -> g()\n"
" end)).\n"
),
%% TODO: rewrite_assoc did not work for '=' or '::'
% ?assertSame(
% "A = {B, C} =\n"
% " case X of\n"
% " true -> f();\n"
% " _ -> g()\n"
% " end.\n"
% ),
% ?assertSame(
% "A = {{B1, B2}, C} = {B, C} =\n"
% " case X of\n"
% " true -> f();\n"
% " _ -> g()\n"
% " end.\n"
% ),
% ?assertSame(
% "A =\n"
% " ({{B1, B2}, C} =\n"
% " ({B, C} =\n"
% " case X of\n"
% " true -> f();\n"
% " _ -> g()\n"
% " end)).\n"
% ),
?assertSame(
"convert_meta(Key, Value) when\n"
" is_list(Key) orelse is_binary(Key),\n"
Expand Down Expand Up @@ -776,7 +777,185 @@ binary_operator_more(Config) when is_list(Config) ->
" equivalent(L3, R3) andalso\n"
" %% checking L4 and R4\n"
" equivalent(L4, R4)\n"
),
?assertFormat(
"A orelse\n"
" B orelse c(\n"
" D,\n"
" E\n"
" ).\n",
"A orelse\n"
" B orelse\n"
" c(\n"
" D,\n"
" E\n"
" ).\n"
),
?assertFormat(
"A orelse B orelse c(\n"
"D, E).\n",
"A orelse B orelse\n"
" c(\n"
" D,\n"
" E\n"
" ).\n"
),
%% TODO: rewrite_assoc did not work for this
% ?assertSame(
% "A = B = c(\n"
% " D,\n"
% " E\n"
% " ).\n"
% ),
?assertSame(
"case maps:get(AbcdefghId, Abcdefghs0, undefined) of\n"
" #abcdefgh{abcdefgh_type = AbcdefghType, specs = Specs, abc_de_abcde = [AbcdefgId | Rest]} =\n"
" Abcdefgh = #abcdefgh{\n"
" abcdefgh_type = AbcdefghType,\n"
" specs = Specs,\n"
" abc_de_abcde = [AbcdefgId | Rest]\n"
" } ->\n"
" {reply, ok, State}\n"
"end.\n",
100
).
%% TODO: rewrite_assoc worked for this, but we cannot support it now
% ?assertFormat(
% "A = B = C = D = E = F\n.",
% "A = B = C =\n"
% " D = E =\n"
% " F.\n",
% 10
% ),
% ?assertFormat(
% "A = B = C =< D = E = F\n.",
% "A = B =\n"
% " C =< D =\n"
% " E = F.\n",
% 10
% ),
% ?assertFormat(
% "A =\n"
% " B = C = D = E = F\n.",
% "A =\n"
% " B = C =\n"
% " D = E =\n"
% " F.\n",
% 10
% ),
% ?assertFormat(
% "A = B = c(D, E).\n",
% "A = B =\n"
% " c(\n"
% " D,\n"
% " E\n"
% " ).\n",
% 5
% ),
% ?assertSame(
% "A =\n"
% " B = c(\n"
% " D,\n"
% " E\n"
% " ).\n"
% ),
% ?assertSame(
% "A =\n"
% " B = c(\n"
% " D,\n"
% " E\n"
% " ) = f(\n"
% " G\n"
% " ).\n"
% ),
% ?assertSame(
% "A ::\n"
% " B :: c(\n"
% " D,\n"
% " E\n"
% " ).\n"
% ),
% ?assertFormat(
% "\"abc\" ++\n"
% " B ++ c(\n"
% " A,\n"
% " B\n"
% " ).\n",
% "\"abc\" ++\n"
% " B ++\n"
% " c(\n"
% " A,\n"
% " B\n"
% " ).\n"
% ),
% ?assertFormat(
% "\"abc\" ++\n"
% " B = c(\n"
% " A,\n"
% " B\n"
% " ).\n",
% "\"abc\" ++\n"
% " B = c(\n"
% " A,\n"
% " B\n"
% " ).\n"
% ),
% ?assertFormat(
% "\"abc\" =\n"
% " B ++ c(\n"
% " A,\n"
% " B\n"
% " ).\n",
% "\"abc\" =\n"
% " B ++\n"
% " c(\n"
% " A,\n"
% " B\n"
% " ).\n"
% ),
% ?assertFormat(
% "\"abc\" =\n"
% " B ++ c(\n"
% " A,\n"
% " B\n"
% " ) ++ f(\n"
% " G\n"
% " ).\n",
% "\"abc\" =\n"
% " B ++\n"
% " c(\n"
% " A,\n"
% " B\n"
% " ) ++\n"
% " f(\n"
% " G\n"
% " ).\n"
% ),
% ?assertSame(
% "A =\n"
% " B = #c{\n"
% " d = D,\n"
% " e = E\n"
% " }.\n"
% ),
% ?assertSame(
% "A = B =\n"
% " %% comment\n"
% " c(\n"
% " D,\n"
% " E\n"
% " ).\n"
% ),
% ?assertSame(
% "A =\n"
% " %% comment\n"
% " B =\n"
% " %% comment\n"
% " c(\n"
% " D,\n"
% " E\n"
% " ).\n"
% ).

tuple(Config) when is_list(Config) ->
?assertFormat("{ }", "{}\n"),
Expand Down

0 comments on commit 245f40c

Please sign in to comment.