From bef40c2ce02e038979e09bb3c64ea512b8dd0d1f Mon Sep 17 00:00:00 2001 From: Albert Meltzer Date: Tue, 19 Nov 2019 22:02:22 -0800 Subject: [PATCH] Router: possibly keep LF before select, and indent 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 #1334. --- .../main/scala/org/scalafmt/internal/Router.scala | 14 ++++++++++---- .../src/test/resources/default/Select.stat | 14 +++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 88b6fc93ed..b4350e796b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -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)) @@ -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) diff --git a/scalafmt-tests/src/test/resources/default/Select.stat b/scalafmt-tests/src/test/resources/default/Select.stat index c8f8e555e0..14a2bdbcfb 100644 --- a/scalafmt-tests/src/test/resources/default/Select.stat +++ b/scalafmt-tests/src/test/resources/default/Select.stat @@ -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) @@ -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 { @@ -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 { @@ -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 }