Skip to content

Commit

Permalink
Router: refactor single-line block logic
Browse files Browse the repository at this point in the history
The policy was almost always the same, so we simply need to propagate
the boolean decision, and add the special case of `fold` and `yield`
at the end.
  • Loading branch information
kitbellew committed Jun 16, 2024
1 parent 34f0067 commit da2e89d
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class Router(formatOps: FormatOps) {
decideNewlinesOnlyAfterToken(arrowOptimal)
}

def getSingleLineDecision = {
def getSingleLinePolicy = {
type Classifiers = Seq[Classifier[T, _]]
def classifiersByParent: Classifiers = leftOwner.parent match {
case Some(_: Term.If) => Seq(T.KwElse.classifier)
Expand All @@ -310,12 +310,12 @@ class Router(formatOps: FormatOps) {
decideNewlinesOnlyAfterClose(close)
}
def getClassicSingleLineDecisionOpt =
if (hasBreak()) None else Some(getSingleLineDecision)
if (noBreak()) Some(true) else None

def getSingleLineLambdaDecisionOpt = {
val ok = !lambdaNLOnly.contains(true) &&
getSpaceAndNewlineAfterCurlyLambda(newlines)._1
if (ok) Some(getSingleLineDecision) else None
if (ok) Some(true) else None
}

// null if skipping
Expand All @@ -342,13 +342,7 @@ class Router(formatOps: FormatOps) {
// do not fold top-level blocks
if (isTopLevelBlock) None
else if (lambdaPolicy != null) getSingleLineLambdaDecisionOpt
else Some(getSingleLineDecision match {
case NoPolicy if leftOwner.is[Term.ForYield] =>
val postClose = nextNonComment(closeFT).right
val bodySlb = SingleLineBlock(getLastToken(leftOwner))
Policy.End == postClose ==> bodySlb
case x => x
})
else Some(false)
// old behaviour
case _ =>
if (lambdaPolicy == null) getClassicSingleLineDecisionOpt
Expand All @@ -357,9 +351,18 @@ class Router(formatOps: FormatOps) {

val singleLineSplit = singleLineDecisionOpt.fold(Split.ignored) { sld =>
val expire = endOfSingleLineBlock(closeFT)
val sldPolicy = getSingleLinePolicy match {
case NoPolicy
if leftOwner.is[Term.ForYield] && !sld &&
(style.newlines.source eq Newlines.fold) =>
val postClose = nextNonComment(closeFT).right
val bodySlb = SingleLineBlock(getLastToken(leftOwner))
Policy.End == postClose ==> bodySlb
case x => x
}
Split(xmlSpace(leftOwner), 0)
.withSingleLine(expire, noSyntaxNL = true, killOnFail = true)
.andPolicy(sld)
.andPolicy(sldPolicy)
}

val splits = Seq(
Expand Down

0 comments on commit da2e89d

Please sign in to comment.