Skip to content

Commit

Permalink
Router: handle then and do similar to else/yield
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 10, 2022
1 parent e5e2b1a commit 2c3a944
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,15 @@ class Router(formatOps: FormatOps) {
.withSingleLineNoOptimal(expire, exclude, noSyntaxNL = noSyntaxNL),
Split(Newline, 1)
)
case ft @ FormatToken(_, _: T.KwThen | _: T.KwDo, _) =>
if (style.newlines.sourceIgnored || newlines == 0)
Seq(
Split(Space, 0)
.withOptimalToken(nextNonCommentSameLine(next(ft)).left),
Split(Newline, 1)
)
else
Seq(Split(Newline, 0))
// Last else branch
case FormatToken(_: T.KwElse, _, _) if (leftOwner match {
case t: Term.If => !t.elsep.is[Term.If]
Expand Down
66 changes: 40 additions & 26 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3115,14 +3115,14 @@ class C:
else 4
>>>
class C:
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
<<< #3033 if-then !danglingParentheses
maxColumn = 15
Expand All @@ -3143,14 +3143,14 @@ class C:
else 4
>>>
class C:
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
<<< #3034 for-yield danglingParentheses
danglingParentheses.ctrlSite = true
Expand Down Expand Up @@ -3199,10 +3199,13 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 for-do !danglingParentheses
danglingParentheses.ctrlSite = false
===
Expand All @@ -3220,10 +3223,13 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 while-do danglingParentheses
danglingParentheses.ctrlSite = true
===
Expand All @@ -3244,11 +3250,15 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
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 do matrix(x)(0) += matrix(x - 1)(0)
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 do matrix(x)(0) += matrix(x - 1)(0)
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
do matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 while-do !danglingParentheses
danglingParentheses.ctrlSite = false
===
Expand All @@ -3269,8 +3279,12 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
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 do matrix(x)(0) += matrix(x - 1)(0)
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 do matrix(x)(0) += matrix(x - 1)(0)
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
do matrix(x)(0) += matrix(x - 1)(0)
24 changes: 12 additions & 12 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2948,14 +2948,14 @@ class C:
else 4
>>>
class C:
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
<<< #3033 if-then short !danglingParentheses
maxColumn = 13
Expand All @@ -2976,14 +2976,14 @@ class C:
else 4
>>>
class C:
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
if true then
3
if true
then 3
else 4
<<< #3034 for-yield danglingParentheses
maxColumn = 50
Expand Down
54 changes: 36 additions & 18 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3081,12 +3081,15 @@ class C:
else 4
>>>
class C:
if true then 3
if true
then 3
else 4
if true then
if true
then
3
else 4
if true then
if true
then
3
else 4
<<< #3033 if-then !danglingParentheses
Expand All @@ -3107,12 +3110,15 @@ class C:
else 4
>>>
class C:
if true then 3
if true
then 3
else 4
if true then
if true
then
3
else 4
if true then
if true
then
3
else 4
<<< #3034 for-yield danglingParentheses
Expand Down Expand Up @@ -3162,10 +3168,12 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do
matrix(x)(0) += matrix(x - 1)(0)
for x <- range do
for x <- range
do
matrix(x)(0) += matrix(x - 1)(0)
for
x <- range
Expand All @@ -3188,10 +3196,12 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range do matrix(x)(0) += matrix(x - 1)(0)
for x <- range
do matrix(x)(0) += matrix(x - 1)(0)
for x <- range do
matrix(x)(0) += matrix(x - 1)(0)
for x <- range do
for x <- range
do
matrix(x)(0) += matrix(x - 1)(0)
for
x <- range
Expand All @@ -3217,13 +3227,17 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
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 do
matrix(x)(0) += matrix(x - 1)(0)
while x < range do matrix(x)(0) += matrix(x - 1)(0)
while x < range do
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 do
while x < range
do
matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 while-do !danglingParentheses
danglingParentheses.ctrlSite = false
Expand All @@ -3245,11 +3259,15 @@ object a:
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
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 do
matrix(x)(0) += matrix(x - 1)(0)
while x < range do matrix(x)(0) += matrix(x - 1)(0)
while x < range do
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 do
while x < range
do
matrix(x)(0) += matrix(x - 1)(0)

0 comments on commit 2c3a944

Please sign in to comment.