Skip to content

Commit

Permalink
Bugfix. Lambda passed as an argument is forced to be on the same line…
Browse files Browse the repository at this point in the history
… as the previous argument (#545)

* bugfix/lambda-forced-to-be-on-same-line(#490)

### What's done:
  * Fixed bugs
  • Loading branch information
aktsay6 authored Dec 1, 2020
1 parent fefe2fd commit 078b6d7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class WhiteSpaceRule(private val configRules: List<RulesConfig>) : Rule("horizon
/**
* This method covers all other opening braces, not covered in [handleKeywordWithParOrBrace].
*/
@Suppress("UnsafeCallOnNullableType")
@Suppress("UnsafeCallOnNullableType", "ComplexMethod")
private fun handleLbrace(node: ASTNode) {
// `{` can't be the very first symbol in the file, so `!!` should be safe
val whitespaceOrPrevNode = node.selfOrParentsTreePrev()!!
Expand All @@ -188,16 +188,20 @@ class WhiteSpaceRule(private val configRules: List<RulesConfig>) : Rule("horizon
it[1].elementType == LAMBDA_EXPRESSION &&
it[2].elementType == VALUE_ARGUMENT &&
// lambda is not passed as a named argument
!it[2].hasChildOfType(EQ) &&
// lambda is the first argument in the list
it[2].prevSibling { prevNode -> prevNode.elementType == COMMA } == null
!it[2].hasChildOfType(EQ)
}
?: false

val prevNode = whitespaceOrPrevNode.let { if (it.elementType == WHITE_SPACE) it.treePrev else it }
val numWhiteSpace = whitespaceOrPrevNode.numWhiteSpaces()
// note: the conditions in the following `if`s cannot be collapsed into simple conjunctions
if (isFromLambdaAsArgument) {
if (numWhiteSpace != 0) {
val isFirstArgument = node
.parent({ it.elementType == VALUE_ARGUMENT })
.let { it?.prevSibling { prevNode -> prevNode.elementType == COMMA } == null }

// If it is lambda, then we don't force it to be on newline or same line
if (numWhiteSpace != 0 && isFirstArgument) {
WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be no whitespace before '{' of lambda" +
" inside argument list", node.startOffset, node) {
whitespaceOrPrevNode.treeParent.removeChild(whitespaceOrPrevNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ return x+ y}
bar(x,y)
)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ fun `method name incorrect, part 4`() {
.foo()
}

fun foo() {
foo(
0,
{ obj -> obj.bar() }
)

bar(
0, { obj -> obj.bar() }
)
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ fun `method name incorrect, part 4`() {
.foo()
}

fun foo() {
foo(
0,
{ obj -> obj.bar() }
)

bar(
0, { obj -> obj.bar() }
)
}

0 comments on commit 078b6d7

Please sign in to comment.