From 24e6942904c4575fb52f6331e526922a48cd9d2f Mon Sep 17 00:00:00 2001 From: Omer Strulovich Date: Tue, 31 May 2022 12:30:03 -0700 Subject: [PATCH] Fix breaking before is keyword causing illegal code Summary: This should resolve https://github.com/facebookincubator/ktfmt/issues/315 Tried to avoid changing code in too many places by looking at the parent. Reviewed By: davidtorosyan Differential Revision: D36783125 fbshipit-source-id: 54fcbdc7372f9147925b2746aa73e61fc06346d3 --- .../com/facebook/ktfmt/format/KotlinInputAstVisitor.kt | 10 +++++++++- .../java/com/facebook/ktfmt/format/FormatterTest.kt | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 cfa58c38..4a6aae44 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -51,6 +51,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression import org.jetbrains.kotlin.psi.KtConstantExpression import org.jetbrains.kotlin.psi.KtConstructorDelegationCall +import org.jetbrains.kotlin.psi.KtContainerNode import org.jetbrains.kotlin.psi.KtContinueExpression import org.jetbrains.kotlin.psi.KtDelegatedSuperTypeEntry import org.jetbrains.kotlin.psi.KtDestructuringDeclaration @@ -2169,7 +2170,14 @@ class KotlinInputAstVisitor( if (openGroupBeforeLeft) builder.open(ZERO) visit(expression.leftHandSide) if (!openGroupBeforeLeft) builder.open(ZERO) - builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent) + val parent = expression.parent + if (parent is KtValueArgument || + parent is KtParenthesizedExpression || + parent is KtContainerNode) { + builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent) + } else { + builder.space() + } visit(expression.operationReference) builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent) builder.block(expressionBreakIndent) { visit(expression.typeReference) } 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 5c448fb8..b403a01b 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -3236,7 +3236,13 @@ class FormatterTest { | println( | a is Int && | b is String) + | l.b?.s?.sOrNull() is + | SomethingLongEnough |} + | + |val a = + | l.sOrNull() is + | SomethingLongEnough |""".trimMargin(), deduceMaxWidth = true)