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

FormatOps: fix bug with block in if/while cond #3206

Merged
merged 2 commits into from
May 4, 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 @@ -2216,7 +2216,7 @@ class FormatOps(
): Option[OptionalBracesRegion] =
ft.meta.leftOwner match {
case t @ Term.While(b: Term.Block, _)
if tokenBefore(b).left.is[T.KwDo] =>
if !matchingOpt(nft.right).exists(_.end >= b.pos.end) =>
dos65 marked this conversation as resolved.
Show resolved Hide resolved
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits = Some {
Expand Down Expand Up @@ -2403,7 +2403,7 @@ class FormatOps(
): Option[OptionalBracesRegion] = {
ft.meta.leftOwner match {
case t @ Term.If(b: Term.Block, _, _)
if tokenBefore(b).left.is[T.KwThen] =>
if !matchingOpt(nft.right).exists(_.end >= b.pos.end) =>
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits = Some {
Expand Down
26 changes: 26 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3417,3 +3417,29 @@ object a {
implicit def generate[T](value: T): Clue[T] = macro
MacroCompatScala2.clueImpl
}
<<< #3204 if
object a:
if
val x = 4
4 == 3
then 5
else 4
>>>
object a:
if
val x = 4
4 == 3
then 5
else 4
<<< #3204 while
object a:
while
val x = 4
4 == 3
do 5
>>>
object a:
while
val x = 4
4 == 3
do 5
25 changes: 25 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3256,3 +3256,28 @@ object a {
implicit def generate[T](value: T): Clue[T] = macro
MacroCompatScala2.clueImpl
}
<<< #3204 if
object a:
if
val x = 4
4 == 3
then 5
else 4
>>>
object a:
if
val x = 4
4 == 3
then 5 else 4
<<< #3204 while
object a:
while
val x = 4
4 == 3
do 5
>>>
object a:
while
val x = 4
4 == 3
do 5
26 changes: 26 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3398,3 +3398,29 @@ object a {
implicit def generate[T](value: T): Clue[T] = macro
MacroCompatScala2.clueImpl
}
<<< #3204 if
object a:
if
val x = 4
4 == 3
then 5
else 4
>>>
object a:
if
val x = 4
4 == 3
then 5
else 4
<<< #3204 while
object a:
while
val x = 4
4 == 3
do 5
>>>
object a:
while
val x = 4
4 == 3
do 5
Original file line number Diff line number Diff line change
Expand Up @@ -3508,3 +3508,32 @@ object a {
implicit def generate[T](value: T): Clue[T] = macro
MacroCompatScala2.clueImpl
}
<<< #3204 if
object a:
if
val x = 4
4 == 3
then 5
else 4
>>>
object a:
if
val x = 4
4 == 3
then
5
else
4
<<< #3204 while
object a:
while
val x = 4
4 == 3
do 5
>>>
object a:
while
val x = 4
4 == 3
do
5