Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added
- Support `ij_kotlin_indent_size` in editorconfig. (https://github.com/facebook/ktfmt/pull/604)
- Support for lists within quoted blocks in KDoc comments (https://github.com/facebook/ktfmt/commit/68fa1585b759ad4b12ca4802bccd297f6a33b0f3)
- Fix `ONLY_ADD` trailing commas strategy causing lines over MAX_WIDTH length (https://github.com/facebook/ktfmt/issues/610)

## [0.62]
### Added
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/com/facebook/ktfmt/format/Formatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ object Formatter {
.let { convertLineSeparators(it) }
.let { sortedAndDistinctImports(it) }
.let { dropRedundantElements(it, options) }
.let { addRedundantElements(it, options) }
.let { prettyPrint(it, options, lineSeparator = "\n") }
.let { addRedundantElements(it, options) }
.let { MultilineStringFormatter(options.continuationIndent).format(it) }
Expand Down
108 changes: 108 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 @@ -9004,6 +9004,114 @@ class FormatterTest {
assertThatFormatting(code).isEqualTo(expected)
}

@Test
fun `line with max length that needs a trailing comma`() {
val code =
"""
|fun foo(a: String, b: String) {
| foo(
| a = "this is a very very very very very very veryy long line that has precisely 100 characters",
| b = "also is a very very very very very very veryyy long line that has precisely 100 characters"
| )
|}
|"""
.trimMargin()

val expected =
"""
|fun foo(a: String, b: String) {
| foo(
| a = "this is a very very very very very very veryy long line that has precisely 100 characters",
| b =
| "also is a very very very very very very veryyy long line that has precisely 100 characters",
| )
|}
|"""
.trimMargin()

assertThatFormatting(code)
.withOptions(
defaultTestFormattingOptions.copy(
maxWidth = 100,
blockIndent = 2,
continuationIndent = 2,
trailingCommaManagementStrategy = TrailingCommaManagementStrategy.ONLY_ADD,
),
)
.isEqualTo(expected)
}

@Test
fun `single-line parameter list breaking to multi-line should add trailing comma in one pass`() {
val code =
"""
|fun foo(param1: String, param2: String, param3: String) {
| functionCall(param1 = "value1", param2 = "value2", param3 = "value3", param4 = "value4", param5 = "value5")
|}
|"""
.trimMargin()

val expected =
"""
|fun foo(param1: String, param2: String, param3: String) {
| functionCall(
| param1 = "value1",
| param2 = "value2",
| param3 = "value3",
| param4 = "value4",
| param5 = "value5",
| )
|}
|"""
.trimMargin()

assertThatFormatting(code)
.withOptions(
defaultTestFormattingOptions.copy(
maxWidth = 100,
blockIndent = 2,
continuationIndent = 2,
trailingCommaManagementStrategy = TrailingCommaManagementStrategy.ONLY_ADD,
),
)
.isEqualTo(expected)
}

@Test
fun `single-line parameter list breaking to multi-line when a parameter spans multiple lines`() {
val code =
"""
|fun foo(param1: String, param2: String, param3: String) {
| functionCall(param1 = "value1", param2 = "value2", param3 = "this one is very long and will have to sit on its own line otherwise the line would overflow")
|}
|"""
.trimMargin()

val expected =
"""
|fun foo(param1: String, param2: String, param3: String) {
| functionCall(
| param1 = "value1",
| param2 = "value2",
| param3 =
| "this one is very long and will have to sit on its own line otherwise the line would overflow",
| )
|}
|"""
.trimMargin()

assertThatFormatting(code)
.withOptions(
defaultTestFormattingOptions.copy(
maxWidth = 100,
blockIndent = 2,
continuationIndent = 2,
trailingCommaManagementStrategy = TrailingCommaManagementStrategy.ONLY_ADD,
),
)
.isEqualTo(expected)
}

companion object {
/** Triple quotes, useful to use within triple-quoted strings. */
private const val TQ = "\"\"\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class GoogleStyleFormatterKtTest {

val expected =
"""
|fun f(a: Int, b: Double, c: String) {
|fun f(
| a: Int,
| b: Double,
| c: String,
|) {
| var result = 0
| val aVeryLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongVar =
| 43
Expand Down
Loading