From c5122ce119f43f2380fae681fd79cf7755ca8d6f Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 10 May 2024 11:37:21 -0700 Subject: [PATCH] Router: binpack: refactor policies, newline split --- .../scala/org/scalafmt/internal/Router.scala | 53 ++++++++++--------- 1 file changed, 28 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 cb8ad1bae9..ca977e67af 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 @@ -1132,7 +1132,7 @@ class Router(formatOps: FormatOps) { def noSplitPolicy: Policy = if (slbOnly) slbPolicy else { - val penalizeOpens = bracketPenalty.fold(Policy.noPolicy) { p => + val opensPolicy = bracketPenalty.fold(Policy.noPolicy) { p => Policy.before(close) { case Decision(ftd @ FormatToken(o: T.LeftBracket, _, m), s) if isParamClauseSite(m.leftOwner) && styleMap.at(o) @@ -1145,7 +1145,7 @@ class Router(formatOps: FormatOps) { if (oneline && isSingleArg) NoPolicy else SingleLineBlock(x.tokens.last, noSyntaxNL = true) } - argPolicy & (penalizeOpens | penalizeBrackets) + argPolicy & (opensPolicy | penalizeBrackets) } val rightIsComment = right.is[T.Comment] val mustUseNL = onlyConfigStyle || @@ -1154,8 +1154,9 @@ class Router(formatOps: FormatOps) { val noSplitModification = if (rightIsComment && !mustUseNL) getMod(ft) else baseNoSplitMod val nlMod = if (rightIsComment && mustUseNL) getMod(ft) else Newline - val nlDanglePolicy = - if (mustDangle) decideNewlinesOnlyBeforeClose(close) else NoPolicy + def getDanglePolicy(implicit fileLine: FileLine) = + decideNewlinesOnlyBeforeClose(close) + val nlPolicy = if (mustDangle) getDanglePolicy else NoPolicy def nlCost = bracketPenalty.getOrElse(1) Seq( @@ -1163,7 +1164,7 @@ class Router(formatOps: FormatOps) { .withOptimalToken(close, ignore = !slbOnly, killOnFail = true) .withPolicy(noSplitPolicy).withIndents(noSplitIndents), Split(nlMod, if (mustUseNL || slbOnly) 0 else nlCost) - .withPolicy(nlDanglePolicy & nlOnelinePolicy & penalizeBrackets) + .withPolicy(nlPolicy & nlOnelinePolicy & penalizeBrackets) .withIndent(indent), ) } @@ -1255,7 +1256,6 @@ class Router(formatOps: FormatOps) { if (oneline) nextCommaOneline.orElse(optClose) else if (style.newlines.source.eq(Newlines.fold)) None else findComma(ft).orElse(optClose) - def unindentPolicy = unindentAtExclude(exclude, Num(-indentLen)) def scajaJsPolicy = Policy(Policy.End.On(close)) { case d: Decision if d.formatToken.right eq close => d.noNewlines } @@ -1270,19 +1270,25 @@ class Router(formatOps: FormatOps) { SingleLineBlock(slbEnd, noSyntaxNL = true) } } else penalizeNewlinesPolicy - def indentOncePolicy = - if (style.binPack.indentCallSiteOnce) { - val trigger = getIndentTrigger(leftOwner) - Policy.on(close) { - case Decision(FormatToken(LeftParenOrBracket(), _, m), s) - if isArgClauseSite(m.leftOwner) => - s.map(x => if (x.isNL) x else x.switch(trigger, false)) - } + val indentPolicy = + if (noSplitIndents.nonEmpty) { + def unindentPolicy = + if (isSingleArg) unindentAtExclude(exclude, Num(-indentLen)) + else NoPolicy + def indentOncePolicy = + if (style.binPack.indentCallSiteOnce) { + val trigger = getIndentTrigger(leftOwner) + Policy.on(close) { + case Decision(FormatToken(LeftParenOrBracket(), _, m), s) + if isArgClauseSite(m.leftOwner) => + s.map(x => if (x.isNL) x else x.switch(trigger, false)) + } + } else NoPolicy + unindentPolicy & indentOncePolicy } else NoPolicy baseNoSplit.withOptimalTokenOpt(opt).withPolicy(noSplitPolicy) - .andPolicy(unindentPolicy, !isSingleArg || noSplitIndents.isEmpty) - .andPolicy(indentOncePolicy, noSplitIndents.isEmpty) .andPolicy(scajaJsPolicy, !flags.scalaJsStyle) + .andPolicy(indentPolicy) } val nlPolicy = { @@ -1314,15 +1320,12 @@ class Router(formatOps: FormatOps) { val nlMod = if (rightIsComment && nlOnly) getMod(ft) else NewlineT(alt = if (singleLineOnly) Some(NoSplit) else None) - Seq( - noSplit, - Split(nlMod, bracketPenalty * (if (oneline) 4 else 2)) - .withIndent(indent) - .withSingleLineOpt(if (singleLineOnly) Some(close) else None) - .andPolicy(nlPolicy) - .andPolicy(penalizeNewlinesPolicy, singleLineOnly) - .andPolicyOpt(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy)), - ) + val nlSplit = Split(nlMod, bracketPenalty * (if (oneline) 4 else 2)) + .withIndent(indent) + .withSingleLineOpt(if (singleLineOnly) Some(close) else None) + .andPolicy(nlPolicy & penalizeNewlinesPolicy, singleLineOnly) + .andPolicyOpt(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy)) + Seq(noSplit, nlSplit) // Closing def site ): ReturnType case FormatToken(left, _: T.Colon, DefDefReturnTypeRight(returnType))