From 037d5c7b0c7139947508dc5d2a68ac6946902712 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:28:19 -0800 Subject: [PATCH 1/2] Tewt first call in removed braces --- .../resources/newlines/source_classic.stat | 16 +++++++++++++ .../test/resources/newlines/source_fold.stat | 14 +++++++++++ .../test/resources/newlines/source_keep.stat | 15 ++++++++++++ .../resources/newlines/source_unfold.stat | 24 +++++++++++++++++++ .../test/scala/org/scalafmt/FormatTests.scala | 2 +- 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat index 3e77dd917..1233f04a1 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat @@ -11117,3 +11117,19 @@ runtime.unsafe } yield () } .getOrThrowFiberFailure() +<<< #4133 first call in removed braces +maxColumn = 78 +newlines.avoidForSimpleOverflow = all +rewrite.rules = [RedundantBraces] +=== +val WatchHeartBeatInterval: FiniteDuration = { + WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") +}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0") +>>> +val WatchHeartBeatInterval: FiniteDuration = + WatchFailureDetectorConfig + .getMillisDuration("heartbeat-interval") + .requiring( + _ > Duration.Zero, + "watch-failure-detector.heartbeat-interval must be > 0" + ) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index 1d8559d43..474c3ef46 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -10391,3 +10391,17 @@ runtime.unsafe.run { runtime.unsafe.run { for { _ <- ZIO.foreachDiscard(1.to(n))(_ => ZIO.yieldNow) } yield () }.getOrThrowFiberFailure() +<<< #4133 first call in removed braces +maxColumn = 78 +newlines.avoidForSimpleOverflow = all +rewrite.rules = [RedundantBraces] +=== +val WatchHeartBeatInterval: FiniteDuration = { + WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") +}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0") +>>> +val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig + .getMillisDuration("heartbeat-interval").requiring( + _ > Duration.Zero, + "watch-failure-detector.heartbeat-interval must be > 0" + ) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat index bab22975a..c6f09c948 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat @@ -10868,3 +10868,18 @@ runtime.unsafe.run { _ <- ZIO.foreachDiscard(1.to(n))(_ => ZIO.yieldNow) } yield () }.getOrThrowFiberFailure() +<<< #4133 first call in removed braces +maxColumn = 78 +newlines.avoidForSimpleOverflow = all +rewrite.rules = [RedundantBraces] +=== +val WatchHeartBeatInterval: FiniteDuration = { + WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") +}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0") +>>> +val WatchHeartBeatInterval: FiniteDuration = + WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") + .requiring( + _ > Duration.Zero, + "watch-failure-detector.heartbeat-interval must be > 0" + ) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat index 09b747095..cfc72521c 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat @@ -11272,3 +11272,27 @@ runtime } yield () } .getOrThrowFiberFailure() +<<< #4133 first call in removed braces +maxColumn = 78 +newlines.avoidForSimpleOverflow = all +rewrite.rules = [RedundantBraces] +=== +val WatchHeartBeatInterval: FiniteDuration = { + WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") +}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0") +>>> +Idempotency violated +=> Diff (- obtained, + expected) +-val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig +- .getMillisDuration("heartbeat-interval") +- .requiring( +- _ > Duration.Zero, +- "watch-failure-detector.heartbeat-interval must be > 0" +- ) ++val WatchHeartBeatInterval: FiniteDuration = ++ WatchFailureDetectorConfig ++ .getMillisDuration("heartbeat-interval") ++ .requiring( ++ _ > Duration.Zero, ++ "watch-failure-detector.heartbeat-interval must be > 0" ++ ) 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 9a30349a8..cffeedbd4 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, 1115673, "total explored") + assertEquals(explored, 1115904, "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 ac3acfa962e99ea56649a28501ba91a3f9e73d0a Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:49:37 -0800 Subject: [PATCH 2/2] Router: skip removed block when traversing --- .../org/scalafmt/internal/FormatOps.scala | 5 ++-- .../scala/org/scalafmt/util/TreeOps.scala | 25 ++++++++++--------- .../resources/newlines/source_unfold.stat | 21 +++++----------- .../test/scala/org/scalafmt/FormatTests.scala | 2 +- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index f6449fb3c..069f8be4c 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -1625,8 +1625,9 @@ class FormatOps( case _: Term.TryClause => Split.ignored // don't tuck curried apply case t: Term.Apply if t.fun.is[Term.Apply] => slbSplit(expire) - case EndOfFirstCall(end) if !slbOnly => slbSplit(end) - case _ => slbSplit(expire) + case t => + val endOpt = if (slbOnly) None else getEndOfFirstCall(t) + slbSplit(endOpt.getOrElse(expire)) } } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index b170f26aa..38032ea85 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -555,19 +555,20 @@ object TreeOps { case _ => Some(false) }.isDefined - object EndOfFirstCall { - def unapply(tree: Tree): Option[Token] = traverse(tree, None) - .map(_.tokens.last) - + def getEndOfFirstCall( + tree: Tree, + )(implicit ftoks: FormatTokens): Option[Token] = { @tailrec - private def traverse(tree: Tree, res: Option[Tree]): Option[Tree] = - tree match { - case t: Term.Select if res.isDefined => traverse(t.qual, Some(t.qual)) - case t: Term.ApplyType => traverse(t.fun, Some(t)) - case t: Member.Apply => traverse(t.fun, Some(t.fun)) - case t: Init => traverse(t.tpe, Some(t.tpe)) - case _ => res - } + def traverse(tree: Tree, res: Option[Tree]): Option[Tree] = tree match { + case t: Term.Select if res.isDefined => traverse(t.qual, Some(t.qual)) + case t: Term.ApplyType => traverse(t.fun, Some(t)) + case t: Member.Apply => traverse(t.fun, Some(t.fun)) + case t: Init => traverse(t.tpe, Some(t.tpe)) + case Term.Block(arg :: Nil) if !ftoks.isEnclosedInBraces(tree) => + traverse(arg, res) + case _ => res + } + traverse(tree, None).map(_.tokens.last) } @inline diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat index cfc72521c..d8685ea85 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat @@ -11281,18 +11281,9 @@ val WatchHeartBeatInterval: FiniteDuration = { WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval") }.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0") >>> -Idempotency violated -=> Diff (- obtained, + expected) --val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig -- .getMillisDuration("heartbeat-interval") -- .requiring( -- _ > Duration.Zero, -- "watch-failure-detector.heartbeat-interval must be > 0" -- ) -+val WatchHeartBeatInterval: FiniteDuration = -+ WatchFailureDetectorConfig -+ .getMillisDuration("heartbeat-interval") -+ .requiring( -+ _ > Duration.Zero, -+ "watch-failure-detector.heartbeat-interval must be > 0" -+ ) +val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig + .getMillisDuration("heartbeat-interval") + .requiring( + _ > Duration.Zero, + "watch-failure-detector.heartbeat-interval must be > 0" + ) 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 cffeedbd4..786c51a28 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, 1115904, "total explored") + assertEquals(explored, 1115896, "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 :'(