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

Router: inline braces-to-parens policy #4500

Merged
merged 1 commit into from
Nov 3, 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 @@ -2977,10 +2977,10 @@ class FormatOps(
@inline
def indentedPackage(pkg: Pkg): Boolean = indentedPackage(pkg.body)

def getBracesToParensModAndPolicy(rb: FormatToken, mod: Modification)(implicit
def getBracesToParensMod(rb: FormatToken, mod: Modification)(implicit
style: ScalafmtConfig,
ft: FormatToken,
): (Modification, Policy) = {
): Modification = {
val isWithinBraces = ft.left.is[T.LeftBrace]
val ok = initStyle.rewrite.bracesToParensForOneLineApply &&
style.rewrite.trailingCommas.isOptional && (mod eq Space) &&
Expand All @@ -2990,16 +2990,7 @@ class FormatOps(
// check if all of them can be converted to parens
insideBracesBlock(if (isWithinBraces) ft else next(ft), rb.left).ranges
.forall(x => couldHaveBracesConvertedToParens(x.lt.leftOwner))
if (ok) {
val end = Policy.End < rb.left
val beforeClose = prev(rb)
val policy = Policy ? isWithinBraces &&
end ==> Policy.on(rb.left, "BracesToParens") {
case Decision(`beforeClose`, ss) => ss
.flatMap(s => if (s.isNL) None else Some(s.withMod(NoSplit)))
}
SpaceOrNoSplit(end) -> policy
} else (mod, Policy.noPolicy)
if (ok) SpaceOrNoSplit(Policy.End < rb.left) else mod
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ class Router(formatOps: FormatOps) {
}

val noSplitMod = xmlSpace(leftOwner)
val (slbMod, slbParensPolicy) =
if (singleLineDecisionOpt.isEmpty) (noSplitMod, NoPolicy)
else getBracesToParensModAndPolicy(closeFT, noSplitMod)
val slbMod =
if (singleLineDecisionOpt.isEmpty) noSplitMod
else getBracesToParensMod(closeFT, noSplitMod)
val singleLineSplitOpt = singleLineDecisionOpt.map { sld =>
val sldPolicy = getSingleLinePolicy
val expire = leftOwner.parent match {
Expand All @@ -403,6 +403,13 @@ class Router(formatOps: FormatOps) {
case _ => endOfSingleLineBlock(closeFT)
}
// copy logic from `( ...`, binpack=never, defining `slbSplit`
val slbParensPolicy = Policy ? (slbMod eq noSplitMod) || {
val beforeClose = prev(closeFT)
Policy.End < close ==> Policy.on(close, "BracesToParens") {
case Decision(`beforeClose`, ss) => ss
.flatMap(s => if (s.isNL) None else Some(s.withMod(NoSplit)))
}
}
Split(slbMod, 0).withSingleLine(expire, noSyntaxNL = true)
.andPolicy(sldPolicy & slbParensPolicy)
}
Expand All @@ -425,8 +432,8 @@ class Router(formatOps: FormatOps) {
else {
val nlPolicy = newlineBeforeClosingCurly
val policy = singleLineSplitOpt match {
case Some(slbSplit) if slbParensPolicy.isEmpty =>
Policy.after(lambdaExpire, "FNARR") {
case Some(slbSplit) if slbMod eq noSplitMod =>
Policy.after(lambdaExpire, s"FNARR($slbSplit)") {
case Decision(FormatToken(`lambdaExpire`, _, _), ss) =>
var hadNoSplit = false
val nlSplits = ss.flatMap { s =>
Expand All @@ -444,7 +451,7 @@ class Router(formatOps: FormatOps) {

val singleLineSplit = singleLineSplitOpt match {
case Some(slbSplit)
if slbParensPolicy.nonEmpty || lambdaSplit.isIgnored => slbSplit
if (slbMod ne noSplitMod) || lambdaSplit.isIgnored => slbSplit
case _ => Split.ignored
}
val splits = Seq(singleLineSplit, lambdaSplit, nlSplit)
Expand Down Expand Up @@ -1514,11 +1521,8 @@ class Router(formatOps: FormatOps) {
.exists(x => isTokenLastOrAfter(x.left, roPos))
}
} =>
val rb = matching(lb)
val (mod, toParensPolicy) = getBracesToParensModAndPolicy(rb, Space)
val policy = Policy
.RelayOnSplit { case (s, _) => s.isNL }(toParensPolicy, NoPolicy)
Seq(Split(mod, 0, policy = policy))
val mod = getBracesToParensMod(matching(lb), Space)
Seq(Split(mod, 0))

// Delim
case FormatToken(left, _: T.Comma, _)
Expand Down
Loading