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 71f59b6a0001..e2ce60e42250 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeStyle/GroovyLanguageCodeStyleSettingsProvider.java @@ -34,9 +34,9 @@ import javax.swing.*; */ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider { - public static final String IN_SPOCK_ONLY = "In Spock only"; public static final String ABSOLUTE = "Absolute"; - public static final String RELATIVE = "Relative"; + public static final String RELATIVE = "Indent statements after label"; + public static final String RELATIVE_REVERSED = "Indent labels"; @NotNull @Override @@ -264,8 +264,6 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe defaultSettings.SPACE_WITHIN_BRACES = true; defaultSettings.KEEP_SIMPLE_CLASSES_IN_ONE_LINE = true; defaultSettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE = true; - //defaultSettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true; - defaultSettings.getIndentOptions().LABEL_INDENT_SIZE = 2; return defaultSettings; } @@ -300,7 +298,7 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe myStyleLabel = new JBLabel("Label indent style:"); - myLabelIndentStyle = new JComboBox(new Object[] {IN_SPOCK_ONLY, ABSOLUTE, RELATIVE}); + myLabelIndentStyle = new JComboBox(new Object[] {ABSOLUTE, RELATIVE, RELATIVE_REVERSED}); add(myStyleLabel, myLabelIndentStyle); } @@ -320,7 +318,7 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe return !RELATIVE.equals(myLabelIndentStyle.getSelectedItem()); } else { - return !IN_SPOCK_ONLY.equals(myLabelIndentStyle.getSelectedItem()); + return !RELATIVE_REVERSED.equals(myLabelIndentStyle.getSelectedItem()); } } @@ -342,7 +340,7 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe myLabelIndentStyle.setSelectedItem(RELATIVE); } else { - myLabelIndentStyle.setSelectedItem(IN_SPOCK_ONLY); + myLabelIndentStyle.setSelectedItem(RELATIVE_REVERSED); } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/ClosureBodyBlock.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/ClosureBodyBlock.java index 45c57c810bcb..9bafca4b4b33 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/ClosureBodyBlock.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/ClosureBodyBlock.java @@ -24,7 +24,6 @@ import com.intellij.openapi.util.TextRange; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.formatter.FormattingContext; -import org.jetbrains.plugins.groovy.formatter.processors.GroovyIndentProcessor; import java.util.List; @@ -53,7 +52,7 @@ public class ClosureBodyBlock extends GroovyBlock { GroovyBlockGenerator generator = new GroovyBlockGenerator(this); List children = GroovyBlockGenerator.getClosureBodyVisibleChildren(myNode.getTreeParent()); - mySubBlocks = generator.generateSubBlockForCodeBlocks(false, children, GroovyIndentProcessor.indentLabelBlock(myNode.getPsi(), myContext.getGroovySettings())); + mySubBlocks = generator.generateSubBlockForCodeBlocks(false, children, myContext.getGroovySettings().INDENT_LABEL_BLOCKS); //at least -> exists assert !mySubBlocks.isEmpty(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlock.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlock.java index 8cdf7ca0919a..eef7050b2cca 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlock.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlock.java @@ -219,7 +219,7 @@ public class GroovyBlock implements Block, GroovyElementTypes, ASTBlock { return new ChildAttributes(Indent.getNormalIndent(), null); } if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) { - final Indent indent = GroovyIndentProcessor.indentLabelBlock(psiParent, getContext().getGroovySettings()) + final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent(); return new ChildAttributes(indent, null); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java index 34789c74040d..5ba50a637744 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java @@ -242,7 +242,7 @@ public class GroovyBlockGenerator implements GroovyElementTypes { } if (blockPsi instanceof GrCodeBlock || blockPsi instanceof GroovyFile || classLevel) { - return generateSubBlockForCodeBlocks(classLevel, visibleChildren(myNode), GroovyIndentProcessor.indentLabelBlock(blockPsi, myContext.getGroovySettings())); + return generateSubBlockForCodeBlocks(classLevel, visibleChildren(myNode), myContext.getGroovySettings().INDENT_LABEL_BLOCKS); } if (blockPsi instanceof GrMethod) { @@ -324,7 +324,7 @@ public class GroovyBlockGenerator implements GroovyElementTypes { return wrap; } - public List generateSubBlockForCodeBlocks(boolean classLevel, final List children, final boolean indentLabelBlocks) { + public List generateSubBlockForCodeBlocks(boolean classLevel, final List children, boolean indentLabelBlocks) { calculateAlignments(children, classLevel); final ArrayList subBlocks = new ArrayList(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java index a7ab5f118e3c..2cc72c343761 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java @@ -18,15 +18,11 @@ package org.jetbrains.plugins.groovy.formatter.processors; import com.intellij.formatting.Indent; import com.intellij.lang.ASTNode; -import com.intellij.psi.PsiClassType; import com.intellij.psi.PsiElement; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; -import com.intellij.psi.util.InheritanceUtil; -import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings; import org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock; import org.jetbrains.plugins.groovy.formatter.blocks.GrLabelBlock; import org.jetbrains.plugins.groovy.formatter.blocks.GroovyBlock; @@ -58,8 +54,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplements import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod; -import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement; -import org.jetbrains.plugins.groovy.spock.SpockUtils; import static org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes.*; @@ -151,46 +145,15 @@ public class GroovyIndentProcessor extends GroovyElementVisitor { if (indentOptions != null && indentOptions.LABEL_INDENT_ABSOLUTE) { myResult = Indent.getAbsoluteLabelIndent(); } - } - else { - if (indentLabelBlock(myChild, myBlock.getContext().getGroovySettings())) { + else if (!myBlock.getContext().getGroovySettings().INDENT_LABEL_BLOCKS) { myResult = Indent.getLabelIndent(); } } - } - - public static boolean indentLabelBlock(@NotNull PsiElement place, final GroovyCodeStyleSettings settings) { - if (settings.INDENT_LABEL_BLOCKS) { - return true; - } - - final GrTypeDefinition clazz = PsiTreeUtil.getParentOfType(place, GrTypeDefinition.class); - if (clazz == null) return false; - if (isDirectInheritorOfSpecification(clazz) || InheritanceUtil.isInheritor(clazz, SpockUtils.SPEC_CLASS_NAME)) { - return true; - } - - final PsiClassType[] supers = clazz.getSuperTypes(); - for (PsiClassType aSuper : supers) { - if (aSuper.equalsToText(SpockUtils.SPEC_CLASS_NAME)) { - return true; + else { + if (myBlock.getContext().getGroovySettings().INDENT_LABEL_BLOCKS) { + myResult = Indent.getLabelIndent(); } } - - return false; - } - - private static boolean isDirectInheritorOfSpecification(@NotNull GrTypeDefinition clazz) { - final GrExtendsClause clause = clazz.getExtendsClause(); - if (clause != null) { - final GrCodeReferenceElement[] refs = clause.getReferenceElements(); - for (GrCodeReferenceElement ref : refs) { - if (SpockUtils.SPEC_CLASS_NAME.equals(ref.getClassNameText())) { - return true; - } - } - } - return false; } @Override 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 5b8be8eb10b2..ae5b677a9167 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 @@ -211,7 +211,7 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { @Override public void visitLabeledStatement(GrLabeledStatement labeledStatement) { if (myType1 == mCOLON) { - if (GroovyIndentProcessor.indentLabelBlock(labeledStatement, myGroovySettings)) { + if (myGroovySettings.INDENT_LABEL_BLOCKS) { createLF(true); } else { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/EnterActionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/EnterActionTest.groovy index eec527680a64..205feed0f3a0 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/EnterActionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/EnterActionTest.groovy @@ -14,13 +14,11 @@ * limitations under the License. */ -package org.jetbrains.plugins.groovy.lang.formatter; - +package org.jetbrains.plugins.groovy.lang.formatter import org.jetbrains.plugins.groovy.GroovyFileType import org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings import org.jetbrains.plugins.groovy.util.TestUtils - /** * @author ilyas */ @@ -350,6 +348,9 @@ def cl = { } void testIndentAfterLabelColon() { + groovySettings.indentOptions.LABEL_INDENT_SIZE = 2 + groovySettings.indentOptions.INDENT_SIZE = 2 + groovyCustomSettings.INDENT_LABEL_BLOCKS = true doTest(''' class A extends spock.lang.Specification { def 'test'() { 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 4807a566255a..ba193926cd5d 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 @@ -112,9 +112,9 @@ public class FormatterTest extends GroovyFormatterTestCase { public void testAnnotationOnSeparateLine() throws Throwable { doTest(); } public void testAlignMultipleVariables() throws Throwable { doTest(); } - public void testSpockTable() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } - public void testSpockTableComments() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } - public void testSpockTableWithStringComment() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } + //public void testSpockTable() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } + //public void testSpockTableComments() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } + //public void testSpockTableWithStringComment() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } public void testElseIfs() throws Throwable { groovySettings.SPECIAL_ELSE_IF_TREATMENT = false; @@ -712,24 +712,36 @@ print abc ?: } void testLabelsInBasicMode() { + groovySettings.indentOptions.INDENT_SIZE = 4 + groovySettings.indentOptions.LABEL_INDENT_SIZE = -2 groovyCustomSettings.INDENT_LABEL_BLOCKS = false checkFormatting('''\ -abc:foo() -bar() +def bar() { + abc: + foo() + bar() +} ''', '''\ -abc: foo() -bar() +def bar() { + abc: + foo() + bar() +} ''') } void testLabels() { checkFormatting('''\ +def foo() { abc:foo() bar() +} ''', '''\ -abc: foo() -bar() +def foo() { + abc: foo() + bar() +} ''') }