Skip to content

Commit

Permalink
FormatOps: separate config style: exists or forced
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 4, 2024
1 parent da42448 commit 7e9f9f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.scalafmt.internal

sealed abstract class ConfigStyle

object ConfigStyle {
case object None extends ConfigStyle
case object Source extends ConfigStyle
case object Forced extends ConfigStyle
}
Original file line number Diff line number Diff line change
Expand Up @@ -892,22 +892,25 @@ class FormatOps(
ft: FormatToken,
beforeCloseFt: => FormatToken,
allowForce: => Boolean = true,
)(implicit style: ScalafmtConfig): Boolean =
style.optIn.configStyleArguments &&
)(implicit style: ScalafmtConfig): ConfigStyle =
if (style.optIn.configStyleArguments)
couldUseConfigStyle(ft, beforeCloseFt, allowForce)
else ConfigStyle.None

def couldUseConfigStyle(
ft: FormatToken,
beforeCloseFt: => FormatToken,
allowForce: => Boolean = true,
)(implicit style: ScalafmtConfig): Boolean = {
)(implicit style: ScalafmtConfig): ConfigStyle = {
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))
if (opensConfigStyle) ConfigStyle.Source
else if (allowForce && forceConfigStyle(hash(ft.left))) ConfigStyle.Forced
else ConfigStyle.None
}

/** Works for `using` as well */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,8 @@ class Router(formatOps: FormatOps) {
val bracketCoef = if (isBracket) Constants.BracketPenalty else 1

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

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

val argsHeadOpt = argumentStarts.get(hash(right))
val isSingleArg = isSeqSingle(getArgs(leftOwner))
Expand Down Expand Up @@ -1192,10 +1193,12 @@ class Router(formatOps: FormatOps) {
val mustDangleForTrailingCommas =
getMustDangleForTrailingCommas(beforeClose)

val onlyConfigStyle = !mustDangleForTrailingCommas &&
mustUseConfigStyle(ft, beforeClose, !opensLiteralArgumentList)
val onlyConfigStyle =
if (mustDangleForTrailingCommas) ConfigStyle.None
else mustUseConfigStyle(ft, beforeClose, !opensLiteralArgumentList)
val rightIsComment = right.is[T.Comment]
val nlOnly = mustDangleForTrailingCommas || onlyConfigStyle ||
val nlOnly = mustDangleForTrailingCommas ||
onlyConfigStyle != ConfigStyle.None ||
style.newlines.keepBreak(newlines) ||
rightIsComment &&
(newlines != 0 || nextNonCommentSameLine(next(ft)).hasBreak)
Expand Down Expand Up @@ -1291,7 +1294,7 @@ class Router(formatOps: FormatOps) {
def binPackOnelinePolicyOpt =
if (needOnelinePolicy) nextCommaOnelinePolicy else Some(NoPolicy)
def bothPolicies = newlineBeforeClose & binPackOnelinePolicyOpt
if (onlyConfigStyle)
if (onlyConfigStyle != ConfigStyle.None)
if (styleMap.forcedBinPack(leftOwner)) bothPolicies
else splitOneArgOneLine(close, leftOwner) | newlineBeforeClose
else if (
Expand Down Expand Up @@ -2018,7 +2021,8 @@ class Router(formatOps: FormatOps) {
case FormatToken(open: T.LeftParen, right, _) =>
val close = matching(open)
val beforeClose = tokens.justBefore(close)
val isConfig = couldUseConfigStyle(formatToken, beforeClose)
val isConfig = ConfigStyle.None !=
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 Down

0 comments on commit 7e9f9f4

Please sign in to comment.