diff --git a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java index 3270b6371633..a9d98ce7212a 100644 --- a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java +++ b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java @@ -550,9 +550,7 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett " while (x < 50000) x++;\n" + " do x++; while (x < 10000);\n" + " switch (a) {\n" + - " case 0:\n" + - " doCase0();\n" + - " break;\n" + + " case 0: case 1:\ndoCase0(); break;\ncase 2: case 3: return;" + " default:\n" + " doDefault();\n" + " }\n" + diff --git a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java index 9872c530782f..6af0433cff19 100644 --- a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java +++ b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java @@ -908,12 +908,13 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { myResult = Spacing.createDependentLFSpacing(0, 1, textRange, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_BEFORE_RBRACE); } } - else if (myChild1.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT) { + else if (myChild1.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT || + myChild2.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT) { if (myChild2.getElementType() == JavaElementType.BLOCK_STATEMENT) { myResult = getSpaceBeforeLBrace(myChild2, mySettings.SPACE_BEFORE_SWITCH_LBRACE, null); } else { - int lineFeeds = mySettings.CASE_STATEMENT_ON_NEW_LINE ? 1 : 0; + int lineFeeds = myChild1.getElementType() != JavaElementType.SWITCH_LABEL_STATEMENT || mySettings.CASE_STATEMENT_ON_NEW_LINE ? 1 : 0; myResult = Spacing.createSpacing(1, 1, lineFeeds, true, mySettings.KEEP_BLANK_LINES_IN_CODE); } } diff --git a/java/java-tests/testData/codeStyle/jsonExport.j2ee.json b/java/java-tests/testData/codeStyle/jsonExport.j2ee.json index fc0e8a53305d..7225ca707087 100644 --- a/java/java-tests/testData/codeStyle/jsonExport.j2ee.json +++ b/java/java-tests/testData/codeStyle/jsonExport.j2ee.json @@ -50,7 +50,7 @@ "call_parameters_new_line_after_left_paren": false, "call_parameters_right_paren_on_new_line": false, "call_parameters_wrap": "on_every_item", - "case_statement_on_new_line": true, + "case_statement_on_separate_line": true,, "catch_on_new_line": false, "class_annotation_wrap": "split_into_lines", "class_brace_style": "end_of_line", diff --git a/java/java-tests/testData/codeStyle/jsonExport.json b/java/java-tests/testData/codeStyle/jsonExport.json index 282b8da738c2..18988c49c776 100644 --- a/java/java-tests/testData/codeStyle/jsonExport.json +++ b/java/java-tests/testData/codeStyle/jsonExport.json @@ -50,7 +50,7 @@ "call_parameters_new_line_after_left_paren": false, "call_parameters_right_paren_on_new_line": false, "call_parameters_wrap": "on_every_item", - "case_statement_on_new_line": true, + "case_statement_on_separate_line": true, "catch_on_new_line": false, "class_annotation_wrap": "split_into_lines", "class_brace_style": "end_of_line", diff --git a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaFormatterTest.kt b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaFormatterTest.kt index 170905836c81..3c27e0b67326 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaFormatterTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaFormatterTest.kt @@ -3758,4 +3758,78 @@ public enum LevelCode { } + fun testIdea195707() { + getSettings().apply { + CASE_STATEMENT_ON_NEW_LINE = true + KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE = true + } + + doTextTest( + """ + public enum RotationDirection { + CLOCKWISE, COUNTERCLOCKWISE; + + public RotationDirection inverse() { + switch(this) { + case CLOCKWISE: return COUNTERCLOCKWISE; case COUNTERCLOCKWISE: return CLOCKWISE; + } + throw new IllegalArgumentException("Unknown " + getClass().getSimpleName() + ": " + this); + } + } + """.trimIndent(), + + """ + public enum RotationDirection { + CLOCKWISE, COUNTERCLOCKWISE; + + public RotationDirection inverse() { + switch (this) { + case CLOCKWISE: + return COUNTERCLOCKWISE; + case COUNTERCLOCKWISE: + return CLOCKWISE; + } + throw new IllegalArgumentException("Unknown " + getClass().getSimpleName() + ": " + this); + } + } + """.trimIndent() + ) + } + + fun testIdea195707_1() { + getSettings().apply { + CASE_STATEMENT_ON_NEW_LINE = false + KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE = true + } + + doTextTest( + """ + public enum RotationDirection { + CLOCKWISE, COUNTERCLOCKWISE; + + public RotationDirection inverse() { + switch(this) { + case CLOCKWISE: return COUNTERCLOCKWISE; case COUNTERCLOCKWISE: return CLOCKWISE; + } + throw new IllegalArgumentException("Unknown " + getClass().getSimpleName() + ": " + this); + } + } + """.trimIndent(), + + """ + public enum RotationDirection { + CLOCKWISE, COUNTERCLOCKWISE; + + public RotationDirection inverse() { + switch (this) { + case CLOCKWISE: return COUNTERCLOCKWISE; + case COUNTERCLOCKWISE: return CLOCKWISE; + } + throw new IllegalArgumentException("Unknown " + getClass().getSimpleName() + ": " + this); + } + } + """.trimIndent() + ) + } + } \ No newline at end of file diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java index 6dc53bfc5c91..d67b7fca3574 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java @@ -394,7 +394,8 @@ public class CommonCodeStyleSettings { public boolean FINALLY_ON_NEW_LINE = false; public boolean INDENT_CASE_FROM_SWITCH = true; - + + @Property(externalName = "case_statement_on_separate_line") public boolean CASE_STATEMENT_ON_NEW_LINE = true; /** diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index 6c4b6a7de868..6fb94c0f9c19 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -196,7 +196,7 @@ wrapping.method.parentheses=Method parentheses wrapping.special.else.if.braces.treatment=Special 'else if' treatment wrapping.indent.case.from.switch=Indent 'case' branches wrapping.indent.break.from.case=Indent 'break' from 'case' -wrapping.case.statements.on.one.line='case' on new line +wrapping.case.statements.on.one.line=Each 'case' on a separate line wrapping.force.braces=Force braces wrapping.method.parameters=Method declaration parameters