Skip to content

Commit dcb5f0e

Browse files
goutamadwantmeta-codesync[bot]
authored andcommitted
Support name-based destructuring declarations (#630)
Summary: - Preserve square-bracket destructuring delimiters instead of always emitting parentheses. - Format name-based destructuring entries with renames such as `x = b`. - Add regression coverage for bracket destructuring and rename spacing. Fixes #629 ## Tests - `./gradlew :ktfmt:test --tests "com.facebook.ktfmt.format.FormatterTest.handle name based destructuring declaration" --no-daemon --console=plain --stacktrace` - `./gradlew :ktfmt:test --tests "com.facebook.ktfmt.format.FormatterTest" --no-daemon --console=plain --stacktrace` - `./gradlew :ktfmt:build --no-daemon --console=plain --stacktrace` Note: I also tried `./gradlew build --no-daemon --console=plain --stacktrace`; the local run reached ktfmt checks, then failed in unrelated `:lambda:test` dependency resolution because local Maven is missing `io.netty:netty-transport-native-epoll:4.1.42.Final:linux-x86_64`. Pull Request resolved: #630 Reviewed By: cortinico Differential Revision: D108969079 Pulled By: hick209 fbshipit-source-id: 2a30f0fac7ab7db5b95ddb2ffaa0ce9e9cb8c9a4
1 parent 78ac5d7 commit dcb5f0e

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1717
* Reduced overall number of allocations to improve formatting performance (~6-7%) (https://github.com/facebook/ktfmt/pull/620)
1818
* Reuse results of `Parser.parse` (https://github.com/facebook/ktfmt/pull/622)
1919

20+
### Fixed
21+
22+
* Support name-based destructuring declarations. (https://github.com/facebook/ktfmt/issues/629)
23+
2024
## [0.63]
2125

2226
### Added

core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ class KotlinInputAstVisitor(
22482248
builder.token("]")
22492249
}
22502250

2251-
/** Example `val (a, b: Int) = Pair(1, 2)` */
2251+
/** Example `val (a, b: Int) = Pair(1, 2)` or `val [a, b] = Pair(1, 2)` */
22522252
override fun visitDestructuringDeclaration(destructuringDeclaration: KtDestructuringDeclaration) {
22532253
builder.sync(destructuringDeclaration)
22542254
val valOrVarKeyword = destructuringDeclaration.valOrVarKeyword
@@ -2257,8 +2257,10 @@ class KotlinInputAstVisitor(
22572257
builder.space()
22582258
}
22592259
val hasTrailingComma = destructuringDeclaration.trailingComma != null
2260+
val openingDelimiter = destructuringDeclaration.lPar?.text ?: "("
2261+
val closingDelimiter = destructuringDeclaration.rPar?.text ?: ")"
22602262
builder.block(ZERO) {
2261-
builder.token("(")
2263+
builder.token(openingDelimiter)
22622264
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakIndent)
22632265
builder.block(expressionBreakIndent) {
22642266
visitEachCommaSeparated(
@@ -2268,7 +2270,7 @@ class KotlinInputAstVisitor(
22682270
)
22692271
}
22702272
}
2271-
builder.token(")")
2273+
builder.token(closingDelimiter)
22722274
val initializer = destructuringDeclaration.initializer
22732275
if (initializer != null) {
22742276
builder.space()
@@ -2282,13 +2284,13 @@ class KotlinInputAstVisitor(
22822284
}
22832285
}
22842286

2285-
/** Example `a: String` which is part of `(a: String, b: String)` */
2287+
/** Example `a: String` or `x = a` which is part of `(a: String, x = a)` */
22862288
override fun visitDestructuringDeclarationEntry(
22872289
multiDeclarationEntry: KtDestructuringDeclarationEntry
22882290
) {
22892291
builder.sync(multiDeclarationEntry)
22902292
declareOne(
2291-
initializer = null,
2293+
initializer = multiDeclarationEntry.initializer,
22922294
kind = DeclarationKind.PARAMETER,
22932295
modifiers = multiDeclarationEntry.modifierList,
22942296
name = multiDeclarationEntry.nameIdentifier?.text ?: fail(),

core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,30 @@ class FormatterTest {
30733073
deduceMaxWidth = true,
30743074
)
30753075

3076+
@Test
3077+
fun `handle name based destructuring declaration`() {
3078+
if (KotlinVersion.CURRENT < KotlinVersion(2, 3)) return
3079+
3080+
assertThatFormatting(
3081+
"""
3082+
|fun f(d: D) {
3083+
| val [a, b ] = d
3084+
| val (a, x = b ) = d
3085+
|}
3086+
|"""
3087+
.trimMargin()
3088+
)
3089+
.isEqualTo(
3090+
"""
3091+
|fun f(d: D) {
3092+
| val [a, b] = d
3093+
| val (a, x = b) = d
3094+
|}
3095+
|"""
3096+
.trimMargin()
3097+
)
3098+
}
3099+
30763100
@Test
30773101
fun `chains with derferences and array indexing`() =
30783102
assertFormatted(

0 commit comments

Comments
 (0)