From 2624a460d8ddd19960bf36b6f47bddcfe62d685c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:53:16 -0700 Subject: [PATCH] Router: inline braces-to-parens policy We don't need to use it before the opening brace, only after. --- .../org/scalafmt/internal/FormatOps.scala | 15 +++-------- .../scala/org/scalafmt/internal/Router.scala | 26 +++++++++++-------- 2 files changed, 18 insertions(+), 23 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 db679c4e4..338a1fbfe 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 @@ -2977,10 +2977,10 @@ class FormatOps( @inline def indentedPackage(pkg: Pkg): Boolean = indentedPackage(pkg.body) - def getBracesToParensModAndPolicy(rb: FormatToken, mod: Modification)(implicit + def getBracesToParensMod(rb: FormatToken, mod: Modification)(implicit style: ScalafmtConfig, ft: FormatToken, - ): (Modification, Policy) = { + ): Modification = { val isWithinBraces = ft.left.is[T.LeftBrace] val ok = initStyle.rewrite.bracesToParensForOneLineApply && style.rewrite.trailingCommas.isOptional && (mod eq Space) && @@ -2990,16 +2990,7 @@ class FormatOps( // check if all of them can be converted to parens insideBracesBlock(if (isWithinBraces) ft else next(ft), rb.left).ranges .forall(x => couldHaveBracesConvertedToParens(x.lt.leftOwner)) - if (ok) { - val end = Policy.End < rb.left - val beforeClose = prev(rb) - val policy = Policy ? isWithinBraces && - end ==> Policy.on(rb.left, "BracesToParens") { - case Decision(`beforeClose`, ss) => ss - .flatMap(s => if (s.isNL) None else Some(s.withMod(NoSplit))) - } - SpaceOrNoSplit(end) -> policy - } else (mod, Policy.noPolicy) + if (ok) SpaceOrNoSplit(Policy.End < rb.left) else mod } } 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 79d0b02a7..3ab64833f 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 @@ -390,9 +390,9 @@ class Router(formatOps: FormatOps) { } val noSplitMod = xmlSpace(leftOwner) - val (slbMod, slbParensPolicy) = - if (singleLineDecisionOpt.isEmpty) (noSplitMod, NoPolicy) - else getBracesToParensModAndPolicy(closeFT, noSplitMod) + val slbMod = + if (singleLineDecisionOpt.isEmpty) noSplitMod + else getBracesToParensMod(closeFT, noSplitMod) val singleLineSplitOpt = singleLineDecisionOpt.map { sld => val sldPolicy = getSingleLinePolicy val expire = leftOwner.parent match { @@ -403,6 +403,13 @@ class Router(formatOps: FormatOps) { case _ => endOfSingleLineBlock(closeFT) } // copy logic from `( ...`, binpack=never, defining `slbSplit` + val slbParensPolicy = Policy ? (slbMod eq noSplitMod) || { + val beforeClose = prev(closeFT) + Policy.End < close ==> Policy.on(close, "BracesToParens") { + case Decision(`beforeClose`, ss) => ss + .flatMap(s => if (s.isNL) None else Some(s.withMod(NoSplit))) + } + } Split(slbMod, 0).withSingleLine(expire, noSyntaxNL = true) .andPolicy(sldPolicy & slbParensPolicy) } @@ -425,8 +432,8 @@ class Router(formatOps: FormatOps) { else { val nlPolicy = newlineBeforeClosingCurly val policy = singleLineSplitOpt match { - case Some(slbSplit) if slbParensPolicy.isEmpty => - Policy.after(lambdaExpire, "FNARR") { + case Some(slbSplit) if slbMod eq noSplitMod => + Policy.after(lambdaExpire, s"FNARR($slbSplit)") { case Decision(FormatToken(`lambdaExpire`, _, _), ss) => var hadNoSplit = false val nlSplits = ss.flatMap { s => @@ -444,7 +451,7 @@ class Router(formatOps: FormatOps) { val singleLineSplit = singleLineSplitOpt match { case Some(slbSplit) - if slbParensPolicy.nonEmpty || lambdaSplit.isIgnored => slbSplit + if (slbMod ne noSplitMod) || lambdaSplit.isIgnored => slbSplit case _ => Split.ignored } val splits = Seq(singleLineSplit, lambdaSplit, nlSplit) @@ -1514,11 +1521,8 @@ class Router(formatOps: FormatOps) { .exists(x => isTokenLastOrAfter(x.left, roPos)) } } => - val rb = matching(lb) - val (mod, toParensPolicy) = getBracesToParensModAndPolicy(rb, Space) - val policy = Policy - .RelayOnSplit { case (s, _) => s.isNL }(toParensPolicy, NoPolicy) - Seq(Split(mod, 0, policy = policy)) + val mod = getBracesToParensMod(matching(lb), Space) + Seq(Split(mod, 0)) // Delim case FormatToken(left, _: T.Comma, _)