Formatter: support "else on new line" option

This commit is contained in:
Pavel V. Talanov
2014-01-17 18:34:43 +04:00
parent 97487e7238
commit 5caabc20f6
9 changed files with 122 additions and 16 deletions
@@ -141,7 +141,8 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
// "ALIGN_MULTILINE_CHAINED_METHODS",
"ALIGN_MULTILINE_PARAMETERS",
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
"ALIGN_MULTILINE_METHOD_BRACKETS"
"ALIGN_MULTILINE_METHOD_BRACKETS",
"ELSE_ON_NEW_LINE"
);
consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements");
consumer.showCustomOption(JetCodeStyleSettings.class, "ALIGN_IN_COLUMNS_CASE_BRANCH", "Align in columns 'case' branches",
@@ -186,10 +186,21 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
aroundInside(ARROW, FUNCTION_TYPE).spaceIf(jetSettings.SPACE_AROUND_FUNCTION_TYPE_ARROW)
betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1)
beforeInside(ELSE_KEYWORD, IF).spaces(1)
}
custom {
if (jetCommonSettings.ELSE_ON_NEW_LINE) {
inPosition(parent = IF, right = ELSE_KEYWORD)
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1)
}
else {
inPosition(parent = IF, left = THEN, right = ELSE_KEYWORD).customRule {
parent, left, right ->
// do not remove linebreak if "then" expression is not a block
val expressionOrBlock = left.getNode()!!.getFirstChildNode()
val keepLineBreaks = expressionOrBlock == null || expressionOrBlock.getElementType() != BLOCK
Spacing.createSpacing(1, 1, 0, keepLineBreaks, 0)
}
}
fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? {
if (block != null && block.getElementType() == blockType) {
@@ -0,0 +1,28 @@
fun f() {
if (true) {
} else if (true) {
} else {
}
if (true) {
} else if (true) {
} else {
}
if (a) 3 else 4
if (a) 5
else 5
if (a) 5
else
5
if (a)
5
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
@@ -0,0 +1,32 @@
fun f() {
if (true) {
}
else if (true) {
}
else {
}
if (true) {
}
else if (true) {
}
else {
}
if (a) 3 else 4
if (a) 5
else 5
if (a) 5
else
5
if (a)
5
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
+30
View File
@@ -0,0 +1,30 @@
fun f() {
if (true) {
}
else if (true) {
}
else {
}
if (true) {
} else if (true) {
} else {
}
if (a) 3 else 4
if (a) 5
else 5
if (a) 5
else
5
if (a)
5
else
5
}
// SET_TRUE: ELSE_ON_NEW_LINE
+5 -9
View File
@@ -7,8 +7,11 @@ fun f() {
}
if (true) {
} else {
}
else {
if (true) {
} else {
}
if (true) {
@@ -20,14 +23,7 @@ fun f() {
}
if (true) {
}
else {
}
if (true) {
}
else
} else
3 > 2
if (true) (3) else (4)
@@ -74,6 +74,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest("idea/testData/formatter/DoWhileSpacing.after.kt");
}
@TestMetadata("ElseOnNewLine.after.kt")
public void testElseOnNewLine() throws Exception {
doTest("idea/testData/formatter/ElseOnNewLine.after.kt");
}
@TestMetadata("EmptyLineAfterPackage.after.kt")
public void testEmptyLineAfterPackage() throws Exception {
doTest("idea/testData/formatter/EmptyLineAfterPackage.after.kt");
@@ -382,6 +387,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted("idea/testData/formatter/DoWhileLineBreak.after.inv.kt");
}
@TestMetadata("ElseOnNewLine.after.inv.kt")
public void testElseOnNewLine() throws Exception {
doTestInverted("idea/testData/formatter/ElseOnNewLine.after.inv.kt");
}
@TestMetadata("ForLineBreak.after.inv.kt")
public void testForLineBreak() throws Exception {
doTestInverted("idea/testData/formatter/ForLineBreak.after.inv.kt");
@@ -14,8 +14,7 @@ public class Test() {
val result = true
if (parent.isDirectory()) {
return true
}
else
} else
return false
}
}
@@ -14,8 +14,7 @@ public open class Test() {
var result: Boolean = true
if (parent?.isDirectory()!!) {
return true
}
else
} else
return false
}
}