Skip to content

Commit

Permalink
Wrap each type parameter in a multiline type parameter list
Browse files Browse the repository at this point in the history
Closes #1867
  • Loading branch information
paul-dingemans committed Mar 18, 2023
1 parent e05dac0 commit 1ddfdb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ddfdb9

Please # to comment.