Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatOps: pass close-break indicator for cfgstyle #3970

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
.tupleOrCallSite(isTuple(ftAfterOpen.meta.leftOwner))
val scalaJsStyle = style.newlines.source == Newlines.classic &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,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 @@ -896,9 +900,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 @@ -1106,7 +1107,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(hash(right))
val isSingleArg = isSeqSingle(getArgs(leftOwner))
Expand Down Expand Up @@ -2022,7 +2023,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
Loading