diff --git a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java index b8b3d5c72e65..254b4ad7b92f 100644 --- a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java +++ b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java @@ -164,7 +164,7 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett " y += (y ^ 0x123) << 2;\n" + " }\n" + " do {\n" + - " try {\n" + + " try (MyResource r1 = getResource(); MyResource r2 = null) {\n" + " if (0 < x && x < 10) {\n" + " while (x != y) {\n" + " x = f(x * 3 + 5);\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 43bfa7c30a56..0ad210a91ac9 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 @@ -614,10 +614,17 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { return; } - if (myRole2 == ChildRole.TRY_BLOCK || myRole2 == ChildRole.FINALLY_BLOCK) { - boolean useSpaceBeforeLBrace = myRole2 == ChildRole.TRY_BLOCK ? mySettings.SPACE_BEFORE_TRY_LBRACE - : mySettings.SPACE_BEFORE_FINALLY_LBRACE; - myResult = getSpaceBeforeLBrace(useSpaceBeforeLBrace, mySettings.BRACE_STYLE, null, mySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE, true); + if (myRole2 == ChildRole.TRY_BLOCK) { + myResult = getSpaceBeforeLBrace(mySettings.SPACE_BEFORE_TRY_LBRACE, + mySettings.BRACE_STYLE, null, mySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE, true); + } + else if (myRole2 == ChildRole.FINALLY_BLOCK) { + myResult = getSpaceBeforeLBrace(mySettings.SPACE_BEFORE_FINALLY_LBRACE, + mySettings.BRACE_STYLE, null, mySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE, true); + } + else if (myType2 == JavaElementType.RESOURCE_LIST) { + myResult = getSpaceBeforeLBrace(mySettings.SPACE_BEFORE_TRY_PARENTHESES, + mySettings.BRACE_STYLE, null, mySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE, true); } } @@ -1211,6 +1218,20 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { } + @Override + public void visitResourceList(final PsiResourceList resourceList) { + if (myType1 == JavaTokenType.LPARENTH || myType2 == JavaTokenType.RPARENTH) { + createSpaceInCode(mySettings.SPACE_WITHIN_TRY_PARENTHESES); + } + + if (myType1 == JavaTokenType.SEMICOLON) { + createSpaceInCode(mySettings.SPACE_AFTER_SEMICOLON); + } + if (myType2 == JavaTokenType.SEMICOLON) { + createSpaceInCode(mySettings.SPACE_BEFORE_SEMICOLON); + } + } + @Override public void visitReferenceParameterList(PsiReferenceParameterList list) { if (myRole1 == ChildRole.LT_IN_TYPE_LIST && myRole2 == ChildRole.TYPE_IN_REFERENCE_PARAMETER_LIST) { createSpaceInCode(false); @@ -1227,8 +1248,6 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { else if (myRole2 == ChildRole.GT_IN_TYPE_LIST) { createSpaceInCode(false); } - - } @Override public void visitTypeCastExpression(PsiTypeCastExpression expression) { diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterSpaceTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterSpaceTest.java index b09a1e229359..02c1c38c38a7 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterSpaceTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterSpaceTest.java @@ -25,6 +25,11 @@ import com.intellij.pom.java.LanguageLevel; * @since Apr 29, 2010 5:50:34 PM */ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest { + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7); + } public void testSpacingBetweenTypeParameters() throws Exception { // Implied by IDEADEV-3666 @@ -279,7 +284,7 @@ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest { public void testEmptyIterationAtFor() throws Exception { // Inspired by IDEA-58293 - + getSettings().SPACE_AFTER_SEMICOLON = true; getSettings().SPACE_WITHIN_FOR_PARENTHESES = false; @@ -290,7 +295,6 @@ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest { } public void testSpacesInDisjunctiveType() throws Exception { - LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7); getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; getSettings().CATCH_ON_NEW_LINE = false; @@ -302,4 +306,62 @@ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest { doMethodTest("try { } catch (E1 | E2 e) { }", "try { } catch (E1|E2 e) { }"); } + + public void testSpacesBeforeResourceList() throws Exception { + getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; + + getSettings().SPACE_BEFORE_TRY_PARENTHESES = true; + getSettings().SPACE_BEFORE_TRY_LBRACE = true; + doMethodTest("try(AutoCloseable r = null){ }", + "try (AutoCloseable r = null) { }"); + + getSettings().SPACE_BEFORE_TRY_PARENTHESES = false; + getSettings().SPACE_BEFORE_TRY_LBRACE = false; + doMethodTest("try (AutoCloseable r = null) { }", + "try(AutoCloseable r = null){ }"); + } + + public void testSpacesWithinResourceList() throws Exception { + getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; + + getSettings().SPACE_WITHIN_TRY_PARENTHESES = false; + doMethodTest("try ( R r = null ) { }", + "try (R r = null) { }"); + getSettings().SPACE_AFTER_SEMICOLON = false; + doMethodTest("try ( R r1 = null ; R r2 = null; ) { }", + "try (R r1 = null;R r2 = null;) { }"); + + getSettings().SPACE_WITHIN_TRY_PARENTHESES = true; + doMethodTest("try (R r = null) { }", + "try ( R r = null ) { }"); + getSettings().SPACE_AFTER_SEMICOLON = true; + doMethodTest("try (R r1 = null ; R r2 = null;) { }", + "try ( R r1 = null; R r2 = null; ) { }"); + } + + public void testSpacesBetweenResources() throws Exception { + getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; + + getSettings().SPACE_BEFORE_SEMICOLON = false; + getSettings().SPACE_AFTER_SEMICOLON = true; + doMethodTest("try (R r1 = null ; R r2 = null;) { }", + "try (R r1 = null; R r2 = null; ) { }"); + + getSettings().SPACE_BEFORE_SEMICOLON = true; + getSettings().SPACE_AFTER_SEMICOLON = false; + doMethodTest("try (R r1 = null; R r2 = null;) { }", + "try (R r1 = null ;R r2 = null ;) { }"); + } + + public void testSpacesInResourceAssignment() throws Exception { + getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; + + getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = true; + doMethodTest("try (R r=null) { }", + "try (R r = null) { }"); + + getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = false; + doMethodTest("try (R r = null) { }", + "try (R r=null) { }"); + } } 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 31f417dcad61..07693d68ec00 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java @@ -411,6 +411,13 @@ public class CommonCodeStyleSettings { */ public boolean SPACE_WITHIN_FOR_PARENTHESES = false; + /** + * "try( Resource r = r() )" + * or + * "catch(Resource r = r())" + */ + public boolean SPACE_WITHIN_TRY_PARENTHESES = false; + /** * "catch( Exception e )" * or @@ -497,6 +504,13 @@ public class CommonCodeStyleSettings { */ public boolean SPACE_BEFORE_FOR_PARENTHESES = true; + /** + * "try (...)" + * or + * "try(...)" + */ + public boolean SPACE_BEFORE_TRY_PARENTHESES = true; + /** * "catch (...)" * or 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 e9bd4f33829f..f55bdcfacff1 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 @@ -37,6 +37,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel { initBooleanField("SPACE_BEFORE_FOR_PARENTHESES", ApplicationBundle.message("checkbox.spaces.for.parentheses"), SPACES_BEFORE_PARENTHESES); initBooleanField("SPACE_BEFORE_WHILE_PARENTHESES", ApplicationBundle.message("checkbox.spaces.while.parentheses"), SPACES_BEFORE_PARENTHESES); initBooleanField("SPACE_BEFORE_SWITCH_PARENTHESES", ApplicationBundle.message("checkbox.spaces.switch.parentheses"), SPACES_BEFORE_PARENTHESES); + initBooleanField("SPACE_BEFORE_TRY_PARENTHESES", ApplicationBundle.message("checkbox.spaces.try.parentheses"), SPACES_BEFORE_PARENTHESES); initBooleanField("SPACE_BEFORE_CATCH_PARENTHESES", ApplicationBundle.message("checkbox.spaces.catch.parentheses"), SPACES_BEFORE_PARENTHESES); initBooleanField("SPACE_BEFORE_SYNCHRONIZED_PARENTHESES", ApplicationBundle.message("checkbox.spaces.synchronized.parentheses"), SPACES_BEFORE_PARENTHESES); initBooleanField("SPACE_BEFORE_ANOTATION_PARAMETER_LIST", ApplicationBundle.message("checkbox.spaces.annotation.parameters"), SPACES_BEFORE_PARENTHESES); @@ -84,6 +85,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel { initBooleanField("SPACE_WITHIN_FOR_PARENTHESES", ApplicationBundle.message("checkbox.spaces.for.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_WHILE_PARENTHESES", ApplicationBundle.message("checkbox.spaces.while.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_SWITCH_PARENTHESES", ApplicationBundle.message("checkbox.spaces.switch.parentheses"), SPACES_WITHIN); + initBooleanField("SPACE_WITHIN_TRY_PARENTHESES", ApplicationBundle.message("checkbox.spaces.try.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_CATCH_PARENTHESES", ApplicationBundle.message("checkbox.spaces.catch.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_SYNCHRONIZED_PARENTHESES", ApplicationBundle.message("checkbox.spaces.synchronized.parentheses"), SPACES_WITHIN); initBooleanField("SPACE_WITHIN_CAST_PARENTHESES", ApplicationBundle.message("checkbox.spaces.type.cast.parentheses"), SPACES_WITHIN); diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index ffaf57f2093f..68930aed4850 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -248,6 +248,7 @@ checkbox.spaces.method.declaration.parentheses=Method declaration parentheses checkbox.spaces.if.parentheses='if' parentheses checkbox.spaces.while.parentheses='while' parentheses checkbox.spaces.for.parentheses='for' parentheses +checkbox.spaces.try.parentheses='try' parentheses checkbox.spaces.catch.parentheses='catch' parentheses checkbox.spaces.switch.parentheses='switch' parentheses checkbox.spaces.synchronized.parentheses='synchronized' parentheses