Skip to content

Commit

Permalink
Handle lambda destructuring declarations with explicit type
Browse files Browse the repository at this point in the history
Summary: Fixes #317

Reviewed By: hick209

Differential Revision: D36188965

fbshipit-source-id: 744b503691fea24eb857218786a1191a2f16f4ce
  • Loading branch information
strulovich authored and facebook-github-bot committed May 6, 2022
1 parent 687b6f7 commit b7ff8ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
10 changes: 10 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 @@ -3638,6 +3638,16 @@ class FormatterTest {
|""".trimMargin(),
deduceMaxWidth = true)

@Test
fun `handle lambda with destructuring and type`() =
assertFormatted(
"""
|fun f() {
| g { (a, b): List<Int> -> a }
| g { (a, b): List<Int>, (c, d): List<Int> -> a }
|}
|""".trimMargin())

@Test
fun `handle parenthesis in lambda calls for now`() =
assertFormatted(
Expand Down

0 comments on commit b7ff8ba

Please sign in to comment.