diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlock.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlock.java index a64a3a535f92..2bc1772906d3 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlock.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlock.java @@ -35,7 +35,14 @@ import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag; import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes; import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList; @@ -121,15 +128,33 @@ public class GroovyBlock implements Block, GroovyElementTypes { @NotNull public ChildAttributes getChildAttributes(final int newChildIndex) { - return getAttributesByParent(); - } - - private ChildAttributes getAttributesByParent() { ASTNode astNode = getNode(); final PsiElement psiParent = astNode.getPsi(); if (psiParent instanceof GroovyFileBase) { return new ChildAttributes(Indent.getNoneIndent(), null); } + if (psiParent instanceof GrSwitchStatement) { + List subBlocks = getSubBlocks(); + if (newChildIndex > 0) { + Block block = subBlocks.get(newChildIndex - 1); + if (block instanceof GroovyBlock) { + PsiElement anchorPsi = ((GroovyBlock)block).getNode().getPsi(); + if (anchorPsi instanceof GrCaseSection) { + for (GrStatement statement : ((GrCaseSection)anchorPsi).getStatements()) { + if (statement instanceof GrBreakStatement || + statement instanceof GrContinueStatement || + statement instanceof GrReturnStatement || + statement instanceof GrThrowStatement) { + return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(anchorPsi), null); + } + } + int indentSize = mySettings.getAdditionalIndentOptions(anchorPsi.getContainingFile().getFileType()).INDENT_SIZE; + return new ChildAttributes(Indent.getSpaceIndent(mySettings.INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize), null); + } + } + } + } + if (BLOCK_SET.contains(astNode.getElementType()) || SWITCH_STATEMENT.equals(astNode.getElementType())) { return new ChildAttributes(Indent.getNormalIndent(), null); 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 ef1d26d2c58b..da414c9655c8 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 @@ -232,10 +232,17 @@ public abstract class GroovyIndentProcessor implements GroovyElementTypes { } private static Indent indentForSwitchStatement(PsiElement psiParent, ASTNode child) { - if (CASE_SECTION.equals(child.getElementType()) && CodeStyleSettingsManager.getSettings(psiParent.getProject()).INDENT_CASE_FROM_SWITCH) { - return Indent.getNormalIndent(); + if (CASE_SECTION.equals(child.getElementType())) { + return getSwitchCaseIndent(psiParent); } return Indent.getNoneIndent(); } + + public static Indent getSwitchCaseIndent(PsiElement psiParent) { + if (CodeStyleSettingsManager.getSettings(psiParent.getProject()).INDENT_CASE_FROM_SWITCH) { + return Indent.getNormalIndent(); + } + return Indent.getNoneIndent(); + } } 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 3c0791d75a8c..45758242c29f 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 @@ -139,7 +139,59 @@ switch(x) { }""" } - + + public void testCaseBeforeReturn() { + doTest """ +switch(x) { + case 0: + return x +}""", """ +switch(x) { + case 0: + + return x +}""" + } + + public void testCaseAfterBreak() { + doTest """ +switch(x) { + case 0: + break +}""", """ +switch(x) { + case 0: + break + +}""" + } + + public void testCaseAfterCall() { + doTest """ +switch(x) { + case 0: + foo() +}""", """ +switch(x) { + case 0: + foo() + +}""" + } + + public void testCaseAfterReturn() { + doTest """ +switch(x) { + case 0: + return 2 +}""", """ +switch(x) { + case 0: + return 2 + +}""" + } + public void testAlmostBeforeClosingClosureBrace() throws Throwable { doTest """ def c = { a ->