IDEA-75064 simple code blocks in one line

This commit is contained in:
Max Medvedev
2013-04-30 11:49:11 +04:00
parent c1838af199
commit 35f40d8078
3 changed files with 83 additions and 21 deletions
@@ -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" +
"}";
@@ -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);
@@ -204,8 +204,8 @@ public class FormatterTest extends GroovyFormatterTestCase {
}
public void doTest() {
final List<String> 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;