Skip to content

Commit

Permalink
Fix indentation of initializer-like expressions with leading comments (
Browse files Browse the repository at this point in the history
…#382)

Summary: Pull Request resolved: #382

Reviewed By: hick209

Differential Revision: D42801455

Pulled By: cgrushko

fbshipit-source-id: c53eb78591d5c23301bb2bafd84f89c5bb7a7a33
  • Loading branch information
nreid260 authored and facebook-github-bot committed Feb 2, 2023
1 parent ba6d9e5 commit cd5fdc7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import org.jetbrains.kotlin.psi.KtWhenConditionWithExpression
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.KtWhileExpression
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.startsWithComment

Expand Down Expand Up @@ -1346,7 +1347,10 @@ class KotlinInputAstVisitor(
visit(delegate)
} else {
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(delegate) }
builder.block(expressionBreakIndent) {
builder.fenceComments()
visit(delegate)
}
}
} else if (initializer != null) {
builder.space()
Expand All @@ -1355,7 +1359,10 @@ class KotlinInputAstVisitor(
visitLambdaOrScopingFunction(initializer)
} else {
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(initializer) }
builder.block(expressionBreakIndent) {
builder.fenceComments()
visit(initializer)
}
}
}
}
Expand Down Expand Up @@ -1409,6 +1416,10 @@ class KotlinInputAstVisitor(
* 2. '... = Runnable @Annotation { ... }' due to the annotation
*/
private fun isLambdaOrScopingFunction(expression: KtExpression?): Boolean {
if (expression == null) return false
if (expression.getPrevSiblingIgnoringWhitespace() is PsiComment) {
return false // Leading comments cause weird indentation.
}
if (expression is KtLambdaExpression) {
return true
}
Expand Down
52 changes: 52 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 @@ -445,6 +445,58 @@ class FormatterTest {
|"""
.trimMargin())

@Test
fun `properties with line comment above initializer`() =
assertFormatted(
"""
|class Foo {
| var x: Int =
| // Comment
| 0
|
| var y: Int =
| // Comment
| scope {
| 0 //
| }
|
| var z: Int =
| // Comment
| if (cond) {
| 0
| } else {
| 1
| }
|}
|"""
.trimMargin())

@Test
fun `properties with line comment above delegate`() =
assertFormatted(
"""
|class Foo {
| var x: Int by
| // Comment
| 0
|
| var y: Int by
| // Comment
| scope {
| 0 //
| }
|
| var z: Int by
| // Comment
| if (cond) {
| 0
| } else {
| 1
| }
|}
|"""
.trimMargin())

@Test
fun `properties with accessors`() =
assertFormatted(
Expand Down

0 comments on commit cd5fdc7

Please sign in to comment.