Skip to content

Commit

Permalink
Router: for/infix before select like fewer-braces
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 10, 2024
1 parent 68c25bb commit 97f3f3a
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,17 @@ class FormatOps(
enclosed: Boolean = true,
): Option[SelectLike] = findPrevSelectAndApply(tree.qual, enclosed)._1

@tailrec
final def findFirstSelect(
tree: Tree,
enclosed: Boolean,
select: Option[SelectLike] = None,
): Option[SelectLike] = findPrevSelectAndApply(tree, enclosed) match {
case (x @ Some(prevSelect), _) =>
findFirstSelect(prevSelect.qual, enclosed, x)
case _ => select
}

@tailrec
private def findLastApplyAndNextSelectEnclosed(
tree: Tree,
Expand Down Expand Up @@ -2161,7 +2172,9 @@ class FormatOps(
case x: Term.Block => getBlockWithNonSingleTermStat(x)
case x: Tree.CasesBlock => Some(x)
case _ => None
}).flatMap(x => WithStats(nft, x.stats.headOption, b, Some(t)))
}).fold(getMaybeFewerBracesSelectSplits(b))(x =>
WithStats(nft, x.stats.headOption, b, Some(t)),
)
case _ => BlockImpl.create(nft)
}
}
Expand Down Expand Up @@ -2350,6 +2363,40 @@ class FormatOps(
getSplits(tree, forceNL, danglingKeyword)
}

private def getMaybeFewerBracesSelectSplits(body: Tree)(implicit
ft: FT,
style: ScalafmtConfig,
) = findFirstSelect(body, enclosed = true).filter(_.qual match {
case q @ (_: Term.ForClause | _: Term.ApplyInfix |
_: Term.SelectPostfix) => !isEnclosedInMatching(q)
case _ => false
}).map { selectLike =>
new OptionalBracesRegion {
def splits: Option[Seq[Split]] = {
val noFbIndent = style.getFewerBraces() == Indents.FewerBraces.never
val indentLen =
if (noFbIndent) style.indent.getSignificant
else style.indent.main + style.indent.getSignificant
val dot = prev(prevNonCommentBefore(selectLike.nameFt))
val beforeDot = prevNonCommentSameLine(dot)
val policy = Policy.End <= beforeDot ==>
Policy.onRight(dot, "NL-NOIND-SELECT") {
case Decision(`beforeDot`, ss) => ss.flatMap { s =>
if ((dot eq beforeDot) && !s.isNL) None
else Some(s.deActivateFor(SplitTag.SelectChainFirstNL))
}
case Decision(_, ss) => ss
.map(_.deActivateFor(SplitTag.SelectChainFirstNL))
}
val nlSplit = Split(Newline2x(ft), 1, policy = policy)
.withIndent(indentLen, beforeDot, ExpiresOn.After)
Some(Seq(nlSplit))
}
def rightBrace: Option[FT] = treeLast(body)
def owner: Option[Tree] = body.parent
}
}

private class WithStats private (
nft: FT,
body: Tree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,8 @@ class Router(formatOps: FormatOps) {
p.casesBlock
case p: Term.NewAnonymous => getHeadOpt(p.templ.body)
.exists(_.left.is[T.Colon])
case _: Term.ForClause | _: Term.ApplyInfix |
_: Term.SelectPostfix => true
case _ => false
}) && !isEnclosedInMatching(tree)
val fewerBracesLike = checkFewerBraces(thisSelect.qual)
Expand Down
Loading

0 comments on commit 97f3f3a

Please sign in to comment.