diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala index 4a44f0dd0..23ec1f42a 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala @@ -127,7 +127,7 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken] getDelimsIfEnclosed(tree).map(_._2) def isEnclosedInMatching(tokens: Tokens, tree: Tree): Boolean = - getHeadIfEnclosed(tokens, tree).isDefined + getDelimsIfEnclosed(tokens, tree).isDefined def isEnclosedInMatching(tree: Tree): Boolean = isEnclosedInMatching(tree.tokens, tree) 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 aca8f8087..ec825dda4 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 @@ -666,8 +666,14 @@ class Router(formatOps: FormatOps) { // New statement case FormatToken(_: T.Semicolon, _, StartsStatementRight(stmt)) if !stmt.is[Term.EndMarker] => + val noSpace = !style.newlines.okSpaceForSource(newlines) || + style.dialect.allowSignificantIndentation && + stmt.is[Case] && stmt.parent.forall { + case p: Tree.Block => !isEnclosedInMatching(p) + case _ => false + } Seq( - Split(!style.newlines.okSpaceForSource(newlines), 0)(Space) + Split(noSpace, 0)(Space) .withSingleLine(endOfSingleLineBlock(getLast(stmt))), // For some reason, this newline cannot cost 1. Split(Newline2x(ft), 0), diff --git a/scalafmt-tests-community/scala3/src/test/scala/org/scalafmt/community/scala3/CommunityScala3Suite.scala b/scalafmt-tests-community/scala3/src/test/scala/org/scalafmt/community/scala3/CommunityScala3Suite.scala index 1d86a7296..ee7a83920 100644 --- a/scalafmt-tests-community/scala3/src/test/scala/org/scalafmt/community/scala3/CommunityScala3Suite.scala +++ b/scalafmt-tests-community/scala3/src/test/scala/org/scalafmt/community/scala3/CommunityScala3Suite.scala @@ -9,7 +9,7 @@ abstract class CommunityScala3Suite(name: String) class CommunityScala3_2Suite extends CommunityScala3Suite("scala-3.2") { - override protected def totalStatesVisited: Option[Int] = Some(32288017) + override protected def totalStatesVisited: Option[Int] = Some(32288009) override protected def builds = Seq(getBuild("3.2.2", dialects.Scala32, 791)) @@ -17,7 +17,7 @@ class CommunityScala3_2Suite extends CommunityScala3Suite("scala-3.2") { class CommunityScala3_3Suite extends CommunityScala3Suite("scala-3.3") { - override protected def totalStatesVisited: Option[Int] = Some(34858906) + override protected def totalStatesVisited: Option[Int] = Some(34858898) override protected def builds = Seq(getBuild("3.3.3", dialects.Scala33, 861)) diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat index 1cb8693fa..82b4a718d 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat @@ -7519,3 +7519,13 @@ val bar: Int => Pf = >>> val bar: Int => Pf = case msg: Int => msg + 1 +<<< #4133 braceless in `match` with a semicolon between cases +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => +>>> +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat index 73d3db914..030e231bd 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat @@ -7233,3 +7233,13 @@ val bar: Int => Pf = >>> val bar: Int => Pf = case msg: Int => msg + 1 +<<< #4133 braceless in `match` with a semicolon between cases +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => +>>> +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat index de6e009c8..34d5fca5b 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat @@ -7548,3 +7548,13 @@ val bar: Int => Pf = >>> val bar: Int => Pf = case msg: Int => msg + 1 +<<< #4133 braceless in `match` with a semicolon between cases +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => +>>> +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat index cbc418600..b7602ab5a 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -7828,3 +7828,13 @@ val bar: Int => Pf = val bar: Int => Pf = case msg: Int => msg + 1 +<<< #4133 braceless `match` with a semicolon between cases +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => +>>> +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; + case _ => 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 7ff27886f..43896ac07 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -137,7 +137,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, 1497775, "total explored") + assertEquals(explored, 1498031, "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 :'(