From 1ddfdb9396d3f07b9ce32a13036d78ea2c3d1a3b Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sat, 18 Mar 2023 15:57:19 +0100 Subject: [PATCH] Wrap each type parameter in a multiline type parameter list Closes #1867 --- CHANGELOG.md | 3 ++- .../ruleset/standard/rules/WrappingRule.kt | 7 ++++-- .../standard/rules/WrappingRuleTest.kt | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cfc233165..8746dfd3a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -258,7 +258,8 @@ Previously the default value for `.editorconfig` property `max_line_length` was * When `.editorconfig` property `ij_kotlin_imports_layout` contains a `|` but no import exists that match any pattern before the first `|` then do not report a violation nor insert a blank line `import-ordering` ([#1845](https://github.com/pinterest/ktlint/issues/1845)) * When negate-patterns only are specified in Ktlint CLI then automatically add the default include patterns (`**/*.kt` and `**/*.kts`) so that all Kotlin files excluding the files matching the negate-patterns will be processed ([#1847](https://github.com/pinterest/ktlint/issues/1847)) * Do not remove newlines from multiline type parameter lists `type-parameter-list-spacing` ([#1867](https://github.com/pinterest/ktlint/issues/1867)) -* +* Wrap each type parameter in a multiline type parameter list `wrapping` ([#1867](https://github.com/pinterest/ktlint/issues/1867)) + ### Changed * Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)). Changelog: diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt index 2630476df6..ac2ff0bb23 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt @@ -31,6 +31,8 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_TYPE_CALL_ENT import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_TYPE_ENTRY import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_TYPE_LIST import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PROJECTION import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST @@ -119,7 +121,8 @@ public class WrappingRule : LPAR, LBRACKET -> rearrangeBlock(node, autoCorrect, emit) // TODO: LT SUPER_TYPE_LIST -> rearrangeSuperTypeList(node, autoCorrect, emit) VALUE_PARAMETER_LIST, VALUE_ARGUMENT_LIST -> rearrangeValueList(node, autoCorrect, emit) - TYPE_ARGUMENT_LIST -> rearrangeTypeArgumentList(node, autoCorrect, emit) + TYPE_ARGUMENT_LIST, TYPE_PARAMETER_LIST -> rearrangeTypeArgumentList(node, autoCorrect, emit) +// TYPE_PARAMETER_LIST -> rearrangeTypeParameterList(node, autoCorrect, emit) ARROW -> rearrangeArrow(node, autoCorrect, emit) WHITE_SPACE -> line += node.text.count { it == '\n' } CLOSING_QUOTE -> rearrangeClosingQuote(node, autoCorrect, emit) @@ -381,7 +384,7 @@ public class WrappingRule : // Each type projection must be preceded with a whitespace containing a newline node .children() - .filter { it.elementType == TYPE_PROJECTION } + .filter { it.elementType == TYPE_PROJECTION || it.elementType == TYPE_PARAMETER } .forEach { typeProjection -> typeProjection .prevSibling { !it.isPartOfComment() } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt index 672a7c9c6e..3194095698 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt @@ -1849,6 +1849,28 @@ internal class WrappingRuleTest { .hasNoLintViolations() } } + + @Test + fun `Issue 1867 - Given a multiline type parameter list then wrap each type parameter to a new line`() { + val code = + """ + fun < + Foo, Bar, + FooBar, + > foobar() + """.trimIndent() + val formattedCode = + """ + fun < + Foo, + Bar, + FooBar, + > foobar() + """.trimIndent() + wrappingRuleAssertThat(code) + .hasLintViolation(2, 10, "A newline was expected before 'Bar'") + .isFormattedAs(formattedCode) + } } // Replace the "$." placeholder with an actual "$" so that string "$.{expression}" is transformed to a String template