IDEA-136400 Add java style support for empty array initializer with no space between braces

This commit is contained in:
David Herman
2015-03-04 11:34:29 +03:00
committed by Yaroslav Lepenkin
parent c3b5810acc
commit 0d7819283f
7 changed files with 44 additions and 4 deletions
@@ -301,6 +301,7 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
"@SuppressWarnings({\"ALL\"})\n" +
"public class Foo<T extends Bar & Abba, U> {\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" +
@@ -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);
}
}
@@ -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" + "}");
@@ -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 {
@@ -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;
/**
@@ -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(
@@ -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
code.style.indents.detector.accept=OK