Skip to content

Commit f767ee8

Browse files
Nivaldo Bondançafacebook-github-bot
authored andcommitted
Create TrailingCommaManagementStrategy which allows option for only addition of trailing commas
Summary: This makes it possible to fix the issues: - #461 - #512 - #514 Differential Revision: D79772883 fbshipit-source-id: 19edf3eefe1daa2fb6b378db7a5aee3dcdfe308e
1 parent cedc196 commit f767ee8

5 files changed

Lines changed: 58 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1212

1313
## [Unreleased]
1414

15-
### Changed
15+
### Added
16+
- `TrailingCommaManagementStrategy.ONLY_ADD` strategy that does not remove existing trailing commas (https://github.com/facebook/ktfmt/issues/461, https://github.com/facebook/ktfmt/issues/512, https://github.com/facebook/ktfmt/issues/514)
1617

18+
### Changed
19+
- `FormattingOptions.manageTrailingCommas` was replaced with `FormattingOptions.trailingCommaManagementStrategy`, which also added new `TrailingCommaManagementStrategy.ONLY_ADD` strategy (https://github.com/facebook/ktfmt/issues/461, https://github.com/facebook/ktfmt/issues/512, https://github.com/facebook/ktfmt/issues/514)
1720

1821
### Removed
1922
- Removed mvn build scripts

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ object Formatter {
4747
FormattingOptions(
4848
blockIndent = 2,
4949
continuationIndent = 4,
50-
manageTrailingCommas = false,
50+
trailingCommaManagementStrategy = TrailingCommaManagementStrategy.NONE,
5151
)
5252

5353
@JvmField

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.facebook.ktfmt.format
1818

19+
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy.COMPLETE
20+
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy.NONE
21+
1922
data class FormattingOptions(
2023
/** ktfmt breaks lines longer than maxWidth. */
2124
val maxWidth: Int = DEFAULT_MAX_WIDTH,
@@ -45,13 +48,11 @@ data class FormattingOptions(
4548
val continuationIndent: Int,
4649

4750
/**
48-
* Automatically remove and insert trialing commas.
51+
* Strategy for managing trailing commas.
4952
*
50-
* Lists that cannot fit on one line will have trailing commas inserted. Lists that span
51-
* multiple lines will have them removed. Manually inserted trailing commas cannot be used as a
52-
* hint to force breaking lists to multiple lines.
53+
* See [TrailingCommaManagementStrategy] for more details.
5354
*/
54-
val manageTrailingCommas: Boolean = true,
55+
val trailingCommaManagementStrategy: TrailingCommaManagementStrategy = COMPLETE,
5556

5657
/** Whether ktfmt should remove imports that are not used. */
5758
val removeUnusedImports: Boolean = true,
@@ -65,4 +66,48 @@ data class FormattingOptions(
6566
companion object {
6667
const val DEFAULT_MAX_WIDTH: Int = 100
6768
}
69+
70+
@Deprecated("Here just for retrocompatibility reasons. Will be removed on 1.0.0")
71+
constructor(
72+
maxWidth: Int,
73+
blockIndent: Int,
74+
continuationIndent: Int,
75+
manageTrailingCommas: Boolean = true,
76+
removeUnusedImports: Boolean = true,
77+
debuggingPrintOpsAfterFormatting: Boolean = false,
78+
) : this(
79+
maxWidth = maxWidth,
80+
blockIndent = blockIndent,
81+
continuationIndent = continuationIndent,
82+
trailingCommaManagementStrategy = if (manageTrailingCommas) COMPLETE else NONE,
83+
removeUnusedImports = removeUnusedImports,
84+
debuggingPrintOpsAfterFormatting = debuggingPrintOpsAfterFormatting,
85+
)
86+
87+
internal val manageTrailingCommas: Boolean
88+
get() = trailingCommaManagementStrategy != NONE
89+
}
90+
91+
enum class TrailingCommaManagementStrategy(
92+
val removeRedundantTrailingCommas: Boolean,
93+
) {
94+
/** Do not manage trailing commas at all, only format what is already present */
95+
NONE(removeRedundantTrailingCommas = false),
96+
97+
/**
98+
* Only add trailing commas when necessary, but do not remove them.
99+
*
100+
* Lists that cannot fit on one line will have trailing commas inserted. Trailing commas can to be
101+
* used to "hint" ktfmt that the list should be broken to multiple lines.
102+
*/
103+
ONLY_ADD(removeRedundantTrailingCommas = false),
104+
105+
/**
106+
* Fully manage trailing commas, adding and removing them where necessary.
107+
*
108+
* Lists that cannot fit on one line will have trailing commas inserted. Lists that span multiple
109+
* lines will have them removed. Manually inserted trailing commas cannot be used as a hint to
110+
* force breaking lists to multiple lines.
111+
*/
112+
COMPLETE(removeRedundantTrailingCommas = true),
68113
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ object RedundantElementManager {
4848
}
4949

5050
redundantSemicolonDetector.takeElement(element)
51-
if (options.manageTrailingCommas) {
51+
if (options.trailingCommaManagementStrategy.removeRedundantTrailingCommas) {
5252
trailingCommaDetector.takeElement(element)
5353
}
5454
super.visitElement(element)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7786,7 +7786,8 @@ class FormatterTest {
77867786
@JvmStatic
77877787
@BeforeClass
77887788
fun setUp(): Unit {
7789-
defaultTestFormattingOptions = META_FORMAT.copy(manageTrailingCommas = false)
7789+
defaultTestFormattingOptions =
7790+
META_FORMAT.copy(trailingCommaManagementStrategy = TrailingCommaManagementStrategy.NONE)
77907791
}
77917792
}
77927793
}

0 commit comments

Comments
 (0)