diff --git a/CHANGELOG.md b/CHANGELOG.md index 72c9175f..7be723f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt index 707aa496..1cf431ab 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt @@ -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) } diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt index 95e03a11..dbe23e0f 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -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 = "\"\"\"" diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index e0cced4e..1afd4a32 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -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