Skip to content

Commit

Permalink
change formatting of pipes to prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Sep 28, 2020
1 parent 3c0f44c commit faf8c3a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/erlfmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ insert_pragma_node(Node) ->
erlfmt_scan:location(),
[{width, pos_integer()}]
) ->
{ok, string(), [error_info()]} |
{error, error_info()} |
{options, [{erlfmt_scan:location(), erlfmt_scan:location()}]}.
{ok, string(), [error_info()]}
| {error, error_info()}
| {options, [{erlfmt_scan:location(), erlfmt_scan:location()}]}.
format_range(FileName, StartLocation, EndLocation, Options) ->
Width = proplists:get_value(width, Options, ?DEFAULT_WIDTH),
try
Expand Down
20 changes: 10 additions & 10 deletions src/erlfmt_algebra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@
% - to preserve the users' intention of a newline by breaking up the group or
% - to break things when a comment is present and they have to be broken up
-type doc() ::
binary() |
doc_force_breaks |
doc_nil |
#doc_string{} |
#doc_line{} |
#doc_cons{} |
#doc_nest{} |
#doc_group{} |
#doc_break{} |
#doc_fits{}.
binary()
| doc_force_breaks
| doc_nil
| #doc_string{}
| #doc_line{}
| #doc_cons{}
| #doc_nest{}
| #doc_group{}
| #doc_break{}
| #doc_fits{}.

-define(is_doc(Doc),
((Doc == doc_force_breaks) orelse
Expand Down
22 changes: 11 additions & 11 deletions src/erlfmt_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,19 @@ breakable_binary_op_to_algebra(OpD, Left, Right, LeftD, RightD, Indent) ->
concat(group(LeftD), group(concat(maybe_force_breaks(HasBreak), RightOpD))).

union_to_algebra(Meta, Left, Right) ->
Unions = [Left | collect_unions(Right)],
UnionsD = lists:map(fun expr_to_algebra/1, Unions),
Doc = fold_doc(fun(LeftD, AccD) -> concat(LeftD, break(<<" |">>, AccD)) end, UnionsD),
Doc = break(expr_to_algebra(Left), fold_unions(Right)),
group(concat(maybe_force_breaks(is_multiline(Meta)), Doc)).

collect_unions({op, Meta, '|', Left, Right}) ->
[move_comments(Meta, Left) | collect_unions(Right)];
collect_unions(Other) ->
[Other].
fold_unions({op, Meta, '|', Left, Right}) ->
LeftD = expr_to_algebra(Left),
LeftPipeD = concat(<<"| ">>, LeftD),
Doc = combine_comments(Meta, LeftPipeD),
break(Doc, fold_unions(Right));
fold_unions(Expr) ->
Meta = element(2, Expr),
ExprD = do_expr_to_algebra(Expr),
ExprPipeD = concat(<<"| ">>, ExprD),
combine_comments(Meta, ExprPipeD).

maybe_force_breaks(true) -> force_breaks();
maybe_force_breaks(false) -> empty().
Expand Down Expand Up @@ -752,10 +756,6 @@ maybe_wrap_in_parens(Meta, Doc) ->
false -> Doc
end.

move_comments(Meta, Node) ->
{Pre, Post} = comments(Meta),
erlfmt_recomment:put_pre_comments(erlfmt_recomment:put_post_comments(Node, Post), Pre).

combine_comments_no_force(Meta, Doc) ->
{Pre, Post} = comments(Meta),
combine_post_comments(Post, Meta, combine_pre_comments(Pre, Meta, Doc)).
Expand Down
6 changes: 3 additions & 3 deletions src/erlfmt_scan.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
-type location() :: {pos_integer(), pos_integer()}.
-type token() :: {atom(), anno(), term()} | {atom(), anno()}.
-type node_ret() ::
{ok, [token()], [comment()], state()} |
{error, {erl_anno:location(), module(), term()}, erl_anno:location()} |
{eof, erl_anno:location()}.
{ok, [token()], [comment()], state()}
| {error, {erl_anno:location(), module(), term()}, erl_anno:location()}
| {eof, erl_anno:location()}.

-opaque state() :: #state{}.

Expand Down
4 changes: 2 additions & 2 deletions test/erlfmt_SUITE_data/attributes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
}).

-type ghi() ::
fun() |
binary().
fun()
| binary().

-type klm() :: fun() | binary().

Expand Down
4 changes: 2 additions & 2 deletions test/erlfmt_SUITE_data/simple_comments.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

-spec foo() ->
%% comment
term() |
term()
%% other comment
[term()].
| [term()].
foo() ->
[
[baz(2), foobar(), map(#{}), record(#foo{}), binary(), comprehension()],
Expand Down
17 changes: 13 additions & 4 deletions test/erlfmt_format_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2223,10 +2223,10 @@ spec(Config) when is_list(Config) ->
?assertFormat(
"-spec encode(#a{} | #blonglonglong{} | #c{} | #d{}) -> binary().",
"-spec encode(\n"
" #a{} |\n"
" #blonglonglong{} |\n"
" #c{} |\n"
" #d{}\n"
" #a{}\n"
" | #blonglonglong{}\n"
" | #c{}\n"
" | #d{}\n"
") -> binary().\n",
30
),
Expand Down Expand Up @@ -2473,4 +2473,13 @@ comment(Config) when is_list(Config) ->
" {\n"
" b\n"
" }\n"
),
?assertSame(
"-spec foo() ->\n"
" %% comment\n"
" term()\n"
" %% other comment\n"
" | [term()]\n"
" %% error comment\n"
" | error.\n"
).

0 comments on commit faf8c3a

Please sign in to comment.