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 ff2d2b996..7e8545b04 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 @@ -2076,7 +2076,7 @@ class Router(formatOps: FormatOps) { .indentAndBreakBeforeCtrl[T.KwThen](owner.cond, mlSplitBase) Seq(slb, mlSplitOpt.getOrElse(mlSplitBase)) case FormatToken(_: T.KwWhile | _: T.KwFor, right, _) - if !leftOwner.is[Term.Name] => // exclude end marker + if leftOwner.isAny[Term.While, Term.ForClause] => // exclude end marker def spaceMod = Space(style.spaces.isSpaceAfterKeyword(right)) val splitBase = { val onlyNL = style.newlines.keepBreak(newlines) @@ -2567,6 +2567,37 @@ class Router(formatOps: FormatOps) { .fold(!rightOwner.is[Pat.Alternative])(_._1 eq BinPack.Site.Never) Seq(Split(if (ok) getNoSplitBeforeClosing(ft, Newline) else Newline, 0)) + case FormatToken(_: T.KwDo, _, _) if leftOwner.is[Term.Do] => + val owner = leftOwner.asInstanceOf[Term.Do] + val eft = getLast(owner.body) + val end = eft.left + val indent = Indent(style.indent.main, end, ExpiresOn.After) + val kwWhile = nextNonComment(eft).right + val noSplit = + if (noBreak() && isRightCommentThenBreak(ft)) Split(Space, 0) + .withIndents(indent) + else { + val exclude = insideBracesBlock(ft, end, parensToo = true) + Split(Space, 0) + .withSingleLine(endOfSingleLineBlock(eft), exclude = exclude) + } + val nlSplit = + Split(Newline, 1, policy = decideNewlinesOnlyBeforeToken(kwWhile)) + .withIndents(indent) + Seq(noSplit, nlSplit) + + case FormatToken(left, _: T.KwWhile, _) if rightOwner.is[Term.Do] => + if (hasBlankLine) Seq(Split(Newline2x, 0)) + else { + val nlOnly = left.is[T.RightBrace] && + style.newlines.alwaysBeforeElseAfterCurlyIf && + leftOwner.parent.contains(rightOwner) || + !style.newlines.sourceIgnored && hasBreak() + val noSplit = Split(nlOnly, 0)(Space) + val nlSplit = Split(Newline, 1) + Seq(noSplit, nlSplit) + } + case FormatToken(left, _: T.KwCatch | _: T.KwFinally, _) if style.newlines.alwaysBeforeElseAfterCurlyIf || !left.is[T.RightBrace] || @@ -2575,8 +2606,8 @@ class Router(formatOps: FormatOps) { Seq(Split(Newline2x(hasBlankLine), 0)) case FormatToken(_, Reserved(), _) => Seq(Split(Space, 0)) - case FormatToken(Reserved(), _, _) => Seq(Split(Space, 0)) + case FormatToken(T.LeftBracket(), _, _) => Seq(Split(NoSplit, 0)) case FormatToken(_, _: T.Symbolic, _) => Seq(Split(Space, 0)) case FormatToken(T.Underscore(), T.Ident("*"), _) => Seq(Split(NoSplit, 0)) diff --git a/scalafmt-tests-community/intellij/src/test/scala/org/scalafmt/community/intellij/CommunityIntellijScalaSuite.scala b/scalafmt-tests-community/intellij/src/test/scala/org/scalafmt/community/intellij/CommunityIntellijScalaSuite.scala index 99d52def4..23ae2efe4 100644 --- a/scalafmt-tests-community/intellij/src/test/scala/org/scalafmt/community/intellij/CommunityIntellijScalaSuite.scala +++ b/scalafmt-tests-community/intellij/src/test/scala/org/scalafmt/community/intellij/CommunityIntellijScalaSuite.scala @@ -13,7 +13,7 @@ abstract class CommunityIntellijScalaSuite(name: String) class CommunityIntellijScala_2024_2_Suite extends CommunityIntellijScalaSuite("intellij-scala-2024.2") { - override protected def totalStatesVisited: Option[Int] = Some(48190466) + override protected def totalStatesVisited: Option[Int] = Some(48193028) override protected def builds = Seq(getBuild( "2024.2.28", @@ -51,7 +51,7 @@ class CommunityIntellijScala_2024_2_Suite class CommunityIntellijScala_2024_3_Suite extends CommunityIntellijScalaSuite("intellij-scala-2024.3") { - override protected def totalStatesVisited: Option[Int] = Some(48370242) + override protected def totalStatesVisited: Option[Int] = Some(48372804) override protected def builds = Seq(getBuild( "2024.3.4", diff --git a/scalafmt-tests-community/scala2/src/test/scala/org/scalafmt/community/scala2/CommunityScala2Suite.scala b/scalafmt-tests-community/scala2/src/test/scala/org/scalafmt/community/scala2/CommunityScala2Suite.scala index 81ee4e240..9c75d82ce 100644 --- a/scalafmt-tests-community/scala2/src/test/scala/org/scalafmt/community/scala2/CommunityScala2Suite.scala +++ b/scalafmt-tests-community/scala2/src/test/scala/org/scalafmt/community/scala2/CommunityScala2Suite.scala @@ -9,7 +9,7 @@ abstract class CommunityScala2Suite(name: String) class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") { - override protected def totalStatesVisited: Option[Int] = Some(35856475) + override protected def totalStatesVisited: Option[Int] = Some(35856745) override protected def builds = Seq(getBuild("v2.12.20", dialects.Scala212, 1277)) @@ -18,7 +18,7 @@ class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") { class CommunityScala2_13Suite extends CommunityScala2Suite("scala-2.13") { - override protected def totalStatesVisited: Option[Int] = Some(44618135) + override protected def totalStatesVisited: Option[Int] = Some(44618129) override protected def builds = Seq(getBuild("v2.13.14", dialects.Scala213, 1287)) diff --git a/scalafmt-tests-community/spark/src/test/scala/org/scalafmt/community/spark/CommunitySparkSuite.scala b/scalafmt-tests-community/spark/src/test/scala/org/scalafmt/community/spark/CommunitySparkSuite.scala index aacf163e3..7b438be87 100644 --- a/scalafmt-tests-community/spark/src/test/scala/org/scalafmt/community/spark/CommunitySparkSuite.scala +++ b/scalafmt-tests-community/spark/src/test/scala/org/scalafmt/community/spark/CommunitySparkSuite.scala @@ -9,7 +9,7 @@ abstract class CommunitySparkSuite(name: String) class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") { - override protected def totalStatesVisited: Option[Int] = Some(72480315) + override protected def totalStatesVisited: Option[Int] = Some(72481120) override protected def builds = Seq(getBuild("v3.4.1", dialects.Scala213, 2585)) @@ -18,7 +18,7 @@ class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") { class CommunitySpark3_5Suite extends CommunitySparkSuite("spark-3.5") { - override protected def totalStatesVisited: Option[Int] = Some(76649844) + override protected def totalStatesVisited: Option[Int] = Some(76650649) override protected def builds = Seq(getBuild("v3.5.3", dialects.Scala213, 2756)) diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat index f481a2dd1..6303066d2 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat @@ -2261,7 +2261,8 @@ for (taskSet <- sortedTaskSets; maxLocality <- taskSet.myLocalityLevels) { shuffledOffers, availableCpus, tasks - ) while (launchedTask) + ) + while (launchedTask) } <<< #4133 do-while within for, extra block rewrite.redundantBraces.maxLines = 100 @@ -2280,7 +2281,8 @@ for (taskSet <- sortedTaskSets; maxLocality <- taskSet.myLocalityLevels) { shuffledOffers, availableCpus, tasks - ) while (launchedTask) + ) + while (launchedTask) } <<< body: do-while, braces, wide maxColumn = 25 @@ -2301,8 +2303,10 @@ object a { >>> object a { do foo while (true) - do a + b while (true) - do a + b while (true) + do a + b + while (true) + do a + b + while (true) } <<< body: do-while, braces, narrow maxColumn = 12 @@ -2322,9 +2326,10 @@ object a { } >>> object a { - do foo while (true) - do a + - b while (true) - do a + - b while (true) + do foo + while (true) + do a + b + while (true) + do a + b + while (true) } diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantParens.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantParens.stat index 57fc08c65..d15a9308e 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantParens.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantParens.stat @@ -1166,7 +1166,8 @@ object a { do ( a + b - ) while (true) + ) + while (true) do ( a + b @@ -1192,7 +1193,8 @@ object a { do { a + b - } while (true) + } + while (true) do { a + b diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index 7763f4243..cf9faebad 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -138,7 +138,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { val explored = Debug.explored.get() logger.debug(s"Total explored: $explored") if (!onlyUnit && !onlyManual) - assertEquals(explored, 1497583, "total explored") + assertEquals(explored, 1497632, "total explored") val results = debugResults.result() // TODO(olafur) don't block printing out test results. // I don't want to deal with scalaz's Tasks :'(