Skip to content

Commit

Permalink
Router: possibly keep LF before select, and indent
Browse files Browse the repository at this point in the history
Currently, the code doesn't introduce newlines into select chains even
if their length exceeds the maximum line length.

It also results in missing indent when a comment is in the middle of
such a chain.

Fix both problems, by allowing a newline if one is present in the input.

Fixes scalameta#1334.
  • Loading branch information
Albert Meltzer committed Nov 24, 2019
1 parent b389ed8 commit bef40c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,16 @@ class Router(formatOps: FormatOps) {
Split(Space, 0, policy = singleLine),
Split(Newline, 1).withPolicy(penalizeNewlineByNesting(cond, arrow))
)
case FormatToken(_, dot @ T.Dot(), _) =>
// allow keeping a newline (and indent) within a select chain
val noNewLine = !style.activeForEdition_2019_11 ||
newlines == 0 || rightOwner.isNot[Term.Select]
val newlinePenalty = if (noNewLine) 0 else 10 + treeDepth(rightOwner)
Seq(
Split(NoSplit, 0),
Split(Newline, newlinePenalty, ignoreIf = noNewLine)
.withIndent(2, dot, Left)
)
// Inline comment
case FormatToken(_, c: T.Comment, between) =>
Seq(Split(newlines2Modification(between), 0))
Expand Down Expand Up @@ -1419,10 +1429,6 @@ class Router(formatOps: FormatOps) {
Split(NoSplit, 0)
)
// Fallback
case FormatToken(_, T.Dot(), _) =>
Seq(
Split(NoSplit, 0)
)
case FormatToken(left, T.Hash(), _) =>
Seq(
Split(if (endsWithSymbolIdent(left)) Space else NoSplit, 0)
Expand Down
14 changes: 7 additions & 7 deletions scalafmt-tests/src/test/resources/default/Select.stat
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ val lastToken = owner.body.tokens.filter {
case _: Whitespace | _: Comment => false
case _ => true
} // edge case, if body is empty expire on arrow.
.lastOption.getOrElse(arrow)
.lastOption.getOrElse(arrow)
<<< apply infix has indent
val expireAAAAAAAAAAAAAAAAAAAAA = owner.tokens.
find(t => t.isInstanceOf[`=`] && owners(t) == owner)
Expand Down Expand Up @@ -286,7 +286,7 @@ class Foo {
>>>
class Foo {
val vv = v.aaa //
.bbb.ccc()
.bbb.ccc()
}
<<< #1334 continue chain indent after a comment, a bit longer
class Foo {
Expand All @@ -299,9 +299,9 @@ class Foo {
>>>
class Foo {
val vv = v.aaa //
.bbb //
.ccc //
.ddd.eee()
.bbb //
.ccc //
.ddd.eee()
}
<<< #1334 continue chain indent after a comment with apply
class Foo {
Expand Down Expand Up @@ -349,6 +349,6 @@ class Foo {
}
>>>
class Foo {
val vv =
v.aaaaaaaaaa.bbbbbbbbbb.cccccccccc.dddddddddd.eeeeeeeeee.ffffffffff.gggggggggg.hhhhhhhhhh.iiiiiiiiii
val vv = v.aaaaaaaaaa.bbbbbbbbbb.cccccccccc.dddddddddd
.eeeeeeeeee.ffffffffff.gggggggggg.hhhhhhhhhh.iiiiiiiiii
}

0 comments on commit bef40c2

Please sign in to comment.