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 8, 2022
1 parent 410ac42 commit b58db35
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 52 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
60 changes: 36 additions & 24 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 @@ -3197,9 +3197,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 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 @@ -3215,9 +3218,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 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 @@ -3233,9 +3239,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)
<<< #3034 while-do !danglingParentheses
danglingParentheses.ctrlSite = false
===
Expand All @@ -3251,6 +3260,9 @@ 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)
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
48 changes: 32 additions & 16 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 @@ -3160,8 +3166,10 @@ 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
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
Expand All @@ -3182,8 +3190,10 @@ 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
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
Expand All @@ -3204,10 +3214,13 @@ 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
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 @@ -3224,8 +3237,11 @@ 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
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 b58db35

Please sign in to comment.