aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt15
-rw-r--r--core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt52
2 files changed, 65 insertions, 2 deletions
diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt
index 86d5205..433ae1a 100644
--- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt
+++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt
@@ -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
@@ -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()
@@ -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)
+ }
}
}
}
@@ -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
}
diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
index 88b16fe..ae9d05e 100644
--- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
+++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
@@ -446,6 +446,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(
"""