Skip to content

Commit

Permalink
Closer to a working solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMc331 committed Jun 20, 2020
1 parent 3cd1686 commit 95bf698
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.COLLECTION_LITERAL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.RPAR
Expand Down Expand Up @@ -117,13 +118,25 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
) {
child.visit { n ->
if (n.elementType == WHITE_SPACE && n.textContains('\n')) {
val isInCollectionLiteral = n.treeParent?.elementType == COLLECTION_LITERAL_EXPRESSION

// If we're inside a collection literal, let's recalculate the adjustment
// because the items inside the collection should not be subject to the same
// indentation as the brackets.
val adjustment = if (isInCollectionLiteral) {
val expectedPosition = intendedIndent.length + indentSize
expectedPosition - child.column
} else {
paramInnerIndentAdjustment
}

val split = n.text.split("\n")
(n as LeafElement).rawReplaceWithText(
split.joinToString("\n") {
if (paramInnerIndentAdjustment > 0) {
it + " ".repeat(paramInnerIndentAdjustment)
if (adjustment > 0) {
it + " ".repeat(adjustment)
} else {
it.substring(0, Math.max(it.length + paramInnerIndentAdjustment, 0))
it.substring(0, Math.max(it.length + adjustment, 0))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,49 @@ class ParameterListWrappingRuleTest {
)
}

@Test
fun testFormatPreservesIndentWithAnnotationsOnMultiLine() {
assertThat(
ParameterListWrappingRule().format(
"""
class A {
fun f(@Annotation
a: Any,
@Annotation([
"v1",
"v2"
])
b: Any,
c: Any =
false,
@Annotation d: Any) {
}
}
""".trimIndent()
)
).isEqualTo(
"""
class A {
fun f(
@Annotation
a: Any,
@Annotation(
[
"v1",
"v2"
]
)
b: Any,
c: Any =
false,
@Annotation d: Any
) {
}
}
""".trimIndent()
)
}

@Test
fun testFormatCorrectsRPARIndentIfNeeded() {
assertThat(
Expand Down

0 comments on commit 95bf698

Please sign in to comment.