From bcd80a0b9bb0a91f9277547a7933db8d7cc975d5 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 10 May 2024 11:35:39 -0700 Subject: [PATCH] FormatOps: pass close-break indicator for cfgstyle Also, for trailing commas, presume close break; the same logic is used in all other cases. --- .../scala/org/scalafmt/internal/FormatOps.scala | 15 ++++++++------- .../main/scala/org/scalafmt/internal/Router.scala | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 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 0a47321d9c..d002c7c459 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 @@ -859,14 +859,14 @@ class FormatOps( def mustUseConfigStyle( ft: FormatToken, - beforeCloseFt: => FormatToken, + breakBeforeClose: => Boolean, allowForce: Boolean = true, )(implicit style: ScalafmtConfig, cfg: Newlines.ConfigStyleElement, ): ConfigStyle = if (allowForce && mustForceConfigStyle(ft)) ConfigStyle.Forced - else if (preserveConfigStyle(ft, beforeCloseFt)) ConfigStyle.Source + else if (preserveConfigStyle(ft, breakBeforeClose)) ConfigStyle.Source else ConfigStyle.None def mustForceConfigStyle(ft: FormatToken)(implicit @@ -875,17 +875,17 @@ class FormatOps( def preserveConfigStyle( ft: FormatToken, - beforeCloseFt: => FormatToken, + breakBeforeClose: => Boolean, )(implicit style: ScalafmtConfig, cfg: Newlines.ConfigStyleElement): Boolean = - cfg.prefer && couldPreserveConfigStyle(ft, beforeCloseFt) + cfg.prefer && couldPreserveConfigStyle(ft, breakBeforeClose) - def couldPreserveConfigStyle(ft: FormatToken, beforeCloseFt: => FormatToken)( + def couldPreserveConfigStyle(ft: FormatToken, breakBeforeClose: => Boolean)( implicit style: ScalafmtConfig, ): Boolean = !style.newlines.sourceIgnored && { ft.hasBreak || (next(ft).hasBreak || style.newlines.forceAfterImplicitParamListModifier) && opensConfigStyleImplicitParamList(ft) - } && beforeCloseFt.hasBreak + } && breakBeforeClose /** Works for `using` as well */ def opensConfigStyleImplicitParamList(formatToken: FormatToken)(implicit @@ -2648,7 +2648,8 @@ class FormatOps( implicit val configStyleFlags = style.configStyleCallSite val configStyle = if (dangleForTrailingCommas) ConfigStyle.None - else mustUseConfigStyle(ftAfterOpen, ftBeforeClose, !literalArgList) + else + mustUseConfigStyle(ftAfterOpen, ftBeforeClose.hasBreak, !literalArgList) val shouldDangle = style.danglingParentheses .atCallSite(ftAfterOpen.meta.leftOwner) val scalaJsStyle = style.newlines.source == Newlines.classic && 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 08877b0983..cb8ad1bae9 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 @@ -867,11 +867,15 @@ class Router(formatOps: FormatOps) { val isBracket = open.is[T.LeftBracket] val bracketCoef = if (isBracket) Constants.BracketPenalty else 1 + val mustDangleForTrailingCommas = + getMustDangleForTrailingCommas(beforeClose) + val rightIsComment = right.is[T.Comment] implicit val configStyleFlags = if (defnSite) style.configStyleDefnSite else style.configStyleCallSite - val configStyle = mustUseConfigStyle(ft, beforeClose) - val onlyConfigStyle = ConfigStyle.None != configStyle + def closeBreak = mustDangleForTrailingCommas || beforeClose.hasBreak + val onlyConfigStyle = ConfigStyle.None != + mustUseConfigStyle(ft, closeBreak) val configStyleFlag = configStyleFlags.prefer val sourceIgnored = style.newlines.sourceIgnored @@ -902,9 +906,6 @@ class Router(formatOps: FormatOps) { defnSiteLastToken(closeFormatToken, leftOwner) else rhsOptimalToken(closeFormatToken) - val mustDangleForTrailingCommas = - getMustDangleForTrailingCommas(beforeClose) - val mustDangle = onlyConfigStyle || expirationToken.is[T.Comment] || mustDangleForTrailingCommas val shouldDangle = @@ -1112,7 +1113,7 @@ class Router(formatOps: FormatOps) { val beforeClose = tokens.justBefore(close) implicit val configStyleFlags = style.configStyleDefnSite val onlyConfigStyle = getMustDangleForTrailingCommas(beforeClose) || - ConfigStyle.None != mustUseConfigStyle(ft, beforeClose) + ConfigStyle.None != mustUseConfigStyle(ft, beforeClose.hasBreak) val argsHeadOpt = argumentStarts.get(ft.meta.idx) val isSingleArg = isSeqSingle(getArgs(leftOwner)) @@ -2026,7 +2027,7 @@ class Router(formatOps: FormatOps) { case FormatToken(open: T.LeftParen, right, _) => val close = matching(open) val beforeClose = tokens.justBefore(close) - val isConfig = couldPreserveConfigStyle(ft, beforeClose) + val isConfig = couldPreserveConfigStyle(ft, beforeClose.hasBreak) val enclosed = leftOwner match { case t: Member.ArgClause if t.values.lengthCompare(1) > 0 => None