Skip to content

Commit

Permalink
FormatOps: pass close-break indicator for cfgstyle
Browse files Browse the repository at this point in the history
Also, for trailing commas, presume close break; the same logic is used
in all other cases.
  • Loading branch information
kitbellew committed May 10, 2024
1 parent 55e479c commit bcd80a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bcd80a0

Please sign in to comment.