Skip to content

Commit

Permalink
Preserve a trailing space when lambda has only block comments
Browse files Browse the repository at this point in the history
This fixes facebook#408
  • Loading branch information
fredyw committed Oct 5, 2023
1 parent 102c89d commit 27af02f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ class KotlinInputAstVisitor(
val valueParams = lambdaExpression.valueParameters
val hasParams = valueParams.isNotEmpty()
val statements = (lambdaExpression.bodyExpression ?: fail()).children
val hasComments = lambdaExpression.bodyExpression?.children()?.any { it is PsiComment } ?: false
val hasStatements = statements.isNotEmpty()
val hasArrow = lambdaExpression.functionLiteral.arrow != null

Expand Down Expand Up @@ -944,7 +945,7 @@ class KotlinInputAstVisitor(
}
}

if (hasParams || hasArrow || hasStatements) {
if (hasParams || hasArrow || hasStatements || hasComments) {
// If we had to break in the body, ensure there is a break before the closing brace
builder.breakOp(Doc.FillMode.UNIFIED, " ", bracePlusZeroIndent)
}
Expand Down
9 changes: 9 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 @@ -5808,6 +5808,15 @@ class FormatterTest {
|"""
.trimMargin())

@Test
fun `lambda with only comments`() =
assertFormatted(
"""
|val a = { /* do nothing */ }
|val b = { /* do nothing */ /* also do nothing */ }
|"""
.trimMargin())

@Test
fun `chaining - many dereferences`() =
assertFormatted(
Expand Down

0 comments on commit 27af02f

Please sign in to comment.