Skip to content

Commit

Permalink
Preserve new lines between when clauses
Browse files Browse the repository at this point in the history
Summary:
Implemented as requested on
facebook#342

Reviewed By: davidtorosyan

Differential Revision: D58271914

fbshipit-source-id: 048d9a754b40200f313654775eb743c024073d39
  • Loading branch information
Nivaldo Bondança authored and facebook-github-bot committed Jun 7, 2024
1 parent 8605080 commit 7d23e59
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1882,8 +1882,12 @@ class KotlinInputAstVisitor(
builder.space()
builder.token("{", Doc.Token.RealOrImaginary.REAL, blockIndent, Optional.of(blockIndent))

expression.entries.forEach { whenEntry ->
expression.entries.forEachIndexed { index, whenEntry ->
builder.block(blockIndent) {
if (index != 0) {
// preserve new line if there's one
builder.blankLineWanted(OpsBuilder.BlankLineWanted.PRESERVE)
}
builder.forcedBreak()
if (whenEntry.isElse) {
builder.token("else")
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,50 @@ class FormatterTest {
|"""
.trimMargin())

@Test
fun `newlines between clauses of when() are preserved`() {
assertThatFormatting(
"""
|fun f(x: Int) {
| when (x) {
|
|
| 1 -> print(1)
| 2 -> print(2)
|
|
| 3 ->
| // Comment
| print(3)
|
| else -> {
| print("else")
| }
|
| }
|}
|"""
.trimMargin())
.isEqualTo(
"""
|fun f(x: Int) {
| when (x) {
| 1 -> print(1)
| 2 -> print(2)
|
| 3 ->
| // Comment
| print(3)
|
| else -> {
| print("else")
| }
| }
|}
|"""
.trimMargin())
}

@Test
fun `when() with a subject expression`() =
assertFormatted(
Expand Down

0 comments on commit 7d23e59

Please sign in to comment.