Skip to content

Commit

Permalink
ParameterListWrappingRule: correctly indent primary constructor param…
Browse files Browse the repository at this point in the history
…eters when class has multiline type parameter (pinterest#950)

* ParameterListWrappingRule: correctly indent primary constructor parameters when class has multiline type parameter

* Delete a comment
  • Loading branch information
t-kameyama authored Oct 25, 2020
1 parent 6ae2726 commit b542a81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER
Expand All @@ -15,6 +16,7 @@ import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.lineIndent
import com.pinterest.ktlint.core.ast.prevLeaf
import com.pinterest.ktlint.core.ast.prevSibling
import com.pinterest.ktlint.core.ast.visit
import kotlin.math.max
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -161,9 +163,13 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
else -> throw UnsupportedOperationException()
}

private fun ASTNode.hasTypeParameterListInFront(): Boolean =
treeParent.children()
.firstOrNull { it.elementType == TYPE_PARAMETER_LIST }
?.children()
?.any { it.isWhiteSpaceWithNewline() } == true
private fun ASTNode.hasTypeParameterListInFront(): Boolean {
val parent = this.treeParent
val typeParameterList = if (parent.elementType == PRIMARY_CONSTRUCTOR) {
parent.prevSibling { it.elementType == TYPE_PARAMETER_LIST }
} else {
parent.children().firstOrNull { it.elementType == TYPE_PARAMETER_LIST }
} ?: return false
return typeParameterList.children().any { it.isWhiteSpaceWithNewline() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,22 @@ class ParameterListWrappingRuleTest {
assertThat(ParameterListWrappingRule().lint(code)).isEmpty()
assertThat(ParameterListWrappingRule().format(code)).isEqualTo(code)
}

// https://github.com/pinterest/ktlint/issues/921
@Test
fun `correctly indent primary constructor parameters when class has multiline type parameter`() {
assertThat(
ParameterListWrappingRule().lint(
"""
class ComposableLambda<
P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16,
P17, P18, R>(
val key: Int,
private val tracked: Boolean,
private val sourceInformation: String?
)
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit b542a81

Please # to comment.