Skip to content

Commit

Permalink
Router: preserve break after while if keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 10, 2022
1 parent b9d63ee commit 4a42bd8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2484,7 +2484,7 @@ class FormatOps(
@inline private def seqLast(seq: Seq[Tree]): Option[T] =
if (isSeqMulti(seq)) treeLast(seq.last) else None

def indentAndBreakBeforeCtrl[A](tree: Tree, split: Split)(implicit
def indentAndBreakBeforeCtrl[A](tree: Tree, fsplit: => Split)(implicit
style: ScalafmtConfig,
classifier: Classifier[T, A]
): Option[Split] =
Expand All @@ -2495,12 +2495,13 @@ class FormatOps(
if (kw.is[A]) Some {
val indent =
style.indent.ctrlSite.getOrElse(style.indent.getSignificant)
val split = fsplit
def policy =
if (split.isNL) decideNewlinesOnlyBeforeClose(kw)
else decideNewlinesOnlyBeforeCloseOnBreak(kw)
split
.withIndent(Num(indent), kw, ExpiresOn.Before)
.andPolicy(
decideNewlinesOnlyBeforeCloseOnBreak(kw),
!style.danglingParentheses.ctrlSite
)
.andPolicy(policy, !style.danglingParentheses.ctrlSite)
}
else None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1868,12 +1868,16 @@ class Router(formatOps: FormatOps) {
.indentAndBreakBeforeCtrl[T.KwThen](owner.cond, mlSplitBase)
Seq(slb, mlSplitOpt.getOrElse(mlSplitBase))
case FormatToken(_: T.KwWhile, right, _) =>
val splitBase = Split(Space(style.spaces.isSpaceAfterKeyword(right)), 0)
def spaceMod = Space(style.spaces.isSpaceAfterKeyword(right))
def splitBase = {
val onlyNL = style.newlines.source.eq(Newlines.keep) && newlines != 0
Split(if (onlyNL) Newline else spaceMod, 0)
}
val split = (formatToken.meta.leftOwner match {
case t: Term.While =>
OptionalBraces.indentAndBreakBeforeCtrl[T.KwDo](t.expr, splitBase)
case _ => None
}).getOrElse(splitBase)
}).getOrElse(Split(spaceMod, 0))
Seq(split)
case FormatToken(_: T.KwFor, right, _) =>
Seq(Split(Space(style.spaces.isSpaceAfterKeyword(right)), 0))
Expand Down
15 changes: 10 additions & 5 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,8 @@ while
exitCode

>>>
while (a1 && a2) == cond1 do
while
(a1 && a2) == cond1 do
val exitCode = 1
exitCode
<<< if redundant parens
Expand Down Expand Up @@ -3231,12 +3232,14 @@ object a:
do matrix(x)(0) += matrix(x - 1)(0)
while x < range do
matrix(x)(0) += matrix(x - 1)(0)
while x < range
while
x < range
do matrix(x)(0) += matrix(x - 1)(0)
while x < range
do
matrix(x)(0) += matrix(x - 1)(0)
while x < range
while
x < range
do
matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 while-do !danglingParentheses
Expand All @@ -3263,11 +3266,13 @@ object a:
do matrix(x)(0) += matrix(x - 1)(0)
while x < range do
matrix(x)(0) += matrix(x - 1)(0)
while x < range
while
x < range
do matrix(x)(0) += matrix(x - 1)(0)
while x < range
do
matrix(x)(0) += matrix(x - 1)(0)
while x < range
while
x < range
do
matrix(x)(0) += matrix(x - 1)(0)

0 comments on commit 4a42bd8

Please sign in to comment.