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: refactor around lambda variables #4461

Merged
merged 1 commit into from
Oct 20, 2024
Merged
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 @@ -287,16 +287,15 @@ class Router(formatOps: FormatOps) {
else Some(false)
(arrow, 0, nlOnly)
case (t: Term.FunctionTerm) :: Nil =>
val arrow = getFuncArrow(lastLambda(t))
val expire = arrow.getOrElse(getLast(t))
val arrow = getFuncArrow(lastLambda(t)).getOrElse(getLast(t))
val nlOnly =
if (style.newlines.alwaysBeforeCurlyLambdaParams) Some(true)
else if (
style.newlines.beforeCurlyLambdaParams eq
Newlines.BeforeCurlyLambdaParams.multiline
) None
else Some(false)
(expire, 0, nlOnly)
(arrow, 0, nlOnly)
case (t: Term.PartialFunction) :: Nil => getLambdaInfo(t.cases)
case (t: Term.CasesBlock) :: Nil if (t.parent match {
case Some(t: Term.Match)
Expand All @@ -308,7 +307,7 @@ class Router(formatOps: FormatOps) {
getLambdaInfo(t.stats)
case _ => getLambdaNone
}
val (lambdaExpire, lambdaIndent, lambdaNLOnly) = leftOwner match {
val (lambdaArrow, lambdaIndent, lambdaNLOnly) = leftOwner match {
case t: Template.Body => t.selfOpt.fold {
if (t.parent.parent.isOpt[Term.NewAnonymous])
getLambdaInfo(t.stats)
Expand All @@ -326,6 +325,10 @@ class Router(formatOps: FormatOps) {
case t: Term.Block => getLambdaInfo(t.stats)
case t => getLambdaInfo(t :: Nil)
}
val noLambdaSplit = style.newlines.keepBreak(newlines) ||
lambdaArrow == null || !lambdaNLOnly.contains(false)
val lambdaExpire =
if (noLambdaSplit) null else getOptimalTokenFor(lambdaArrow)

def getSingleLinePolicy = {
val needBreak = closeFT.right match {
Expand Down Expand Up @@ -378,11 +381,11 @@ class Router(formatOps: FormatOps) {

// do not fold top-level blocks
if (isTopLevelBlock) None
else if (lambdaExpire != null) getSingleLineLambdaDecisionOpt
else if (lambdaArrow != null) getSingleLineLambdaDecisionOpt
else Some(false)
// old behaviour
case _ =>
if (lambdaExpire == null) getClassicSingleLineDecisionOpt
if (lambdaArrow == null) getClassicSingleLineDecisionOpt
else getSingleLineLambdaDecisionOpt
}

Expand Down Expand Up @@ -422,15 +425,12 @@ class Router(formatOps: FormatOps) {
.withIndent(style.indent.main, close, Before)

// must be after nlSplit
val noLambdaSplit = style.newlines.keepBreak(newlines) ||
lambdaExpire == null || !lambdaNLOnly.contains(false)
val lambdaSplit =
if (noLambdaSplit) Split.ignored
else {
val arrowOptimal = getOptimalTokenFor(lambdaExpire)
val policy = singleLineSplitOpt match {
case Some(Left(slbSplit)) => Policy.after(arrowOptimal, "FNARR") {
case Decision(FormatToken(`arrowOptimal`, _, _), ss) =>
case Some(Left(slbSplit)) => Policy.after(lambdaExpire, "FNARR") {
case Decision(FormatToken(`lambdaExpire`, _, _), ss) =>
val nlPolicy = newlineBeforeClosingCurly
var hadNoSplit = false
val nlSplits = ss.flatMap { s =>
Expand All @@ -441,9 +441,9 @@ class Router(formatOps: FormatOps) {
else nlSplits
}
case _ => newlineBeforeClosingCurly &
decideNewlinesOnlyAfterToken(arrowOptimal)
decideNewlinesOnlyAfterToken(lambdaExpire)
}
Split(noSplitMod, 0).withSingleLine(arrowOptimal).andPolicy(policy)
Split(noSplitMod, 0).withSingleLine(lambdaExpire).andPolicy(policy)
.withIndent(lambdaIndent, close, Before)
}

Expand Down
Loading