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: raise NL cost for binpacked ctrl body #4048

Merged
merged 2 commits into from
Jun 14, 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 @@ -1455,11 +1455,13 @@ class FormatOps(
val expire = nextNonCommentSameLine(blastFT).left
def penalize(penalty: Int) = Policy ? (penalty > 0) &&
new PolicyOps.PenalizeAllNewlines(Policy.End == blast, penalty)
def getNlSplit(penalty: Int)(implicit fileLine: FileLine): Split =
nlSplitFunc(1).andPolicy(penalize(penalty)).forThisLine(nextLine)
def getSplits(spaceSplit: Split) = (
def getNlSplit(penalty: Int, nlCost: Int = 1)(implicit
fileLine: FileLine,
): Split = nlSplitFunc(nlCost).andPolicy(penalize(penalty))
.forThisLine(nextLine)
def getSplits(spaceSplit: Split, nlCost: Int = 1) = (
spaceSplit.withIndents(spaceIndents),
getNlSplit(1)(nextLine(spaceSplit.fileLine)),
getNlSplit(1, nlCost)(nextLine(spaceSplit.fileLine)),
)
def getSlb(end: T, excl: TokenRanges)(implicit fileLine: FileLine) =
SingleLineBlock(end, exclude = excl, noSyntaxNL = true)
Expand All @@ -1476,9 +1478,9 @@ class FormatOps(
val spacePolicy = policy | penalize(penalty)
Split(Space, 0).withPolicy(spacePolicy).withOptimalToken(blast)
}
def getPolicySplits(penalty: Int, policy: Policy)(implicit
fileLine: FileLine,
) = getSplits(getSpaceSplit(penalty, policy))
def getPolicySplits(penalty: Int, policy: Policy, nlCost: Int = 1)(
implicit fileLine: FileLine,
) = getSplits(getSpaceSplit(penalty, policy), nlCost)
def getSlbSplits(
exclude: TokenRanges = TokenRanges.empty,
policy: Policy = Policy.NoPolicy,
Expand Down Expand Up @@ -1526,7 +1528,13 @@ class FormatOps(
else getSplits(getSlbSplit(getLastToken(lia.op)))
case _ =>
val callPolicy = CallSite.getFoldedPolicy(body)
getPolicySplits(if (callPolicy.nonEmpty) 0 else 1, callPolicy)
val nlCost = body match {
case Member.Apply(_, ac)
if styleMap.at(getHead(ac)).binPack.callSiteFor(ac).isOneline =>
3
case _ => 1
}
getPolicySplits(if (callPolicy.nonEmpty) 0 else 1, callPolicy, nlCost)
}

Seq(spaceSplit, nlSplit)
Expand Down
53 changes: 53 additions & 0 deletions scalafmt-tests/src/test/resources/scalajs/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1153,3 +1153,56 @@ object a {
global.NewFromConstructor(ctor, argss.head: _*))(
Apply(_, _))
}
<<< don't break after equals if bp, overflow
preset = default
maxColumn = 85
newlines.source = keep
binPack.preset = oneline
danglingParentheses.preset = false
newlines.configStyleCallSite.prefer = true
newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
===
object a {
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))(
sym, index, formalArgsRegistry, param, static, captures = Nil)
}
>>>
object a {
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(prevArgsCount =>
varDefs.take(prevArgsCount).toList.map(_.ref))(
sym, index, formalArgsRegistry, param, static, captures = Nil)
}
<<< don't break after equals if bp, !overflow
preset = default
maxColumn = 85
newlines.source = keep
binPack.preset = oneline
danglingParentheses.preset = false
newlines.configStyleCallSite.prefer = true
===
object a {
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))(
sym, index, formalArgsRegistry, param, static, captures = Nil)
}
>>>
object a {
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static,
captures = Nil)(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)(
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
val rhs = genScalaArg(prevArgsCount =>
varDefs.take(prevArgsCount).toList.map(_.ref))(
sym, index, formalArgsRegistry, param, static, captures = Nil)
}
Loading