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>