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: preserve break after while if keep #3049

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
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 @@ -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)