Skip to content

Commit

Permalink
Router: refactor handling fewer braces in select
Browse files Browse the repository at this point in the history
Force break unless fewer-braces expression is enclosed.
  • Loading branch information
kitbellew committed Dec 10, 2024
1 parent 6c169cb commit a5d10f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1726,21 +1726,26 @@ class Router(formatOps: FormatOps) {
val (prevSelect, prevApply) =
findPrevSelectAndApply(thisSelect.qual, enclosed)
val afterComment = left.is[T.Comment]
val nlOnly =

// includes other optional-braces-like trees
def checkFewerBraces(tree: Tree) =
(tree match {
case p: Term.Apply => isFewerBraces(p)
case p: Term.MatchLike => getHead(p.casesBlock).leftOwner ne
p.casesBlock
case p: Term.NewAnonymous => getHeadOpt(p.templ.body)
.exists(_.left.is[T.Colon])
case _ => false
}) && !isEnclosedInMatching(tree)
val fewerBracesLike = checkFewerBraces(thisSelect.qual)
val indentFewerBraces = style.getFewerBraces()

val nlOnly = fewerBracesLike ||
style.newlines.sourceIgnored && afterComment && hasBreak() ||
prevApply.exists(x => getHead(x.argClause).left.is[T.Colon])
prevApply.exists(x => getHead(x.argClause).left.is[T.Colon])
val expire = getLastExceptParen(expireTree)
val indentLen = style.indent.main

def checkFewerBraces(tree: Tree) = tree match {
case p: Term.Apply => isFewerBraces(p)
case p: Term.MatchLike => getHead(p.casesBlock).meta.leftOwner ne
p.casesBlock
case p: Term.NewAnonymous => getHeadOpt(p.templ.body)
.exists(_.left.is[T.Colon])
case _ => false
}

val nextDotOpt = nextSelect.map(ns => tokenBefore(ns.nameFt))
val beforeNextDotOpt = nextDotOpt.map(prev)
val nextDotIfSig = nextSelect
Expand Down Expand Up @@ -1781,12 +1786,12 @@ class Router(formatOps: FormatOps) {
def getNlMod = {
val endSelect = nextDotIfSig.fold {
nextSelect.fold {
val ko = style.getFewerBraces() ==
Indents.FewerBraces.always && checkFewerBraces(expireTree)
val ko = indentFewerBraces == Indents.FewerBraces.always &&
checkFewerBraces(expireTree)
if (ko) None else Some(expire)
}(ns => Some(getLastNonTrivial(ns.qual)))
} { nd =>
val ok = style.getFewerBraces() == Indents.FewerBraces.never
val ok = indentFewerBraces == Indents.FewerBraces.never
if (ok) Some(nd) else None
}
val altIndent = endSelect.map(Indent(-indentLen, _, After))
Expand Down Expand Up @@ -1954,8 +1959,8 @@ class Router(formatOps: FormatOps) {
}

// trigger indent only on the first newline
val fbIndent = style.getFewerBraces() != Indents.FewerBraces.never
val noIndent = !fbIndent && checkFewerBraces(thisSelect.qual)
val fbIndent = indentFewerBraces != Indents.FewerBraces.never
val noIndent = !fbIndent && fewerBracesLike
val nlIndent =
if (noIndent) Indent.Empty else Indent(indentLen, expire, After)
val spcPolicy: Policy = delayedBreakPolicyOpt
Expand All @@ -1966,7 +1971,7 @@ class Router(formatOps: FormatOps) {
baseSplits.map(_.withIndent(nlIndent).andFirstPolicy(nlPolicy))
else {
val spcIndent = nextDotIfSig.fold {
val ok = style.getFewerBraces() == Indents.FewerBraces.always &&
val ok = indentFewerBraces == Indents.FewerBraces.always &&
nextSelect.isEmpty && checkFewerBraces(expireTree)
if (ok) nlIndent else Indent.empty
}(x => if (fbIndent) Indent(indentLen, x, Before) else Indent.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ abstract class CommunityScala3Suite(name: String)

class CommunityScala3_2Suite extends CommunityScala3Suite("scala-3.2") {

override protected def totalStatesVisited: Option[Int] = Some(39222286)
override protected def totalStatesVisited: Option[Int] = Some(39222208)

override protected def builds = Seq(getBuild("3.2.2", dialects.Scala32, 791))

}

class CommunityScala3_3Suite extends CommunityScala3Suite("scala-3.3") {

override protected def totalStatesVisited: Option[Int] = Some(42407030)
override protected def totalStatesVisited: Option[Int] = Some(42406952)

override protected def builds = Seq(getBuild("3.3.3", dialects.Scala33, 861))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1187430, "total explored")
assertEquals(explored, 1187374, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit a5d10f0

Please sign in to comment.