From f67830d78a32a30d71c556e9cdfda3e30f4c03cb 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 ++++--------------- .../test/resources/newlines/source_fold.stat | 2 +- .../test/scala/org/scalafmt/FormatTests.scala | 2 +- 3 files changed, 6 insertions(+), 17 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 5c828eee90..ff3c51c4c9 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/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index 1187f58aa8..23a3b3ce96 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 eb88a5a5f3..2814e64498 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 :'(