Skip to content

Commit

Permalink
Router: add explicit rules for do/while in Term.Do
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 16, 2024
1 parent 2351028 commit 8cdd44a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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] ||
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2280,7 +2281,8 @@ for (taskSet <- sortedTaskSets; maxLocality <- taskSet.myLocalityLevels) {
shuffledOffers,
availableCpus,
tasks
) while (launchedTask)
)
while (launchedTask)
}
<<< body: do-while, braces, wide
maxColumn = 25
Expand All @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,8 @@ object a {
do (
a
+ b
) while (true)
)
while (true)
do (
a
+ b
Expand All @@ -1192,7 +1193,8 @@ object a {
do {
a +
b
} while (true)
}
while (true)
do {
a +
b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :'(
Expand Down

0 comments on commit 8cdd44a

Please sign in to comment.