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: bugfix, check else for opening brace #3299

Merged
merged 3 commits into from
Aug 17, 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 @@ -2162,8 +2162,9 @@ class FormatOps(
def rightBrace = blockLast(b)
})
case t @ Term.If(_, thenp, _) if !nft.right.is[T.KwThen] && {
isTreeMultiStatBlock(thenp) || isElsePWithOptionalBraces(t) ||
!ifWithoutElse(t) && existsBlockIfWithoutElse(thenp, false)
isTreeMultiStatBlock(thenp) || !ifWithoutElse(t) &&
(isElsePWithOptionalBraces(t) ||
existsBlockIfWithoutElse(thenp, false))
} =>
Some(new OptionalBracesRegion {
def owner = Some(t)
Expand Down Expand Up @@ -2526,16 +2527,18 @@ class FormatOps(
}

@tailrec
private def isElsePWithOptionalBraces(tree: Term.If): Boolean =
!ifWithoutElse(tree) &&
!tokenBefore(tree.elsep).left.is[T.LeftBrace] &&
(tree.elsep match {
case t: Term.If =>
isThenPWithOptionalBraces(t) || isElsePWithOptionalBraces(t)
case Term.Block(List(t: Term.If)) =>
isThenPWithOptionalBraces(t) || isElsePWithOptionalBraces(t)
case t => isTreeMultiStatBlock(t)
})
private def isElsePWithOptionalBraces(tree: Term.If): Boolean = {
val elsep = tree.elsep
!tokens.getHead(elsep).left.is[T.LeftBrace] && (elsep match {
case t: Term.If =>
isThenPWithOptionalBraces(t) ||
!ifWithoutElse(t) && isElsePWithOptionalBraces(t)
case Term.Block(List(t: Term.If)) =>
isThenPWithOptionalBraces(t) ||
!ifWithoutElse(t) && isElsePWithOptionalBraces(t)
case t => isTreeMultiStatBlock(t)
})
}

private def shouldBreakInOptionalBraces(
ft: FormatToken
Expand Down
34 changes: 34 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5971,3 +5971,37 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
29 changes: 29 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5685,3 +5685,32 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1) true else { false }

def notOK: Boolean =
if (1 == 1) true
else {
println("Some code")
false
}
}
34 changes: 34 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5977,3 +5977,37 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
34 changes: 34 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6129,3 +6129,37 @@ def sum(a: Int, b: Int): Int =
>>>
def sum(a: Int, b: Int): Int =
a + b
<<< #3295 1
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
36 changes: 36 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3518,3 +3518,39 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
maxColumn = 40
===
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
31 changes: 31 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3349,3 +3349,34 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
maxColumn = 40
===
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1) true else { false }

def notOK: Boolean =
if (1 == 1) true
else {
println("Some code")
false
}
}
36 changes: 36 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3500,3 +3500,39 @@ class Foo() {
try { c; a + b }.foo
finally { c; a + b }.foo
}
<<< #3295 1
maxColumn = 40
===
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3625,3 +3625,39 @@ class Foo() {
a + b
}.foo
}
<<< #3295 1
maxColumn = 40
===
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}
>>>
class Foo() {
def ok: Boolean =
if (1 == 1)
true
else {
false
}

def notOK: Boolean =
if (1 == 1)
true
else {
println("Some code")
false
}
}