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: handle then and do similar to else/yield #3037

Merged
merged 2 commits 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 @@ -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
211 changes: 199 additions & 12 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ object a:
bar
def foo =
for
x <- y
do
x <- y do
bar
>>>
object a:
Expand Down Expand Up @@ -1459,7 +1458,7 @@ object a:
c
do foo
<<< if break before then if multiline cond, !dangle
maxColumn = 12
maxColumn = 15
indent.callSite = 4
indent.ctrlSite = 6
danglingParentheses.ctrlSite = false
Expand All @@ -1476,7 +1475,7 @@ object a:
c then
foo
<<< while break before do if multiline cond, !dangle
maxColumn = 12
maxColumn = 13
indent.callSite = 4
indent.ctrlSite = 6
danglingParentheses.ctrlSite = false
Expand Down Expand Up @@ -1896,8 +1895,7 @@ danglingParentheses.ctrlSite = false
===
if // comm
val cond1 = true
(a1 && a2) == cond1
then
(a1 && a2) == cond1 then
val exitCode = 1
exitCode

Expand All @@ -1911,8 +1909,7 @@ if // comm
danglingParentheses.ctrlSite = false
===
if
(a1 && a2) == cond1
then
(a1 && a2) == cond1 then
val exitCode = 1
exitCode

Expand All @@ -1925,8 +1922,7 @@ danglingParentheses.ctrlSite = false
===
while // comm
val cond1 = true
(a1 && a2) == cond1
do
(a1 && a2) == cond1 do
val exitCode = 1
exitCode

Expand All @@ -1940,8 +1936,7 @@ while // comm
danglingParentheses.ctrlSite = false
===
while
(a1 && a2) == cond1
do
(a1 && a2) == cond1 do
val exitCode = 1
exitCode

Expand Down Expand Up @@ -3101,3 +3096,195 @@ object a:
for (m <- 0 until 10) do
val msg = m.toString
received += msg
<<< #3033 if-then danglingParentheses
maxColumn = 15
danglingParentheses.ctrlSite = true
===
class C:
if true
then 3
else 4
if true
then
3
else 4
if
true
then
3
else 4
>>>
class C:
if true
then 3
else 4
if true
then 3
else 4
if true
then 3
else 4
<<< #3033 if-then !danglingParentheses
maxColumn = 15
danglingParentheses.ctrlSite = false
===
class C:
if true
then 3
else 4
if true
then
3
else 4
if
true
then
3
else 4
>>>
class C:
if true
then 3
else 4
if true
then 3
else 4
if true
then 3
else 4
<<< #3034 for-yield danglingParentheses
danglingParentheses.ctrlSite = true
===
object a:
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
for x <- range
yield
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 for-yield !danglingParentheses
danglingParentheses.ctrlSite = false
===
object a:
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
for x <- range
yield
matrix(x)(0) += matrix(x - 1)(0)
>>>
object a:
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
for x <- range
yield matrix(x)(0) += matrix(x - 1)(0)
<<< #3034 for-do danglingParentheses
danglingParentheses.ctrlSite = true
===
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)
>>>
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)
<<< #3034 for-do !danglingParentheses
danglingParentheses.ctrlSite = false
===
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)
>>>
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)
<<< #3034 while-do danglingParentheses
danglingParentheses.ctrlSite = true
===
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)
>>>
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)
<<< #3034 while-do !danglingParentheses
danglingParentheses.ctrlSite = false
===
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)
>>>
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)
Loading