Repository: analysis-dev/diktat (kotlin)

Issue:
WhiteSpaceRule should be applied after NewlinesRule
For example,
```kotlin
fun foo() {
    a
    && b
}
```
->
```kotlin
fun foo() {
    a&&
            b
}
```

with diktat 0.1.3

Pull request:
### What's done:
   Changed rules
   closes #484

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt
index 7148055352..14fae1bc4d 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt
@@ -107,10 +107,10 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy
                 ::ExtensionFunctionsSameNameRule,
                 // formatting: moving blocks, adding line breaks, indentations etc.
                 ::ConsecutiveSpacesRule,
-                ::WhiteSpaceRule,  // this rule should be after other rules that can cause wrong spacing
                 ::HeaderCommentRule,
                 ::FileStructureRule,  // this rule should be right before indentation because it should operate on already valid code
                 ::NewlinesRule,  // newlines need to be inserted right before fixing indentation
+                ::WhiteSpaceRule,  // this rule should be after other rules that can cause wrong spacing
                 ::IndentationRule  // indentation rule should be the last because it fixes formatting after all the changes done by previous rules
         )
                 .map {
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/NewlinesRule.kt
index 64c8c1cc04..2b54696df0 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/NewlinesRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/NewlinesRule.kt
@@ -297,7 +297,7 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
                         val colon = funNode.findChildByType(COLON)!!
                         val expression = node.findChildByType(RETURN_KEYWORD)!!.nextCodeSibling()!!
                         funNode.apply {
-                            removeRange(colon, null)
+                            removeRange(if (colon.treePrev.elementType == WHITE_SPACE) colon.treePrev else colon, null)
                             addChild(PsiWhiteSpaceImpl(" "), null)
                             addChild(LeafPsiElement(EQ, "="), null)
                             addChild(PsiWhiteSpaceImpl(" "), null)


Test patch:
diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleExpected.kt
index 94641ee4b1..e7eccb61dc 100644
--- a/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleExpected.kt
+++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleExpected.kt
@@ -10,3 +10,5 @@ fun foo(list: List<Bar>?) {
 ?.qux()
             ?:foobar
 }
+
+fun bar(x :Int,y:Int) = x+ y
diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleTest.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleTest.kt
index cf029827e2..4315b7dadc 100644
--- a/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleTest.kt
+++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/FunctionalStyleTest.kt
@@ -7,3 +7,6 @@ fun foo(list: List<Bar>?) {
             ?:
             foobar
 }
+
+fun bar(x :Int,y:Int) :Int {
+    return   x+ y }
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt
index 4f6be67d67..b95f7d0413 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Expected.kt
@@ -24,5 +24,8 @@ private fun foo(node: ASTNode) {
             // this is a generated else block
         }
     }
+    val qwe = a && b
+    val qwe = a &&
+            b
 }
 
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt
index cd34b64f85..47c6406a26 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example2Test.kt
@@ -15,4 +15,8 @@ private fun foo (node: ASTNode) {
     when (node.elementType) {
         CLASS, FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> checkAnnotation(node)
     }
+    val qwe = a
+            && b
+    val qwe = a &&
+            b
 }
\ No newline at end of file