Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/npe-indentation-rule(#555)
Browse files Browse the repository at this point in the history
# Conflicts:
#	diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
#	diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
  • Loading branch information
aktsay6 committed Dec 1, 2020
2 parents d9d7b1b + 078b6d7 commit c797494
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 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 @@ -33,8 +33,19 @@ fun `method name incorrect, part 4`() {
}

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

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

fun bar() {
val diktatExtension = project.extensions.create(DIKTAT_EXTENSION, DiktatExtension::class.java).apply {

inputs = project.fileTree("src").apply {
include("**/*.kt")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ fun `method name incorrect, part 4`() {
}

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

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

fun bar() {
val diktatExtension = project.extensions.create(DIKTAT_EXTENSION, DiktatExtension::class.java)
diktatExtension.inputs = project.fileTree("src").apply {
include("**/*.kt")
Expand Down

0 comments on commit c797494

Please sign in to comment.