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: move to TreeOps getting lambda penalties #4600

Merged
merged 1 commit into from
Nov 25, 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 @@ -452,34 +452,10 @@ class Router(formatOps: FormatOps) {
val (nlCost, nlArrowPenalty) =
if (!nl.isNL) (0, 0)
else if (slbMod eq noSplitMod) (1 + nlPenalty, nlPenalty)
else {
def argClausePenalty(t: Term.ArgClause)(isFunc: Term => Boolean) =
t.values match {
case arg :: Nil =>
if (isFunc(arg)) Some((nestedApplies(t), 2))
else t.parent match {
case Some(p: Term.Apply) =>
Some((nestedApplies(p), treeDepth(p.fun)))
case _ => None
}
case _ => None
}
leftOwner match {
case t: Term.ArgClause =>
argClausePenalty(t)(_.is[Term.FunctionTerm])
case t: Term => t.parent match {
case Some(p: Term.ArgClause) => argClausePenalty(p) {
case Term.Block((_: Term.FunctionTerm) :: Nil) =>
getHead(t) eq ft
case _ => false
}
case _ => None
}
case _ => None
else getLambdaPenaltiesOnLeftBraceOnLeft(ft)
.fold((1, nlPenalty)) { case (sharedPenalty, herePenalty) =>
(sharedPenalty + herePenalty, sharedPenalty)
}
}.fold((1, nlPenalty)) { case (sharedPenalty, herePenalty) =>
(sharedPenalty + herePenalty, sharedPenalty)
}
val newlineBeforeClosingCurly = decideNewlinesOnlyBeforeClose(close)
val nlPolicy = lambdaNLPolicy ==> newlineBeforeClosingCurly
val nlSplit = Split(nl, nlCost, policy = nlPolicy)
Expand Down Expand Up @@ -1949,17 +1925,16 @@ class Router(formatOps: FormatOps) {
}
val exclude = insideBracesBlock(ft, end, parensToo = true)
.excludeCloseDelim
val bracesToParens = ftAfterRight.right.is[T.OpenDelim] &&
(ftAfterRight.rightOwner match {
case t: Member.ArgClause => t.values.lengthCompare(1) == 0
case t: Term.Block => t.parent.is[Member.ArgClause]
case _ => false
}) && {
implicit val ft: FT = next(ftAfterRight)
val rb = matchingRight(ftAfterRight)
getBracesToParensMod(rb, Space, isWithinBraces = true)._1 ne
Space
}
val ftNextAfterRight = next(ftAfterRight)
val singleArg =
if (!ftAfterRight.right.is[T.OpenDelim]) None
else getSingleArgOnLeftBraceOnLeft(ftNextAfterRight).map(_._2)
val bracesToParens = singleArg.isDefined && {
implicit val ft: FT = ftNextAfterRight
val rb = matchingRight(ftAfterRight)
getBracesToParensMod(rb, Space, isWithinBraces = true)._1 ne
Space
}
val noSplit = Split(modSpace, 0)
.withSingleLine(end, exclude = exclude)
Seq(noSplit, nlSplitBase(if (bracesToParens) 0 else 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,42 @@ object TreeOps {
math.max(res, treeDepth(t))
}

def getSingleArgOnLeftBraceOnLeft(ft: FT)(implicit
ftoks: FormatTokens,
): Option[(Term.ArgClause, Stat)] = ft.leftOwner match {
case ac: Term.ArgClause => (ac.values match {
case (t: Term.Block) :: Nil if ftoks.getHead(t) eq ft =>
getBlockSingleStat(t)
case t :: Nil => Some(t)
case _ => None
}).map(x => (ac, x))
case t: Term => t.parent match {
case Some(ac: Term.ArgClause) if ac.values.lengthCompare(1) == 0 =>
(t match {
case t: Term.Block if ftoks.getHead(ac) eq ft =>
getBlockSingleStat(t)
case _ => Some(t)
}).map(x => (ac, x))
case _ => None
}
case _ => None
}

def getSingleArgLambdaPenalties(
ac: Term.ArgClause,
arg: Stat,
): Option[(Int, Int)] =
if (arg.is[Term.FunctionTerm]) Some((nestedApplies(ac), 2))
else ac.parent match {
case Some(p: Term.Apply) => Some((nestedApplies(p), treeDepth(p.fun)))
case _ => None
}

def getLambdaPenaltiesOnLeftBraceOnLeft(ft: FormatToken)(implicit
ftoks: FormatTokens,
): Option[(Int, Int)] = getSingleArgOnLeftBraceOnLeft(ft)
.flatMap((getSingleArgLambdaPenalties _).tupled)

final def canBreakAfterFuncArrow(func: Term.FunctionTerm)(implicit
ftoks: FormatTokens,
style: ScalafmtConfig,
Expand Down