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 {
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) {
inPosition(parent = parent, right = keyword)
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1, allowBlankLines = false)
@@ -139,16 +143,27 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
else {
inPosition(parent = parent, right = keyword).customRule {
parent, left, right ->
// do not remove linebreak if expression to the left is not a block
val previousNonWhitespaceLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(right.getNode())
val keepLineBreaks = previousNonWhitespaceLeaf == null || previousNonWhitespaceLeaf.getElementType() != RBRACE
Spacing.createSpacing(1, 1, 0, keepLineBreaks, 0)
val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(right.getNode())
val leftBlock = if (
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.WHILE_ON_NEW_LINE, keyword = WHILE_KEYWORD, parent = DO_WHILE)
ruleForKeywordOnNewLine(jetCommonSettings.ELSE_ON_NEW_LINE, keyword = ELSE_KEYWORD, parent = IF) { keywordParent, block ->
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.FINALLY_ON_NEW_LINE, keyword = FINALLY, parent = TRY)
@@ -28,6 +28,22 @@ fun f() {
5
else
5
if (true)
if (true) {
4
} else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
@@ -34,6 +34,23 @@ fun f() {
5
else
5
if (true)
if (true) {
4
}
else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
+16
View File
@@ -34,6 +34,22 @@ fun f() {
5
else
5
if (true)
if (true) {
4
} else {
4
}
else
5
if (true)
while (true) {
4
}
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
@@ -17,6 +17,12 @@ fun main(args: Array<String>) {
while (true) //blabla
{
}
do
if (true) {
}
while (true)
}
// SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -21,6 +21,13 @@ fun main(args: Array<String>)
while (true) //blabla
{
}
do
if (true)
{
}
while (true)
}
// SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -21,6 +21,12 @@ fun main(args: Array<String>) {
while (true) //blabla
{
}
do
if (true) {
}
while (true)
}
// SET_TRUE: LBRACE_ON_NEXT_LINE