From 0d7819283f747a95104ecb39e7e8008ca8d17196 Mon Sep 17 00:00:00 2001 From: David Herman Date: Wed, 4 Mar 2015 11:34:29 +0300 Subject: [PATCH] IDEA-136400 Add java style support for empty array initializer with no space between braces --- .../JavaLanguageCodeStyleSettingsProvider.java | 2 ++ .../java/JavaSpacePropertyProcessor.java | 13 +++++++++++-- .../psi/formatter/java/JavaFormatterTest.java | 18 ++++++++++++++++++ .../CodeStyleSettingsCustomizable.java | 3 ++- .../psi/codeStyle/CommonCodeStyleSettings.java | 7 +++++++ .../codeStyle/CodeStyleSpacesPanel.java | 2 ++ .../src/messages/ApplicationBundle.properties | 3 ++- 7 files changed, 44 insertions(+), 4 deletions(-) diff --git a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java index 80bda49e697d..5a85a68ab396 100644 --- a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java +++ b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java @@ -301,6 +301,7 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett "@SuppressWarnings({\"ALL\"})\n" + "public class Foo {\n" + " int[] X = new int[]{1, 3, 5, 6, 7, 87, 1213, 2};\n" + + " int[] empty = new int[]{};" + "\n" + " public void foo(int x, int y) {\n" + " Runnable r = () -> {};\n" + @@ -357,6 +358,7 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett " int\n" + "i = 0;\n" + " int[] a = new int[] {1, 2, 0x0052, 0x0053, 0x0054};\n" + + " int[] empty = new int[] {};\n" + " int var1 = 1; int var2 = 2;\n" + " foo1(0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057);\n" + " int x = (3 + 4 + 5 + 6) * (7 + 8 + 9 + 10) * (11 + 12 + 13 + 14 + 0xFFFFFFFF);\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 821460ad9343..e5ee97a42d94 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 @@ -1589,12 +1589,21 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { private void visitArrayInitializer() { if (myRole1 == ChildRole.LBRACE) { if (mySettings.ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE) { - int spaces = mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES ? 1 : 0; + int spaces; + if (myRole2 != ChildRole.RBRACE) { + spaces = mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES ? 1 : 0; + } + else { + spaces = mySettings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES ? 1 : 0; + } myResult = Spacing.createDependentLFSpacing(spaces, spaces, myParent.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } else { - createSpaceProperty(mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES, mySettings.KEEP_BLANK_LINES_IN_CODE); + boolean addSpace = (myRole2 != ChildRole.RBRACE) + ? mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES + : mySettings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES; + createSpaceProperty(addSpace, mySettings.KEEP_BLANK_LINES_IN_CODE); } } diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java index c836164a06a7..f14615280fed 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java @@ -529,6 +529,24 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest { "class a {\n" + " void f() {\n" + " final int[] i = new int[] { 0 };\n" + " }\n" + "}"); } + public void testEmptyArray() throws Exception { + final CommonCodeStyleSettings settings = getSettings(); + settings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES = true; + settings.SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE = true; + settings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false; + doTextTest("class a {\n" + " void f() {\n" + " final int[] i = new int[]{ };\n" + " }\n" + "}", + "class a {\n" + " void f() {\n" + " final int[] i = new int[] {};\n" + " }\n" + "}"); + } + + public void testEmptyArrayIsntWrapped() throws Exception { + final CommonCodeStyleSettings settings = getSettings(); + settings.ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = true; + settings.ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE = true; + settings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = true; + doTextTest("class a {\n" + " void f() {\n" + " final int[] i = new int[]{ };\n" + " }\n" + "}", + "class a {\n" + " void f() {\n" + " final int[] i = new int[]{ };\n" + " }\n" + "}"); + } + public void testTwoJavaDocs() throws Exception { doTextTest("/**\n" + " * \n" + " */\n" + " class Test {\n" + " /**\n" + " */\n" + " public void foo();\n" + "}", "/**\n" + " *\n" + " */\n" + "class Test {\n" + " /**\n" + " */\n" + " public void foo();\n" + "}"); diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsCustomizable.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsCustomizable.java index 6781dcfef1bf..0f52d23a5c14 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsCustomizable.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsCustomizable.java @@ -89,7 +89,8 @@ public interface CodeStyleSettingsCustomizable { SPACE_BEFORE_ANOTATION_PARAMETER_LIST, SPACE_WITHIN_ANNOTATION_PARENTHESES, SPACE_WITHIN_EMPTY_METHOD_CALL_PARENTHESES, - SPACE_WITHIN_EMPTY_METHOD_PARENTHESES + SPACE_WITHIN_EMPTY_METHOD_PARENTHESES, + SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES, } enum BlankLinesOption { 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 1e7bc6a1c237..4197c1359171 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java @@ -632,6 +632,13 @@ public class CommonCodeStyleSettings { */ public boolean SPACE_WITHIN_ARRAY_INITIALIZER_BRACES = false; + /** + * "int X[] { }" + * or + * "int X[] {}" + */ + public boolean SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false; + public boolean SPACE_AFTER_TYPE_CAST = true; /** diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSpacesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSpacesPanel.java index 51c946b03e33..be4ef0ddd131 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSpacesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSpacesPanel.java @@ -82,6 +82,8 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel { initBooleanField("SPACE_WITHIN_BRACES", ApplicationBundle.message("checkbox.spaces.within.braces"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_BRACKETS", ApplicationBundle.message("checkbox.spaces.within.brackets"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_ARRAY_INITIALIZER_BRACES", ApplicationBundle.message("checkbox.spaces.within.array.initializer.braces"), SPACES_WITHIN); + initBooleanField("SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES", ApplicationBundle.message("checkbox.spaces.within.empty.array.initializer.braces"), SPACES_WITHIN); + initBooleanField("SPACE_WITHIN_PARENTHESES", ApplicationBundle.message("checkbox.spaces.within.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_METHOD_CALL_PARENTHESES", ApplicationBundle.message("checkbox.spaces.checkbox.spaces.method.call.parentheses"), SPACES_WITHIN); initBooleanField( diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index e3916e2b8327..ae135a02a1b7 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -301,6 +301,7 @@ checkbox.spaces.finally.keyword='finally' keyword checkbox.spaces.within.brackets=Brackets checkbox.spaces.within.braces=Code braces checkbox.spaces.within.array.initializer.braces=Array initializer braces +checkbox.spaces.within.empty.array.initializer.braces=Empty array initializer braces checkbox.spaces.within.parentheses=Grouping parentheses checkbox.spaces.checkbox.spaces.method.call.parentheses=Method call parentheses checkbox.spaces.checkbox.spaces.empty.method.call.parentheses=Empty method call parentheses @@ -695,4 +696,4 @@ code.style.detector.use.spaces=Indent with {0} spaces code.style.detector.use.tabs=Indent with tab code.style.indents.detector.show.settings=Show Settings -code.style.indents.detector.accept=OK \ No newline at end of file +code.style.indents.detector.accept=OK