Skip to content

Commit

Permalink
WhiteSpaceRule after NewlinesRule (#549)
Browse files Browse the repository at this point in the history
* WhiteSpaceRule after NewlinesRule

### What's done:
    Fixed bug and added tests
  • Loading branch information
kentr0w authored Nov 23, 2020
1 parent 0bf93cf commit a1ccdbd
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ fun foo(list: List<Bar>?) {
?.qux()
?:foobar
}

fun bar(x :Int,y:Int) = x+ y
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ fun foo(list: List<Bar>?) {
?:
foobar
}

fun bar(x :Int,y:Int) :Int {
return x+ y }
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ private fun foo(node: ASTNode) {
// this is a generated else block
}
}
val qwe = a && b
val qwe = a &&
b
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a1ccdbd

Please sign in to comment.