Skip to content

Commit

Permalink
Resolve conflict between parameter-list-spacing and parameter-list-wr…
Browse files Browse the repository at this point in the history
…apping (#2491)

* Resolve conflict between parameter-list-spacing and parameter-list-wrapping

Closes #2488
  • Loading branch information
paul-dingemans authored Jan 6, 2024
1 parent 784263f commit 6a6cc17
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions ktlint-ruleset-standard/api/ktlint-ruleset-standard.api
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ public final class com/pinterest/ktlint/ruleset/standard/rules/PackageNameRuleKt

public final class com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule : com/pinterest/ktlint/ruleset/standard/StandardRule {
public fun <init> ()V
public fun beforeFirstNode (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;)V
public fun beforeVisitChildNodes (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;ZLkotlin/jvm/functions/Function3;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.lineLengthWithoutNewlinePrefix
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
Expand All @@ -32,7 +36,18 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
*/
@SinceKtlint("0.46", EXPERIMENTAL)
@SinceKtlint("1.0", STABLE)
public class ParameterListSpacingRule : StandardRule("parameter-list-spacing") {
public class ParameterListSpacingRule :
StandardRule(
id = "parameter-list-spacing",
usesEditorConfigProperties =
setOf(MAX_LINE_LENGTH_PROPERTY),
) {
private var maxLineLength = MAX_LINE_LENGTH_PROPERTY.defaultValue

override fun beforeFirstNode(editorConfig: EditorConfig) {
maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY]
}

override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
Expand Down Expand Up @@ -201,6 +216,13 @@ public class ParameterListSpacingRule : StandardRule("parameter-list-spacing") {
// Bar,
// )
Unit
} else if (whiteSpaceAfterColon.hasTypeReferenceWhichDoesNotFitOnSameLineAsColon()) {
// Allow the type to be wrapped to the next line when the type does not fit on same line as colon:
// class Foo(
// val someReallyLongFieldNameUsedInMyClass:
// SomeReallyLongDependencyClass
// )
Unit
} else if (whiteSpaceAfterColon.isNotSingleSpace()) {
replaceWithSingleSpace(whiteSpaceAfterColon, emit, autoCorrect)
}
Expand Down Expand Up @@ -270,6 +292,24 @@ public class ParameterListSpacingRule : StandardRule("parameter-list-spacing") {
this
?.findChildByType(TYPE_REFERENCE)
?.findChildByType(MODIFIER_LIST)

private fun ASTNode.hasTypeReferenceWhichDoesNotFitOnSameLineAsColon() =
takeIf { it.isWhiteSpaceWithNewline() }
?.nextCodeSibling()
?.takeIf { it.elementType == TYPE_REFERENCE }
?.let { typeReference ->
val length =
// length of previous line
lineLengthWithoutNewlinePrefix() +
// single space before type reference
1 -
// length of current indent before typeReference
this.text.substringAfterLast("\n").length +
// length of line containing typeReference
typeReference.lineLengthWithoutNewlinePrefix()
length > maxLineLength
}
?: false
}

public val PARAMETER_LIST_SPACING_RULE_ID: RuleId = ParameterListSpacingRule().ruleId
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -479,6 +481,33 @@ class ParameterListSpacingRuleTest {
parameterListSpacingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2488 - Given a parameter with type reference which does not fit on a single line`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
class Foo(
val foooooooooooo:
Foooooooooooo,
val fooooooooooooX:
Foooooooooooo,
)
""".trimIndent()
val formattedCode =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
class Foo(
val foooooooooooo: Foooooooooooo,
val fooooooooooooX:
Foooooooooooo,
)
""".trimIndent()
parameterListSpacingRuleAssertThat(code)
.setMaxLineLength()
.hasLintViolation(3, 23, "Expected a single space")
.isFormattedAs(formattedCode)
}

private companion object {
const val TOO_MANY_SPACES = " "
}
Expand Down

0 comments on commit 6a6cc17

Please sign in to comment.