Skip to content

Commit

Permalink
FormatOps: getMustDangleForTrailingCommas(Token)
Browse files Browse the repository at this point in the history
Remove this method and create a reusable FormatToken at invocation site.
  • Loading branch information
Albert Meltzer authored and kitbellew committed Feb 19, 2024
1 parent a312f93 commit c7c5e81
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2852,11 +2852,6 @@ class FormatOps(
case _ => false
}

def getMustDangleForTrailingCommas(close: T)(implicit
style: ScalafmtConfig
): Boolean =
getMustDangleForTrailingCommas(tokens.justBefore(close))

def getMustDangleForTrailingCommas(getCloseFt: => FormatToken)(implicit
style: ScalafmtConfig
): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ class Router(formatOps: FormatOps) {
case FormatToken(open: T.LeftBrace, _, _)
if existsParentOfType[ImportExportStat](leftOwner) =>
val close = matching(open)
val beforeClose = tokens.justBefore(close)
val policy = SingleLineBlock(
close,
okSLC = style.importSelectors eq ImportSelectors.singleLine
)
val newlineBeforeClosingCurly = decideNewlinesOnlyBeforeClose(close)

val mustDangleForTrailingCommas = getMustDangleForTrailingCommas(close)
val mustDangleForTrailingCommas =
getMustDangleForTrailingCommas(beforeClose)
val mustUseNL = newlines != 0 &&
tokens.isRightCommentThenBreak(formatToken)
val newlinePolicy = style.importSelectors match {
Expand Down Expand Up @@ -907,6 +909,8 @@ class Router(formatOps: FormatOps) {
isParamClauseSite(leftOwner)
} =>
val close = matching(open)
val closeFormatToken = tokens(close)
val beforeClose = prev(closeFormatToken)
val tupleSite = isTuple(leftOwner)
val anyDefnSite = isParamClauseSite(leftOwner)
val defnSite = !tupleSite && anyDefnSite
Expand Down Expand Up @@ -962,7 +966,6 @@ class Router(formatOps: FormatOps) {
else
Num(style.indent.callSite)

val closeFormatToken = tokens(close)
val isBeforeOpenParen =
if (defnSite)
style.newlines.isBeforeOpenParenDefnSite
Expand All @@ -976,17 +979,15 @@ class Router(formatOps: FormatOps) {
rhsOptimalToken(closeFormatToken)

val mustDangleForTrailingCommas =
getMustDangleForTrailingCommas(prev(closeFormatToken))
getMustDangleForTrailingCommas(beforeClose)

val mustDangle = onlyConfigStyle || expirationToken.is[T.Comment] ||
mustDangleForTrailingCommas
val shouldDangle =
if (defnSite) !shouldNotDangleAtDefnSite(leftOwner.parent, false)
else style.danglingParentheses.tupleOrCallSite(tupleSite)
val wouldDangle = shouldDangle || {
val beforeClose = prev(closeFormatToken)
val wouldDangle = shouldDangle ||
beforeClose.hasBreak && beforeClose.left.is[T.Comment]
}

val newlinePolicy: Policy =
if (wouldDangle || mustDangle) {
Expand Down Expand Up @@ -1211,8 +1212,9 @@ class Router(formatOps: FormatOps) {
if (isBracket) Some(Constants.BracketPenalty) else None
val penalizeBrackets =
bracketPenalty.map(p => PenalizeAllNewlines(close, p + 3))
val beforeClose = tokens.justBefore(close)
val onlyConfigStyle = mustUseConfigStyle(formatToken) ||
getMustDangleForTrailingCommas(close)
getMustDangleForTrailingCommas(beforeClose)

val argsHeadOpt = argumentStarts.get(hash(right))
val isSingleArg = isSeqSingle(getArgs(leftOwner))
Expand Down Expand Up @@ -1274,6 +1276,7 @@ class Router(formatOps: FormatOps) {
if !style.binPack.callSite(open).isNever &&
isArgClauseSite(leftOwner) =>
val close = matching(open)
val beforeClose = tokens.justBefore(close)
val isBracket = open.is[T.LeftBracket]
val bracketPenalty = if (isBracket) Constants.BracketPenalty else 1

Expand All @@ -1287,7 +1290,8 @@ class Router(formatOps: FormatOps) {
styleMap.opensLiteralArgumentList(formatToken)
val singleLineOnly =
style.binPack.literalsSingleLine && opensLiteralArgumentList
val mustDangleForTrailingCommas = getMustDangleForTrailingCommas(close)
val mustDangleForTrailingCommas =
getMustDangleForTrailingCommas(beforeClose)

val onlyConfigStyle = !mustDangleForTrailingCommas &&
mustUseConfigStyle(formatToken, !opensLiteralArgumentList)
Expand Down

0 comments on commit c7c5e81

Please sign in to comment.