Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentListWrappingRule: don't report it when line breaks is inside a lambda #928

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
// skip lambda arguments
node.treeParent?.elementType != ElementType.FUNCTION_LITERAL &&
// skip if number of arguments is big (we assume it with a magic number of 8)
node.children().filter { it.elementType == ElementType.VALUE_ARGUMENT }.toList().size <= 8
node.children().count { it.elementType == ElementType.VALUE_ARGUMENT } <= 8
) {
// each argument should be on a separate line if
// - at least one of the arguments is
Expand Down Expand Up @@ -182,10 +182,12 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
}

private fun ASTNode.textContainsIgnoringLambda(char: Char): Boolean {
return children()
.flatMap { if (it.elementType == ElementType.VALUE_ARGUMENT) it.children() else sequenceOf(it) }
.filter { it.elementType != ElementType.LAMBDA_EXPRESSION }
.any { it.textContains(char) }
return children().any { child ->
val elementType = child.elementType
elementType == ElementType.WHITE_SPACE && child.textContains(char) ||
elementType == ElementType.COLLECTION_LITERAL_EXPRESSION && child.textContains(char) ||
elementType == ElementType.VALUE_ARGUMENT && child.children().any { it.textContainsIgnoringLambda(char) }
}
}

private fun ASTNode.hasTypeParameterListInFront(): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@ class ArgumentListWrappingRuleTest {
).isEmpty()
}

@Test
fun testLambdaArgumentsAreIgnored2() {
assertThat(
ArgumentListWrappingRule().lint(
"""
fun main() {
foo(bar.apply {
// stuff
})
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun testLambdaArgumentsAreIgnored3() {
assertThat(
ArgumentListWrappingRule().lint(
"""
fun main() {
println(Runnable {
println("hello")
println("world")
})
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun testFormatWithLambdaArguments() {
assertThat(
Expand Down