Skip to content

Commit

Permalink
Managing trailing commas is turned on by default now
Browse files Browse the repository at this point in the history
Summary:
As requested on
#216
#442

Reviewed By: davidtorosyan

Differential Revision: D58585873

fbshipit-source-id: 61662ce4b21ad56b5554eb2275cd2e9341ccb1a4
  • Loading branch information
Nivaldo Bondança authored and facebook-github-bot committed Jun 18, 2024
1 parent a3d89d9 commit cec9b50
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
## [Unreleased]

### Changed
- All styles managing trailing commas now (https://github.com/facebook/ktfmt/issues/216, https://github.com/facebook/ktfmt/issues/442)


## [0.51]
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/java/com/facebook/ktfmt/format/Formatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ object Formatter {
FormattingOptions(
blockIndent = 2,
continuationIndent = 4,
manageTrailingCommas = false,
)

@JvmField
val GOOGLE_FORMAT =
FormattingOptions(
blockIndent = 2,
continuationIndent = 2,
manageTrailingCommas = true,
)

/** A format that attempts to reflect https://kotlinlang.org/docs/coding-conventions.html. */
Expand All @@ -64,7 +62,6 @@ object Formatter {
FormattingOptions(
blockIndent = 4,
continuationIndent = 4,
manageTrailingCommas = false,
)

private val MINIMUM_KOTLIN_VERSION = KotlinVersion(1, 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data class FormattingOptions(
* multiple lines will have them removed. Manually inserted trailing commas cannot be used as a
* hint to force breaking lists to multiple lines.
*/
val manageTrailingCommas: Boolean,
val manageTrailingCommas: Boolean = true,

/** Whether ktfmt should remove imports that are not used. */
val removeUnusedImports: Boolean = true,
Expand Down
18 changes: 15 additions & 3 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package com.facebook.ktfmt.format
import com.facebook.ktfmt.format.Formatter.META_FORMAT
import com.facebook.ktfmt.testutil.assertFormatted
import com.facebook.ktfmt.testutil.assertThatFormatting
import com.facebook.ktfmt.testutil.defaultTestFormattingOptions
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.fail
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand Down Expand Up @@ -1472,7 +1474,7 @@ class FormatterTest {
.trimMargin()

assertThatFormatting(code)
.withOptions(META_FORMAT.copy(removeUnusedImports = false))
.withOptions(defaultTestFormattingOptions.copy(removeUnusedImports = false))
.isEqualTo(code)
}

Expand Down Expand Up @@ -4288,7 +4290,9 @@ class FormatterTest {
|}
|"""
.trimMargin()
assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 22)).isEqualTo(expected)
assertThatFormatting(code)
.withOptions(defaultTestFormattingOptions.copy(maxWidth = 22))
.isEqualTo(expected)
}

@Test
Expand Down Expand Up @@ -5373,7 +5377,9 @@ class FormatterTest {
|class MyClass {}
|"""
.trimMargin()
assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 33)).isEqualTo(expected)
assertThatFormatting(code)
.withOptions(defaultTestFormattingOptions.copy(maxWidth = 33))
.isEqualTo(expected)
}

@Test
Expand Down Expand Up @@ -7494,5 +7500,11 @@ class FormatterTest {
companion object {
/** Triple quotes, useful to use within triple-quoted strings. */
private const val TQ = "\"\"\""

@JvmStatic
@BeforeClass
fun setUp(): Unit {
defaultTestFormattingOptions = META_FORMAT.copy(manageTrailingCommas = false)
}
}
}
Loading

0 comments on commit cec9b50

Please sign in to comment.