Skip to content

Commit

Permalink
Force a function type inside a nullable type to a separate line in ca…
Browse files Browse the repository at this point in the history
…se the single line nullable exceeds the max line length

Closes #1255
  • Loading branch information
Paul Dingemans committed Jan 30, 2022
1 parent bc9d81d commit bf30d98
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Fix indent of delegated super type entry (`indent`) ([#1210](https://github.com/pinterest/ktlint/issues/1210))
- Improve indentation of closing quotes of a multiline raw string literal (`indent`) ([#1262](https://github.com/pinterest/ktlint/pull/1262))
- Fix false positive indentation (`parameter-list-wrapping`, `argument-list-wrapping`) ([#897](https://github.com/pinterest/ktlint/issues/897), [#1045](https://github.com/pinterest/ktlint/issues/1045), [#1119](https://github.com/pinterest/ktlint/issues/1119), [#1255](https://github.com/pinterest/ktlint/issues/1255), [#1267](https://github.com/pinterest/ktlint/issues/1267), [#1319](https://github.com/pinterest/ktlint/issues/1319), [#1320](https://github.com/pinterest/ktlint/issues/1320), [#1337](https://github.com/pinterest/ktlint/issues/1337)
- Force a single line function type inside a nullable type to a separate line when the max line length is exceeded (`parameter-list-wrapping`) ([#1255](https://github.com/pinterest/ktlint/issues/1255))

### Changed
- Update Kotlin version to `1.6.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,49 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
if (indentConfig.disabled) {
return
}

node
.takeIf { it.elementType == NULLABLE_TYPE }
?.takeUnless {
// skip when max line length not set or does not exceed max line length
maxLineLength <= 0 || (node.column - 1 + node.textLength) <= maxLineLength
}?.findChildByType(FUNCTION_TYPE)
?.findChildByType(VALUE_PARAMETER_LIST)
?.takeIf { it.findChildByType(VALUE_PARAMETER) != null }
?.takeUnless { it.textContains('\n') }
?.let {
node
.children()
.forEach {
when (it.elementType) {
LPAR -> {
emit(
it.startOffset,
"Parameter of nullable type should be on a separate line (unless the type fits on a single line)",
true
)
if (autoCorrect) {
(it as LeafElement).upsertWhitespaceAfterMe("\n${indentConfig.indent}")
}
}
RPAR -> {
emit(it.startOffset, errorMessage(it), true)
if (autoCorrect) {
(it as LeafElement).upsertWhitespaceBeforeMe("\n")
}
}
}
}
}

if (node.elementType == VALUE_PARAMETER_LIST &&
// skip when there are no parameters
node.firstChildNode?.treeNext?.elementType != RPAR &&
// skip lambda parameters
node.treeParent?.elementType != FUNCTION_LITERAL
node.treeParent?.elementType != FUNCTION_LITERAL &&
// skip when function type is wrapped in a nullable type [which was already when processing the nullable
// type node itself.
!(node.treeParent.elementType == FUNCTION_TYPE && node.treeParent?.treeParent?.elementType == NULLABLE_TYPE)
) {
// each parameter should be on a separate line if
// - at least one of the parameters is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,28 @@ class ParameterListWrappingRuleTest {
""".trimIndent()
assertThat(ParameterListWrappingRule().lint(code)).isEmpty()
}

@Test
fun `Issue 1255 - Given a variable declaration for nullable function type which exceeds the max-line-length then wrap the function type to a new line`() {
val code =
"""
var changesListener: ((width: Double?, depth: Double?, length: Double?, area: Double?) -> Unit)? = null
""".trimIndent()
val formattedCode =
"""
var changesListener: (
(width: Double?, depth: Double?, length: Double?, area: Double?) -> Unit
)? = null
""".trimIndent()
assertThat(
ParameterListWrappingRule().lint(
code,
userData = mapOf("max_line_length" to "80")
)
).containsExactly(
LintError(1, 22, "parameter-list-wrapping", "Parameter of nullable type should be on a separate line (unless the type fits on a single line)"),
LintError(1, 95, "parameter-list-wrapping", """Missing newline before ")"""")
)
assertThat(ParameterListWrappingRule().format(code, userData = mapOf("max_line_length" to "80"))).isEqualTo(formattedCode)
}
}

0 comments on commit bf30d98

Please sign in to comment.