Skip to content

Commit

Permalink
Router: keep newline after if if source=keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 27, 2022
1 parent 7068db8 commit fdf74f2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2476,18 +2476,19 @@ class FormatOps(
@inline private def seqLast(seq: Seq[Tree]): Option[T] =
if (isSeqMulti(seq)) treeLast(seq.last) else None

def indentAndBreakBeforeCtrl[A](tree: Tree, fsplit: => Split)(implicit
def indentAndBreakBeforeCtrl[A](tree: Tree, split: Split)(implicit
style: ScalafmtConfig,
classifier: Classifier[T, A]
): Option[Split] =
if (!style.dialect.allowSignificantIndentation) None
else if (tree.is[Term.Block] && isEnclosedInMatching(tree)) None
if (
!style.dialect.allowSignificantIndentation ||
tree.is[Term.Block] && !split.isNL && isEnclosedInMatching(tree)
) None
else {
val kw = tokenAfter(tree).right
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,15 +1952,19 @@ class Router(formatOps: FormatOps) {
if elses.contains(r) =>
d.onlyNewlinesWithFallback(Split(Newline, 0))
}
val spaceMod = Space(style.spaces.isSpaceAfterKeyword(right))
val slb = Split(spaceMod, 0).withSingleLine(expire, killOnFail = true)
val mlSplitBase = Split(spaceMod, 1).withPolicy(breakOnlyBeforeElse)
val mod =
if (style.newlines.keepBreak(newlines)) Newline
else Space(style.spaces.isSpaceAfterKeyword(right))
val slb =
Split(mod.isNewline, 0)(mod).withSingleLine(expire, killOnFail = true)
val mlSplitBase = Split(mod, if (slb.isIgnored) 0 else 1)
.withPolicy(breakOnlyBeforeElse)
val mlSplitOpt = OptionalBraces
.indentAndBreakBeforeCtrl[T.KwThen](owner.cond, mlSplitBase)
Seq(slb, mlSplitOpt.getOrElse(mlSplitBase))
case FormatToken(_: T.KwWhile | _: T.KwFor, right, _) =>
def spaceMod = Space(style.spaces.isSpaceAfterKeyword(right))
def splitBase = {
val splitBase = {
val onlyNL = style.newlines.keepBreak(newlines)
Split(if (onlyNL) Newline else spaceMod, 0)
}
Expand Down
17 changes: 12 additions & 5 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,8 @@ if
exitCode

>>>
if (a1 && a2) == cond1 then
if
(a1 && a2) == cond1 then
val exitCode = 1
exitCode
<<< while indentation danglingParentheses false
Expand Down Expand Up @@ -3055,7 +3056,9 @@ object a {
}
>>>
object a {
if ({ !Yield(2); false }) then {
if
({ !Yield(2); false })
then {
!Yield(3)
} else {
!Yield(4)
Expand All @@ -3082,7 +3085,9 @@ object a {
}
>>>
object a {
while ({ !Yield(2); false }) do {
while
({ !Yield(2); false })
do {
!Yield(3)
}
}
Expand Down Expand Up @@ -3111,7 +3116,8 @@ class C:
then
3
else 4
if true
if
true
then
3
else 4
Expand Down Expand Up @@ -3140,7 +3146,8 @@ class C:
then
3
else 4
if true
if
true
then
3
else 4
Expand Down

0 comments on commit fdf74f2

Please sign in to comment.