Skip to content

Commit

Permalink
Fix breaking before is keyword causing illegal code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
strulovich authored and facebook-github-bot committed May 31, 2022
1 parent c0e6243 commit 24e6942
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
6 changes: 6 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 @@ -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)

Expand Down

1 comment on commit 24e6942

@nreid260
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

Please sign in to comment.