Skip to content

Commit

Permalink
Improve handling of as and is when left hand is a call-chain
Browse files Browse the repository at this point in the history
Summary:
This improves the formatting of casting using the `as` keyword.
I am not merging this into the handling of qualified expressions since `as` can only appear in the end of a chain (parenthesis are required to continue the chain in every reasonable case, an unreasonable case is `any as Foo as Bar`, which we can easily support, but why spend time on it)

Reviewed By: hick209

Differential Revision: D35503254

fbshipit-source-id: 0bbc1834bccf4842cf9d97d07750b9939720fc22
  • Loading branch information
strulovich authored and facebook-github-bot committed Apr 9, 2022
1 parent 46129b6 commit 07d2ac0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2068,26 +2068,30 @@ class KotlinInputAstVisitor(

/** Example `a is Int` or `b !is Int` */
override fun visitIsExpression(expression: KtIsExpression) {
builder.block(ZERO) {
builder.sync(expression)
visit(expression.leftHandSide)
builder.space()
visit(expression.operationReference)
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(expression.typeReference) }
}
builder.sync(expression)
val openGroupBeforeLeft = expression.leftHandSide !is KtQualifiedExpression
if (openGroupBeforeLeft) builder.open(ZERO)
visit(expression.leftHandSide)
if (!openGroupBeforeLeft) builder.open(ZERO)
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
visit(expression.operationReference)
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(expression.typeReference) }
builder.close()
}

/** Example `a as Int` or `a as? Int` */
override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS) {
builder.block(ZERO) {
builder.sync(expression)
visit(expression.left)
builder.space()
visit(expression.operationReference)
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(expression.right) }
}
builder.sync(expression)
val openGroupBeforeLeft = expression.left !is KtQualifiedExpression
if (openGroupBeforeLeft) builder.open(ZERO)
visit(expression.left)
if (!openGroupBeforeLeft) builder.open(ZERO)
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
visit(expression.operationReference)
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
builder.block(expressionBreakIndent) { visit(expression.right) }
builder.close()
}

/**
Expand Down
22 changes: 16 additions & 6 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2122,8 +2122,8 @@ class FormatterTest {
| .boom()[1, 3, 5]
| .lah
| .doo { it }
| .feep[1] as
| Boo
| .feep[1]
| as Boo

This comment has been minimized.

Copy link
@nreid260

nreid260 Apr 12, 2022

Contributor

This formatting is inconsistent with the other binary operators, which still produce the old format

Example:

-------------
fooooooo
  .bar + 0
|}
|""".trimMargin(),
deduceMaxWidth = true)
Expand Down Expand Up @@ -3122,19 +3122,29 @@ class FormatterTest {
|fun castIt(
| something: Any
|) {
| doIt(
| something
| as List<*>)
| doIt(
| something
| is List<*>)
| println(
| something is
| something
| is
| List<String>)
| doIt(
| something as
| something
| as
| List<String>)
| println(
| something is
| something
| is
| PairList<
| String,
| Int>)
| doIt(
| something as
| something
| as
| PairList<
| String,
| Int>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,34 @@ class GoogleStyleFormatterKtTest {
|fun castIt(
| something: Any
|) {
| doIt(
| something
| as List<*>
| )
| doIt(
| something
| is List<*>
| )
| println(
| something is
| something
| is
| List<String>
| )
| doIt(
| something as
| something
| as
| List<String>
| )
| println(
| something is
| something
| is
| PairList<
| String,
| Int>
| )
| doIt(
| something as
| something
| as
| PairList<
| String,
| Int>
Expand Down

0 comments on commit 07d2ac0

Please sign in to comment.