Don't remove linebreak after smb. else's statement }

This commit is contained in:
Nikolay Krasko
2014-05-11 00:09:29 +04:00
parent 8eb8e5a928
commit 9eb2615ac3
7 changed files with 90 additions and 7 deletions
@@ -131,7 +131,11 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
} }
custom { custom {
fun CustomSpacingBuilder.ruleForKeywordOnNewLine(shouldBeOnNewLine: Boolean, keyword: IElementType, parent: IElementType) { fun CustomSpacingBuilder.ruleForKeywordOnNewLine(
shouldBeOnNewLine: Boolean,
keyword: IElementType,
parent: IElementType,
afterBlockFilter: (wordParent: ASTNode, block: ASTNode) -> Boolean = { keywordParent, block -> true }) {
if (shouldBeOnNewLine) { if (shouldBeOnNewLine) {
inPosition(parent = parent, right = keyword) inPosition(parent = parent, right = keyword)
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1, allowBlankLines = false) .lineBreakIfLineBreakInParent(numSpacesOtherwise = 1, allowBlankLines = false)
@@ -139,16 +143,27 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
else { else {
inPosition(parent = parent, right = keyword).customRule { inPosition(parent = parent, right = keyword).customRule {
parent, left, right -> parent, left, right ->
// do not remove linebreak if expression to the left is not a block
val previousNonWhitespaceLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(right.getNode()) val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(right.getNode())
val keepLineBreaks = previousNonWhitespaceLeaf == null || previousNonWhitespaceLeaf.getElementType() != RBRACE val leftBlock = if (
Spacing.createSpacing(1, 1, 0, keepLineBreaks, 0) previousLeaf != null &&
previousLeaf.getElementType() == RBRACE &&
previousLeaf.getTreeParent()?.getElementType() == BLOCK) {
previousLeaf.getTreeParent()!!
} else null
val removeLineBreaks = leftBlock != null && afterBlockFilter(right.getNode()?.getTreeParent()!!, leftBlock)
Spacing.createSpacing(1, 1, 0, !removeLineBreaks, 0)
} }
} }
} }
ruleForKeywordOnNewLine(jetCommonSettings.ELSE_ON_NEW_LINE, keyword = ELSE_KEYWORD, parent = IF) ruleForKeywordOnNewLine(jetCommonSettings.ELSE_ON_NEW_LINE, keyword = ELSE_KEYWORD, parent = IF) { keywordParent, block ->
ruleForKeywordOnNewLine(jetCommonSettings.WHILE_ON_NEW_LINE, keyword = WHILE_KEYWORD, parent = DO_WHILE) block.getTreeParent()?.getElementType() == THEN && block.getTreeParent()?.getTreeParent() == keywordParent
}
ruleForKeywordOnNewLine(jetCommonSettings.WHILE_ON_NEW_LINE, keyword = WHILE_KEYWORD, parent = DO_WHILE) { keywordParent, block ->
block.getTreeParent()?.getElementType() == BODY && block.getTreeParent()?.getTreeParent() == keywordParent
}
ruleForKeywordOnNewLine(jetCommonSettings.CATCH_ON_NEW_LINE, keyword = CATCH, parent = TRY) ruleForKeywordOnNewLine(jetCommonSettings.CATCH_ON_NEW_LINE, keyword = CATCH, parent = TRY)
ruleForKeywordOnNewLine(jetCommonSettings.FINALLY_ON_NEW_LINE, keyword = FINALLY, parent = TRY) ruleForKeywordOnNewLine(jetCommonSettings.FINALLY_ON_NEW_LINE, keyword = FINALLY, parent = TRY)
@@ -28,6 +28,22 @@ fun f() {
5 5
else else
5 5
if (true)
if (true) {
4
} else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
} }
// SET_TRUE: ELSE_ON_NEW_LINE // SET_TRUE: ELSE_ON_NEW_LINE
@@ -34,6 +34,23 @@ fun f() {
5 5
else else
5 5
if (true)
if (true) {
4
}
else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
} }
// SET_TRUE: ELSE_ON_NEW_LINE // SET_TRUE: ELSE_ON_NEW_LINE
+16
View File
@@ -34,6 +34,22 @@ fun f() {
5 5
else else
5 5
if (true)
if (true) {
4
} else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
} }
// SET_TRUE: ELSE_ON_NEW_LINE // SET_TRUE: ELSE_ON_NEW_LINE
@@ -17,6 +17,12 @@ fun main(args: Array<String>) {
while (true) //blabla while (true) //blabla
{ {
} }
do
if (true) {
}
while (true)
} }
// SET_TRUE: LBRACE_ON_NEXT_LINE // SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -21,6 +21,13 @@ fun main(args: Array<String>)
while (true) //blabla while (true) //blabla
{ {
} }
do
if (true)
{
}
while (true)
} }
// SET_TRUE: LBRACE_ON_NEXT_LINE // SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -21,6 +21,12 @@ fun main(args: Array<String>) {
while (true) //blabla while (true) //blabla
{ {
} }
do
if (true) {
}
while (true)
} }
// SET_TRUE: LBRACE_ON_NEXT_LINE // SET_TRUE: LBRACE_ON_NEXT_LINE