From 7b35b4f40fc42b037999bfcf357b9258bdd33b13 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:19:08 -0800 Subject: [PATCH] Router: remove `{` nlPenalty computation Simplify the code a little bit, this penalty was completely superseded by the lambda penalty execution path. --- .../scala/org/scalafmt/internal/Router.scala | 19 ++++--------------- .../CommunityIntellijScalaSuite.scala | 4 ++-- .../scala2/CommunityScala2Suite.scala | 4 ++-- .../scala3/CommunityScala3Suite.scala | 4 ++-- .../community/spark/CommunitySparkSuite.scala | 4 ++-- .../test/resources/newlines/source_fold.stat | 2 +- .../test/scala/org/scalafmt/FormatTests.scala | 2 +- 7 files changed, 14 insertions(+), 25 deletions(-) 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 5c828eee9..ff3c51c4c 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 @@ -434,23 +434,12 @@ class Router(formatOps: FormatOps) { } case _ => NoPolicy } - val nlPenalty = leftOwner match { - case _ if !style.newlines.fold => 0 - case t: Term.ArgClause if (t.values match { - case x :: Nil if !x.is[Term.FunctionTerm] => true - case _ => false - }) => 1 - case t @ Tree.Block(x :: Nil) - if !x.is[Term.FunctionTerm] && t.parent.is[Term.ArgClause] => 1 - case _ => 0 - } val (nlCost, nlArrowPenalty) = if (!nl.isNL) (0, 0) - else if (slbMod eq noSplitMod) (1 + nlPenalty, nlPenalty) - else getLambdaPenaltiesOnLeftBraceOnLeft(ft) - .fold((1, nlPenalty)) { case (sharedPenalty, herePenalty) => - (sharedPenalty + herePenalty, sharedPenalty) - } + else { + if (slbMod eq noSplitMod) None + else getLambdaPenaltiesOnLeftBraceOnLeft(ft) + }.fold((1, 0)) { case (shared, here) => (shared + here, shared) } val newlineBeforeClosingCurly = decideNewlinesOnlyBeforeClose(close) val nlPolicy = lambdaNLPolicy ==> newlineBeforeClosingCurly val nlSplit = Split(nl, nlCost, policy = nlPolicy) 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 e1815e444..bb04de4e0 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(59338266) + override protected def totalStatesVisited: Option[Int] = Some(59317488) 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(59554927) + override protected def totalStatesVisited: Option[Int] = Some(59533781) 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 f714269a9..f9d9e4501 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(42554853) + override protected def totalStatesVisited: Option[Int] = Some(42542831) 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(53041386) + override protected def totalStatesVisited: Option[Int] = Some(53024660) override protected def builds = Seq(getBuild("v2.13.14", dialects.Scala213, 1287)) 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 e2167f7fc..e9812fa5b 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(39228800) + override protected def totalStatesVisited: Option[Int] = Some(39220760) 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(42414545) + override protected def totalStatesVisited: Option[Int] = Some(42405381) override protected def builds = Seq(getBuild("3.3.3", dialects.Scala33, 861)) 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 d4d3b02fa..11c574e94 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(86438662) + override protected def totalStatesVisited: Option[Int] = Some(86363674) 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(91444980) + override protected def totalStatesVisited: Option[Int] = Some(91365411) override protected def builds = Seq(getBuild("v3.5.3", dialects.Scala213, 2756)) 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 1187f58aa..23a3b3ce9 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -9440,7 +9440,7 @@ object a { } } } ->>> { stateVisits = 2250, stateVisits2 = 2250 } +>>> { stateVisits = 2229, stateVisits2 = 2229 } object a { private object MemoMap { def make(implicit trace: Trace): UIO[MemoMap] = Ref.Synchronized 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 eb88a5a5f..2814e6449 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -144,7 +144,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, 1183838, "total explored") + assertEquals(explored, 1183640, "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 :'(