diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 16ff16082f..5adb1e331f 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -1689,6 +1689,7 @@ class FormatOps( def foldedNonEmptyNonComment( body: Tree, nlSplitFunc: Int => Split, + isKeep: Boolean, spaceIndents: Seq[Indent] = Seq.empty ): Seq[Split] = { def bheadFT = tokens.getHead(body) @@ -1748,7 +1749,7 @@ class FormatOps( } val adjustedBody = getBlockStat(body) val (spaceSplit, nlSplit) = adjustedBody match { - case t: Term.If if ifWithoutElse(t) || hasStateColumn => + case t: Term.If if isKeep || ifWithoutElse(t) || hasStateColumn => val thenBeg = tokens.getHead(t.thenp) val thenHasLB = thenBeg.left.is[T.LeftBrace] val end = if (thenHasLB) thenBeg else prevNonComment(prev(thenBeg)) @@ -1789,10 +1790,11 @@ class FormatOps( private def foldedNonComment( body: Tree, nlSplitFunc: Int => Split, + isKeep: Boolean, spaceIndents: Seq[Indent] ): Seq[Split] = if (body.tokens.isEmpty) Seq(Split(Space, 0)) - else foldedNonEmptyNonComment(body, nlSplitFunc, spaceIndents) + else foldedNonEmptyNonComment(body, nlSplitFunc, isKeep, spaceIndents) private def unfoldedSpaceNonEmptyNonComment( body: Tree, @@ -1848,10 +1850,11 @@ class FormatOps( def folded( ft: FormatToken, body: Tree, + isKeep: Boolean, spaceIndents: Seq[Indent] = Seq.empty )(nlSplitFunc: Int => Split): Seq[Split] = checkComment(ft, nlSplitFunc) { _ => - foldedNonComment(body, nlSplitFunc, spaceIndents) + foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents) } def slbOnly( @@ -1878,14 +1881,15 @@ class FormatOps( Seq(nlSplitFunc(0).forThisLine) case Newlines.classic => Option(classicNoBreakFunc).fold { - foldedNonComment(body, nlSplitFunc, spaceIndents) + foldedNonComment(body, nlSplitFunc, isKeep = true, spaceIndents) } { func => val spcSplit = func.forThisLine val nlSplit = nlSplitFunc(spcSplit.getCost(_ + 1, 0)).forThisLine Seq(spcSplit, nlSplit) } - case _ => // fold or keep without break - foldedNonComment(body, nlSplitFunc, spaceIndents) + case x => // fold or keep without break + val isKeep = x eq Newlines.keep + foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents) } } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 4a9ac1e268..a653ac0ee6 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -555,7 +555,10 @@ class Router(formatOps: FormatOps) { beforeMultiline.eq(Newlines.classic) || getSingleStatExceptEndMarker(body).isEmpty ) withSlbSplit - else CtrlBodySplits.foldedNonEmptyNonComment(body, nlSplit(ft)) + else { + val isKeep = beforeMultiline.eq(Newlines.keep) + CtrlBodySplits.foldedNonEmptyNonComment(body, nlSplit(ft), isKeep) + } } // New statement case tok @ FormatToken(_: T.Semicolon, _, StartsStatementRight(stmt)) @@ -2648,7 +2651,8 @@ class Router(formatOps: FormatOps) { case Newlines.unfold => Seq(baseSplit.withSingleLine(expire), newlineSplit(1)) - case _ => CtrlBodySplits.folded(ft, body)(newlineSplit) + case x => + CtrlBodySplits.folded(ft, body, x eq Newlines.keep)(newlineSplit) } } diff --git a/scalafmt-tests/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/src/test/resources/newlines/source_classic.stat index 44cc9a5ae3..a9051a08fe 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_classic.stat @@ -5862,23 +5862,20 @@ class Foo { } >>> class Foo { - val foo = - if (true) { - "" - } else { - "a" - } - val foo = - if (true) - "" - else - "a" + val foo = if (true) { + "" + } else { + "a" + } + val foo = if (true) + "" + else + "a" val foo = if (true) "" else "a" val foo = if (true) "aaaaa" else "bbbbb" - val foo = - if (true) "aaaaa" - else "bbbbb" + val foo = if (true) "aaaaa" + else "bbbbb" } <<< #3173 beforeMultiline = fold newlines.beforeMultiline = fold diff --git a/scalafmt-tests/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/src/test/resources/newlines/source_keep.stat index b73bf7b134..6e781781d3 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_keep.stat @@ -231,12 +231,10 @@ object a { >>> object a { val ok1 = if (a > 10) Some(a) else None - val ok2 = - if (a > 10) { Some(a) } - else { None } - val ok3 = - if (aaaa > 10000) { Some(aaaa) } - else { None } + val ok2 = if (a > 10) { Some(a) } + else { None } + val ok3 = if (aaaa > 10000) { Some(aaaa) } + else { None } } <<< 2.7 #1747: one line without else maxColumn = 60 @@ -274,23 +272,20 @@ object a { } >>> object a { - val ok1 = - if (a > 10) - Some(a) - else - None - val ok2 = - if (a > 10) { - Some(a) - } else { - None - } - val ok3 = - if (aaaa > 10000) { - Some(aaaa) - } else { - None - } + val ok1 = if (a > 10) + Some(a) + else + None + val ok2 = if (a > 10) { + Some(a) + } else { + None + } + val ok3 = if (aaaa > 10000) { + Some(aaaa) + } else { + None + } } <<< 2.9 #1747: split on else maxColumn = 60 @@ -307,20 +302,17 @@ object a { } >>> object a { - val ok1 = - if (a > 10) Some(a) - else - None - val ok2 = - if (a > 10) { Some(a) } - else { - None - } - val ok3 = - if (aaaa > 10000) { Some(aaaa) } - else { - None - } + val ok1 = if (a > 10) Some(a) + else + None + val ok2 = if (a > 10) { Some(a) } + else { + None + } + val ok3 = if (aaaa > 10000) { Some(aaaa) } + else { + None + } } <<< 2.10 #1747: split on then without else maxColumn = 60 @@ -2399,15 +2391,14 @@ val ret = (if (dailyStats.isEmpty) yestNav) / nav - 1 }) >>> -val ret = - (if (dailyStats.isEmpty) - 0 - else { - val yestStats = dailyStats.last - val yestNav = yestStats.nav - (nav - - yestNav) / nav - 1 - }) +val ret = (if (dailyStats.isEmpty) + 0 + else { + val yestStats = dailyStats.last + val yestNav = yestStats.nav + (nav - + yestNav) / nav - 1 + }) <<< 8.24 maxColumn = 80 === @@ -5324,35 +5315,29 @@ object a { } >>> object a { - def foo = { - if (a) { - a + - b - } else { - a + - b - } + def foo = if (a) { + a + + b + } else { + a + + b } - val foo = { - if (a) { - a + - b - } else if (b) { - a + - b - } else if (c) { - a + - b - } + val foo = if (a) { + a + + b + } else if (b) { + a + + b + } else if (c) { + a + + b } - var foo = { - if (a) { - a + - b - } else { - a + - b - } + var foo = if (a) { + a + + b + } else { + a + + b } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf @@ -5385,39 +5370,33 @@ object a { } >>> object a { - def foo = { - if (a) { - a + - b - } - else { - a + - b - } + def foo = if (a) { + a + + b } - val foo = { - if (a) { - a + - b - } - else if (b) { - a + - b - } - else if (c) { - a + - b - } + else { + a + + b } - var foo = { - if (a) { - a + - b - } - else { - a + - b - } + val foo = if (a) { + a + + b + } + else if (b) { + a + + b + } + else if (c) { + a + + b + } + var foo = if (a) { + a + + b + } + else { + a + + b } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks @@ -5889,23 +5868,20 @@ class Foo { } >>> class Foo { - val foo = - if (true) { - "" - } else { - "a" - } - val foo = - if (true) - "" - else - "a" + val foo = if (true) { + "" + } else { + "a" + } + val foo = if (true) + "" + else + "a" val foo = if (true) "" else "a" val foo = if (true) "aaaaa" else "bbbbb" - val foo = - if (true) "aaaaa" - else "bbbbb" + val foo = if (true) "aaaaa" + else "bbbbb" } <<< #3173 beforeMultiline = fold newlines.beforeMultiline = fold diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index 64952a9832..838adfe12d 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -2568,35 +2568,29 @@ object a { } >>> object a { - def foo = { - if (a) { - a + - b - } else { - a + - b - } + def foo = if (a) { + a + + b + } else { + a + + b } - val foo = { - if (a) { - a + - b - } else if (b) { - a + - b - } else if (c) { - a + - b - } + val foo = if (a) { + a + + b + } else if (b) { + a + + b + } else if (c) { + a + + b } - var foo = { - if (a) { - a + - b - } else { - a + - b - } + var foo = if (a) { + a + + b + } else { + a + + b } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf @@ -2629,39 +2623,33 @@ object a { } >>> object a { - def foo = { - if (a) { - a + - b - } - else { - a + - b - } + def foo = if (a) { + a + + b } - val foo = { - if (a) { - a + - b - } - else if (b) { - a + - b - } - else if (c) { - a + - b - } + else { + a + + b } - var foo = { - if (a) { - a + - b - } - else { - a + - b - } + val foo = if (a) { + a + + b + } + else if (b) { + a + + b + } + else if (c) { + a + + b + } + var foo = if (a) { + a + + b + } + else { + a + + b } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks