diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java index fb3c93cba6a..9cf68e32449 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java @@ -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", diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt b/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt index 0fac52037ce..6cfdc0620e0 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt @@ -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) { diff --git a/idea/testData/formatter/ElseOnNewLine.after.inv.kt b/idea/testData/formatter/ElseOnNewLine.after.inv.kt new file mode 100644 index 00000000000..ca818120398 --- /dev/null +++ b/idea/testData/formatter/ElseOnNewLine.after.inv.kt @@ -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 \ No newline at end of file diff --git a/idea/testData/formatter/ElseOnNewLine.after.kt b/idea/testData/formatter/ElseOnNewLine.after.kt new file mode 100644 index 00000000000..d2d7b24c5d0 --- /dev/null +++ b/idea/testData/formatter/ElseOnNewLine.after.kt @@ -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 \ No newline at end of file diff --git a/idea/testData/formatter/ElseOnNewLine.kt b/idea/testData/formatter/ElseOnNewLine.kt new file mode 100644 index 00000000000..3efd2786fbf --- /dev/null +++ b/idea/testData/formatter/ElseOnNewLine.kt @@ -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 \ No newline at end of file diff --git a/idea/testData/formatter/IfSpacing.after.kt b/idea/testData/formatter/IfSpacing.after.kt index 92d5b513672..2570789bcdb 100644 --- a/idea/testData/formatter/IfSpacing.after.kt +++ b/idea/testData/formatter/IfSpacing.after.kt @@ -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) diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index f477e26546c..77fa36cc376 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -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"); diff --git a/j2k/tests/testData/ast/issues/kt-824-isDir.ide.kt b/j2k/tests/testData/ast/issues/kt-824-isDir.ide.kt index a9f9714ed46..5acf781d134 100644 --- a/j2k/tests/testData/ast/issues/kt-824-isDir.ide.kt +++ b/j2k/tests/testData/ast/issues/kt-824-isDir.ide.kt @@ -14,8 +14,7 @@ public class Test() { val result = true if (parent.isDirectory()) { return true - } - else + } else return false } } diff --git a/j2k/tests/testData/ast/issues/kt-824-isDir.kt b/j2k/tests/testData/ast/issues/kt-824-isDir.kt index 9a162b3cd0c..f4fbc7809ce 100644 --- a/j2k/tests/testData/ast/issues/kt-824-isDir.kt +++ b/j2k/tests/testData/ast/issues/kt-824-isDir.kt @@ -14,8 +14,7 @@ public open class Test() { var result: Boolean = true if (parent?.isDirectory()!!) { return true - } - else + } else return false } }