Skip to content

Commit

Permalink
FormatOps: inline opensConfigStyle{,Classic}
Browse files Browse the repository at this point in the history
  • Loading branch information
albertikm authored and kitbellew committed Feb 19, 2024
1 parent c7c5e81 commit 3c5339b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
Expand Up @@ -980,33 +980,23 @@ class FormatOps(

def mustUseConfigStyle(
ft: FormatToken,
beforeCloseFt: => FormatToken,
allowForce: => Boolean = true
)(implicit style: ScalafmtConfig): Boolean =
style.optIn.configStyleArguments && couldUseConfigStyle(ft, allowForce)
style.optIn.configStyleArguments &&
couldUseConfigStyle(ft, beforeCloseFt, allowForce)

def couldUseConfigStyle(
ft: FormatToken,
beforeCloseFt: => FormatToken,
allowForce: => Boolean = true
)(implicit style: ScalafmtConfig): Boolean =
opensConfigStyle(ft) || allowForce && forceConfigStyle(hash(ft.left))

def opensConfigStyle(
ft: => FormatToken,
whenSourceIgnored: Boolean = false
)(implicit style: ScalafmtConfig): Boolean =
if (style.newlines.sourceIgnored) whenSourceIgnored
else opensConfigStyleClassic(ft)

private def opensConfigStyleClassic(
ft: FormatToken
)(implicit style: ScalafmtConfig): Boolean = {
def opensImplicit =
(style.newlines.forceAfterImplicitParamListModifier ||
next(ft).hasBreak) && opensConfigStyleImplicitParamList(ft)
(ft.hasBreak || opensImplicit) && {
val close = matching(ft.left)
tokens(close, -1).hasBreak
}
def opensImplicit = (next(ft).hasBreak ||
style.newlines.forceAfterImplicitParamListModifier) &&
opensConfigStyleImplicitParamList(ft)
val opensConfigStyle = !style.newlines.sourceIgnored && // classic
(ft.hasBreak || opensImplicit) && beforeCloseFt.hasBreak
opensConfigStyle || allowForce && forceConfigStyle(hash(ft.left))
}

/** Works for `using` as well */
Expand Down
Expand Up @@ -940,7 +940,7 @@ class Router(formatOps: FormatOps) {
val bracketCoef = if (isBracket) Constants.BracketPenalty else 1

val rightIsComment = right.is[T.Comment]
val onlyConfigStyle = mustUseConfigStyle(formatToken)
val onlyConfigStyle = mustUseConfigStyle(formatToken, beforeClose)

val sourceIgnored = style.newlines.sourceIgnored
val (onlyArgument, isSingleEnclosedArgument) =
Expand Down Expand Up @@ -1213,7 +1213,7 @@ class Router(formatOps: FormatOps) {
val penalizeBrackets =
bracketPenalty.map(p => PenalizeAllNewlines(close, p + 3))
val beforeClose = tokens.justBefore(close)
val onlyConfigStyle = mustUseConfigStyle(formatToken) ||
val onlyConfigStyle = mustUseConfigStyle(formatToken, beforeClose) ||
getMustDangleForTrailingCommas(beforeClose)

val argsHeadOpt = argumentStarts.get(hash(right))
Expand Down Expand Up @@ -1294,7 +1294,7 @@ class Router(formatOps: FormatOps) {
getMustDangleForTrailingCommas(beforeClose)

val onlyConfigStyle = !mustDangleForTrailingCommas &&
mustUseConfigStyle(formatToken, !opensLiteralArgumentList)
mustUseConfigStyle(ft, beforeClose, !opensLiteralArgumentList)
val rightIsComment = right.is[T.Comment]
val nlOnly = mustDangleForTrailingCommas || onlyConfigStyle ||
style.newlines.keepBreak(newlines) ||
Expand Down Expand Up @@ -2198,8 +2198,9 @@ class Router(formatOps: FormatOps) {
)

case FormatToken(open: T.LeftParen, right, _) =>
val isConfig = couldUseConfigStyle(formatToken)
val close = matching(open)
val beforeClose = tokens.justBefore(close)
val isConfig = couldUseConfigStyle(formatToken, beforeClose)
val enclosed = leftOwner match {
case t: Member.ArgClause if t.values.lengthCompare(1) > 0 => None
case t => findEnclosedBetweenParens(open, close, t)
Expand All @@ -2216,9 +2217,8 @@ class Router(formatOps: FormatOps) {
}
if (isInfix) Num(0)
else {
val closeFt = tokens(close, -1)
val willBreak = closeFt.left.is[T.Comment] &&
prevNonCommentSameLine(closeFt).hasBreak
val willBreak = beforeClose.left.is[T.Comment] &&
prevNonCommentSameLine(beforeClose).hasBreak
Num(if (willBreak) style.indent.main else 0)
}
}
Expand Down

0 comments on commit 3c5339b

Please sign in to comment.