diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java index 50637a1771f5..df15f9773476 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java @@ -129,6 +129,8 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe CommonCodeStyleSettings defaultSettings = new CommonCodeStyleSettings(GroovyFileType.GROOVY_LANGUAGE); defaultSettings.initIndentOptions(); defaultSettings.SPACE_WITHIN_BRACES = true; + defaultSettings.KEEP_SIMPLE_CLASSES_IN_ONE_LINE = true; + defaultSettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE = true; return defaultSettings; } @@ -268,11 +270,11 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe " int x = (3 + 4 + 5 + 6) * (7 + 8 + 9 + 10) * (11 + 12 + 13 + 14 + 0xFFFFFFFF)\n" + " String s1, s2, s3\n" + " s1 = s2 = s3 = \"012345678901456\"\n" + - " assert i + j + k + l + n+ m <= 2 : \"assert description\"" + + " assert i + j + k + l + n+ m <= 2 : \"assert description\"\n" + " int y = 2 > 3 ? 7 + 8 + 9 : 11 + 12 + 13\n" + " super.getFoo().foo().getBar().bar()\n" + "\n" + - " label: " + + " label: \n" + " if (2 < 3) return else if (2 > 3) return else return\n" + " for (int i = 0; i < 0xFFFFFF; i += 2) System.out.println(i)\n" + " print([\n" + @@ -299,7 +301,7 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe " throws Exception { \n" + " foo.foo().bar(\"arg1\", \n" + " \"arg2\") \n" + - " new Object() {}" + + " new Object() {}\n" + " } \n" + " class TestInnerClass {}\n" + " interface TestInnerInterface {}\n" + @@ -314,7 +316,7 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe " }\n" + " @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") public static int myFoo\n" + " public void method(@Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") final int param){\n" + - " @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") final int localVariable" + + " @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") final int localVariable\n" + " }\n" + "}"; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java index 4b42b9ff1ce5..d197328b06a4 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement; import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList; -import org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil; import static org.jetbrains.plugins.groovy.GroovyFileType.GROOVY_LANGUAGE; import static org.jetbrains.plugins.groovy.formatter.models.spacing.SpacingTokens.*; @@ -301,20 +300,23 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { } public void visitOpenBlock(GrOpenBlock block) { - if (block.getParent() instanceof GrBlockStatement) { - if (myType1 == mLCURLY || myType2 == mRCURLY) { + + final PsiElement parent = block.getParent(); + boolean keepInOneLine = parent instanceof GrMethod ? + mySettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE : + mySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE; + + if (myType1 == mLCURLY && myType2 == mRCURLY) { + myResult = Spacing.createSpacing(0, 0, keepInOneLine ? 0 : 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + else if (myType1 == mLCURLY || myType2 == mRCURLY) { + if (keepInOneLine) { + myResult = Spacing.createDependentLFSpacing(1, 1, block.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + else { myResult = Spacing.createSpacing(1, 1, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } } - else if (myType1 == mLCURLY && myType2 == mRCURLY) { - myResult = Spacing.createSpacing(0, 0, 0, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); - } - else if (myType1 == mLCURLY && !GrStringUtil.isMultilineStringElement(myChild2) || - myType2 == mRCURLY && !GrStringUtil.isMultilineStringElement(myChild1)) { - final int spaceWithinBraces = mySettings.SPACE_WITHIN_BRACES ? 1 : 0; - final TextRange range = block.getTextRange(); - myResult = Spacing.createDependentLFSpacing(spaceWithinBraces, 1, range, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); - } } public void visitNewExpression(GrNewExpression newExpression) { @@ -342,7 +344,12 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { public void visitTypeDefinitionBody(GrTypeDefinitionBody typeDefinitionBody) { if (myType1 == mLCURLY && myType2 == mRCURLY) { - myResult = Spacing.createSpacing(0, 0, 0, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_BEFORE_RBRACE); + if (mySettings.KEEP_SIMPLE_CLASSES_IN_ONE_LINE) { + myResult = Spacing.createSpacing(0, 0, 0, mySettings.KEEP_LINE_BREAKS, 0); + } + else { + myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_BEFORE_RBRACE); + } } else if (myType1 == mLCURLY) { myResult = Spacing.createSpacing(0, 0, mySettings.BLANK_LINES_AFTER_CLASS_HEADER + 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_DECLARATIONS); @@ -824,7 +831,8 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { } else if (keepOneLine) { int space = spaceBeforeLbrace ? 1 : 0; - myResult = Spacing.createDependentLFSpacing(space, space, myParent.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + myResult = Spacing.createDependentLFSpacing(space, space, myParent.getTextRange(), mySettings.KEEP_LINE_BREAKS, + mySettings.KEEP_BLANK_LINES_IN_CODE); } else { myResult = Spacing.createSpacing(0, 0, 1, false, mySettings.KEEP_BLANK_LINES_IN_CODE); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy index 65f74cbc349e..9d90dfc69b78 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy @@ -204,8 +204,8 @@ public class FormatterTest extends GroovyFormatterTestCase { } public void doTest() { - final List data = TestUtils.readInput(testDataPath + getTestName(true) + ".test"); - checkFormatting(data.get(0), StringUtil.trimEnd(data.get(1), "\n")); + def (String before, String after) = TestUtils.readInput(testDataPath + getTestName(true) + ".test"); + checkFormatting(before, StringUtil.trimEnd(after, "\n")); } public void testJavadocLink() throws Throwable { @@ -258,6 +258,58 @@ public class FormatterTest extends GroovyFormatterTestCase { void testImplementsList() { doTest() } + void testSimpleClassInOneLine() { + groovySettings.KEEP_SIMPLE_CLASSES_IN_ONE_LINE = false + checkFormatting('''\ +class A {} +class B { +} +''', '''\ +class A { +} +class B { +} +''') + } + + void testSimpleMethodInOneLine() { + groovySettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE = false + checkFormatting('''\ +def foo() {2} +''', '''\ +def foo() { + 2 +} +''') + + groovySettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE = true + checkFormatting('''\ +def foo() {2} +''', '''\ +def foo() { 2 } +''') + + } + + void testSimpleBlocksInOneLine() { + groovySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true + checkFormatting('''\ +if (abc) {return 2} +''', '''\ +if (abc) { return 2 } +''') + + groovySettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = false + checkFormatting('''\ +if (abc) {return 2} +''', '''\ +if (abc) { + return 2 +} +''') + + } + private void doGeeseTest() { GroovyCodeStyleSettings customSettings = myTempSettings.getCustomSettings(GroovyCodeStyleSettings.class); boolean oldvalue = customSettings.USE_FLYING_GEESE_BRACES;