-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make multi-line tuples & binaries next-break-fits #117
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,8 +318,9 @@ binary_op_to_algebra(Op, Meta, Left, Right, Indent) -> | |
combine_comments(Meta, maybe_wrap_in_parens(Meta, Doc)). | ||
|
||
binary_op_to_algebra(Op, Left, Right, LeftD, RightD, Indent) -> | ||
DontBreakCalls = is_call(Right) andalso not has_break_between(Left, Right), | ||
case DontBreakCalls orelse is_next_break_fits(Right) of | ||
BreakFitsException = is_binary_op_break_fits_exception(Right) andalso | ||
not has_break_between(Left, Right), | ||
case is_next_break_fits(Right) orelse BreakFitsException of | ||
true -> | ||
with_next_break_fits(true, RightD, fun(R) -> | ||
concat([group(LeftD), <<" ">>, Op, <<" ">>, group(R)]) | ||
|
@@ -328,6 +329,12 @@ binary_op_to_algebra(Op, Left, Right, LeftD, RightD, Indent) -> | |
breakable_binary_op_to_algebra(Op, Left, Right, LeftD, RightD, Indent) | ||
end. | ||
|
||
is_binary_op_break_fits_exception({call, _, _, _}) -> true; | ||
is_binary_op_break_fits_exception({macro_call, _, _, _}) -> true; | ||
is_binary_op_break_fits_exception({tuple, _, _}) -> true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should only do this if the tuple will be indeed broken out. I think right now something weird will happen for not broken tuples that don't fit, e.g. Foo = {1, 2, 3,
4, 5, 6} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is what happening, given the code in this pull request, the following test will pass ?assertSame(
"Foo = {1, 2, 3,\n"
" 4, 5, 6}\n",
15
), There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't think this should happen. I think for this it should be: Foo =
{1, 2, 3,
4, 5, 6} We should only inline those when the tuple is broken up, e.g. Foo = {
1,
2,
3,
4
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what is happening on master right now: ?assertSame(
"{1, 2, 3,\n"
" 4, 5,\n"
" 6}\n",
11
), ?assertSame(
"Foo =\n"
" {1, 2, 3,\n"
" 4, 5,\n"
" 6}\n",
15
), I think I like what is happening in this pull request ?assertSame(
"Foo = {1, 2, 3,\n"
" 4, 5, 6}\n",
15
), What would you prefer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Aha sorry, didn't see this. I agree :) |
||
is_binary_op_break_fits_exception({bin, _, _}) -> true; | ||
is_binary_op_break_fits_exception(_) -> false. | ||
|
||
breakable_binary_op_to_algebra(OpD, Left, Right, LeftD, RightD, Indent) -> | ||
HasBreak = has_break_between(Left, Right), | ||
RightOpD = nest(break(concat(<<" ">>, OpD), group(RightD)), Indent, break), | ||
|
@@ -713,10 +720,6 @@ has_inner_break(Outer, Inner) -> | |
is_next_break_fits_op(Op) -> | ||
lists:member(Op, ?NEXT_BREAK_FITS_OPS). | ||
|
||
is_call({call, _, _, _}) -> true; | ||
is_call({macro_call, _, _, _}) -> true; | ||
is_call(_) -> false. | ||
|
||
is_next_break_fits(Expr) -> | ||
lists:member(element(1, Expr), ?NEXT_BREAK_FITS) andalso has_no_comments_or_parens(Expr). | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we special-case it only here for assignment or should we do it everywhere i.e. amending
is_next_break_fits
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it out, so basically adding,
call
,macro_call
andbin
to theNEXT_BREAK_FITS
list, results in:instead of before
and
instead of before
So I don't think we should modify
is_next_break_fits
and add these cases.So then there is still a question about whether we should pattern match in
binary_op_to_algebra
to only have this special case forOp =:= '='
.Or did I misinterpret what you were trying to say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about replicating the conditions that make a tuple be laid out with
line
,erlfmt/src/erlfmt_format.erl
Lines 395 to 396 in 15f2a25
This means transforming
is_next_break_fits
into something likeThis means:
NEXT_BREAK_FITS
call
andmacro call
to break after=
and::
operatorsflex
we print them like today when they areflex
, but in cases where they are foced to be multi-line we treat them as any other containerThe last point is the only modification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked very well. I had no instinct for this yet. Great idea :)