From a4572dd6fb1384832f1e74e8ffc26b1749c0ef9b Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:07:58 -0800 Subject: [PATCH 1/2] Test braceless `match` w/ semicolon between cases --- .../src/test/resources/scala3/OptionalBraces.stat | 10 ++++++++++ .../resources/scala3/OptionalBraces_fold.stat | 15 +++++++++++++++ .../resources/scala3/OptionalBraces_keep.stat | 10 ++++++++++ .../resources/scala3/OptionalBraces_unfold.stat | 10 ++++++++++ .../src/test/scala/org/scalafmt/FormatTests.scala | 2 +- 5 files changed, 46 insertions(+), 1 deletion(-) 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..8b0714636 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,18 @@ 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 _ => +>>> +test does not parse: [dialect scala3] `outdent` expected but `case` found +tag match + case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => + node.refPrivate = in0.readByte() == PRIVATE; case _ => + ^ +====== full result: ====== +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..a38123b17 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, 1497994, "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 :'( From 4153336d6ded3fa2a76845d843c63c4231e6ac23 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 5 Nov 2024 11:29:40 -0800 Subject: [PATCH 2/2] Router: force NL in `; case` of braceless match --- .../main/scala/org/scalafmt/internal/FormatTokens.scala | 2 +- .../src/main/scala/org/scalafmt/internal/Router.scala | 8 +++++++- .../scalafmt/community/scala3/CommunityScala3Suite.scala | 4 ++-- .../src/test/resources/scala3/OptionalBraces_fold.stat | 9 ++------- .../shared/src/test/scala/org/scalafmt/FormatTests.scala | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) 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_fold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat index 8b0714636..030e231bd 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat @@ -7239,12 +7239,7 @@ tag match node.refPrivate = in0.readByte() == PRIVATE; case _ => >>> -test does not parse: [dialect scala3] `outdent` expected but `case` found tag match case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => - node.refPrivate = in0.readByte() == PRIVATE; case _ => - ^ -====== full result: ====== -tag match - case TYPEREFsymbol | TYPEREFdirect | TERMREFsymbol | TERMREFdirect => - node.refPrivate = in0.readByte() == PRIVATE; case _ => + 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 a38123b17..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, 1497994, "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 :'(