From 07d2ac0e51df170b34cd32e91a5e13a3ed4c66d6 Mon Sep 17 00:00:00 2001 From: Omer Strulovich Date: Fri, 8 Apr 2022 19:42:09 -0700 Subject: [PATCH] Improve handling of `as` and `is` when left hand is a call-chain 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 --- .../ktfmt/format/KotlinInputAstVisitor.kt | 36 ++++++++++--------- .../facebook/ktfmt/format/FormatterTest.kt | 22 ++++++++---- .../format/GoogleStyleFormatterKtTest.kt | 20 ++++++++--- 3 files changed, 52 insertions(+), 26 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 76a930f7..b8181455 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -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() } /** 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 7b591942..ac893e3a 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -2122,8 +2122,8 @@ class FormatterTest { | .boom()[1, 3, 5] | .lah | .doo { it } - | .feep[1] as - | Boo + | .feep[1] + | as Boo |} |""".trimMargin(), deduceMaxWidth = true) @@ -3122,19 +3122,29 @@ class FormatterTest { |fun castIt( | something: Any |) { + | doIt( + | something + | as List<*>) + | doIt( + | something + | is List<*>) | println( - | something is + | something + | is | List) | doIt( - | something as + | something + | as | List) | println( - | something is + | something + | is | PairList< | String, | Int>) | doIt( - | something as + | something + | as | PairList< | String, | Int>) diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index d80d1315..caec529b 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -461,22 +461,34 @@ class GoogleStyleFormatterKtTest { |fun castIt( | something: Any |) { + | doIt( + | something + | as List<*> + | ) + | doIt( + | something + | is List<*> + | ) | println( - | something is + | something + | is | List | ) | doIt( - | something as + | something + | as | List | ) | println( - | something is + | something + | is | PairList< | String, | Int> | ) | doIt( - | something as + | something + | as | PairList< | String, | Int>