Repository: analysis-dev/diktat (kotlin)

Issue:
CompactInitializationRule builds incorrect tree
## Describe the bug
``` kotlin
fun foo() {
    val diktatExtension = project.extensions.create(DIKTAT_EXTENSION, DiktatExtension::class.java)
    diktatExtension.inputs = project.fileTree("src").apply {
        include("**/*.kt")
    }
    diktatExtension.reporter = PlainReporter(System.out)
}
```
This rule builds incorrect tree. In `FUNCTION_LITERAL` there is no `WHITE_SPACE` after `LBRACE`
## Environment information
* diktat version: 0.1.4(same for 0.1.5)

Pull request:
### What's done:
  * Fixed bug

This pull request closes #599

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/CompactInitialization.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/CompactInitialization.kt
index a89b813fae..3a21df2582 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/CompactInitialization.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/classes/CompactInitialization.kt
@@ -8,6 +8,8 @@ import org.cqfn.diktat.ruleset.utils.getFunctionName
 import org.cqfn.diktat.ruleset.utils.log
 
 import com.pinterest.ktlint.core.Rule
+import com.pinterest.ktlint.core.ast.ElementType.LBRACE
+import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
 import com.pinterest.ktlint.core.ast.isPartOfComment
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
@@ -102,8 +104,13 @@ class CompactInitialization(private val configRules: List<RulesConfig>) : Rule("
                         .takeWhile { it != property.node }
                         .toList()
                         .reversed()
-                        .forEach {
-                            bodyExpression.addChild(it.clone() as ASTNode, null)
+                        .forEachIndexed { index, it ->
+                            // adds whiteSpace to functional literal if previous of bodyExpression is LBRACE
+                            if (index == 0 && bodyExpression.treePrev.elementType == LBRACE && it.elementType == WHITE_SPACE) {
+                                bodyExpression.treeParent.addChild(it.clone() as ASTNode, bodyExpression)
+                            } else {
+                                bodyExpression.addChild(it.clone() as ASTNode, null)
+                            }
                             it.treeParent.removeChild(it)
                         }
                     // strip receiver name and move assignment itself into `apply`


Test patch:
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
index d36c67c2c7..ad4ccf8a9f 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
@@ -43,3 +43,11 @@ fun foo() {
     )
 }
 
+fun bar() {
+    val diktatExtension = project.extensions.create(DIKTAT_EXTENSION, DiktatExtension::class.java).apply {
+        inputs = project.fileTree("src").apply {
+            include("**/*.kt")
+        }
+        reporter = PlainReporter(System.out)}
+}
+
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
index 6a66c874c0..487190f619 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
@@ -23,11 +23,11 @@ fun `method name incorrect, part 4`() {
     lintMethod(code, LintError(2, 7, ruleId, "${FUNCTION_NAME_INCORRECT_CASE.warnText()} methODTREE", true))
 
     foo
-                // we are calling bar
+            // we are calling bar
             .bar()
 
     bar
-                /* This is a block comment */
+            /* This is a block comment */
             .foo()
 }
 
@@ -42,3 +42,10 @@ fun foo() {
     )
 }
 
+fun bar() {
+    val diktatExtension = project.extensions.create(DIKTAT_EXTENSION, DiktatExtension::class.java)
+    diktatExtension.inputs = project.fileTree("src").apply {
+        include("**/*.kt")
+    }
+    diktatExtension.reporter = PlainReporter(System.out)
+}
\ No newline at end of file