Skip to content

Commit 24e6942

Browse files
strulovichfacebook-github-bot
authored andcommitted
Fix breaking before is keyword causing illegal code
Summary: This should resolve #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
1 parent c0e6243 commit 24e6942

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
5151
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
5252
import org.jetbrains.kotlin.psi.KtConstantExpression
5353
import org.jetbrains.kotlin.psi.KtConstructorDelegationCall
54+
import org.jetbrains.kotlin.psi.KtContainerNode
5455
import org.jetbrains.kotlin.psi.KtContinueExpression
5556
import org.jetbrains.kotlin.psi.KtDelegatedSuperTypeEntry
5657
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
@@ -2169,7 +2170,14 @@ class KotlinInputAstVisitor(
21692170
if (openGroupBeforeLeft) builder.open(ZERO)
21702171
visit(expression.leftHandSide)
21712172
if (!openGroupBeforeLeft) builder.open(ZERO)
2172-
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
2173+
val parent = expression.parent
2174+
if (parent is KtValueArgument ||
2175+
parent is KtParenthesizedExpression ||
2176+
parent is KtContainerNode) {
2177+
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
2178+
} else {
2179+
builder.space()
2180+
}
21732181
visit(expression.operationReference)
21742182
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
21752183
builder.block(expressionBreakIndent) { visit(expression.typeReference) }

Diff for: core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -3236,7 +3236,13 @@ class FormatterTest {
32363236
| println(
32373237
| a is Int &&
32383238
| b is String)
3239+
| l.b?.s?.sOrNull() is
3240+
| SomethingLongEnough
32393241
|}
3242+
|
3243+
|val a =
3244+
| l.sOrNull() is
3245+
| SomethingLongEnough
32403246
|""".trimMargin(),
32413247
deduceMaxWidth = true)
32423248

0 commit comments

Comments
 (0)