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 single-line block logic #4053

Merged
merged 1 commit into from
Jun 16, 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 @@ -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
Loading