From b7ff8ba9f6bd5f1c65c4b7a508d186ca5ecb6180 Mon Sep 17 00:00:00 2001 From: Omer Strulovich Date: Thu, 5 May 2022 21:03:02 -0700 Subject: [PATCH] Handle lambda destructuring declarations with explicit type Summary: Fixes https://github.com/facebookincubator/ktfmt/issues/317 Reviewed By: hick209 Differential Revision: D36188965 fbshipit-source-id: 744b503691fea24eb857218786a1191a2f16f4ce --- .../facebook/ktfmt/format/KotlinInputAstVisitor.kt | 12 ++++++++++-- .../java/com/facebook/ktfmt/format/FormatterTest.kt | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 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 1f56f333..e59a1eef 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -2020,15 +2020,23 @@ class KotlinInputAstVisitor( builder.sync(parameter) builder.block(ZERO) { val destructuringDeclaration = parameter.destructuringDeclaration + val typeReference = parameter.typeReference if (destructuringDeclaration != null) { - visit(destructuringDeclaration) + builder.block(ZERO) { + visit(destructuringDeclaration) + if (typeReference != null) { + builder.token(":") + builder.space() + visit(typeReference) + } + } } else { declareOne( kind = DeclarationKind.PARAMETER, modifiers = parameter.modifierList, valOrVarKeyword = parameter.valOrVarKeyword?.text, name = parameter.nameIdentifier?.text, - type = parameter.typeReference, + type = typeReference, initializer = parameter.defaultValue) } } 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 ff83cb96..22482e51 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -3638,6 +3638,16 @@ class FormatterTest { |""".trimMargin(), deduceMaxWidth = true) + @Test + fun `handle lambda with destructuring and type`() = + assertFormatted( + """ + |fun f() { + | g { (a, b): List -> a } + | g { (a, b): List, (c, d): List -> a } + |} + |""".trimMargin()) + @Test fun `handle parenthesis in lambda calls for now`() = assertFormatted(