Search in sources :

Example 1 with GrBreakStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement in project intellij-community by JetBrains.

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> 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) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrBreakStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement in project intellij-community by JetBrains.

the class ControlFlowBuilderUtil method isLastStatementInCaseSection.

private static boolean isLastStatementInCaseSection(GrCaseSection caseSection, GrSwitchStatement switchStatement) {
    final GrCaseSection[] sections = switchStatement.getCaseSections();
    final int i = ArrayUtilRt.find(sections, caseSection);
    if (i == sections.length - 1) {
        return true;
    }
    for (int j = i + 1; j < sections.length; j++) {
        GrCaseSection section = sections[j];
        for (GrStatement statement : section.getStatements()) {
            if (!(statement instanceof GrBreakStatement)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 3 with GrBreakStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement in project intellij-community by JetBrains.

the class ControlFlowBuilderUtil method isCertainlyReturnStatement.

/**
   * check whether statement is return (the statement which provides return value) statement of method or closure.
   *
   * @param st
   * @return
   */
public static boolean isCertainlyReturnStatement(GrStatement st) {
    final PsiElement parent = st.getParent();
    if (parent instanceof GrOpenBlock) {
        if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
            return false;
        PsiElement pparent = parent.getParent();
        if (pparent instanceof GrMethod) {
            return true;
        }
        if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
            pparent = pparent.getParent();
        }
        if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
            return isCertainlyReturnStatement((GrStatement) pparent);
        }
    } else if (parent instanceof GrClosableBlock) {
        return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
    } else if (parent instanceof GroovyFileBase) {
        return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
    } else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
        return isCertainlyReturnStatement((GrStatement) parent);
    } else if (parent instanceof GrCaseSection) {
        final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
        final GrStatement last = ArrayUtil.getLastElement(statements);
        final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
        if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
            return isCertainlyReturnStatement(switchStatement);
        } else if (st == last) {
            if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
                return isCertainlyReturnStatement(switchStatement);
            }
        }
    }
    return false;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrBreakStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrAnnotationArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList)1 GrControlStatement (org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrContinueStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement)1 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)1 GrThrowStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement)1 GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)1 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)1 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)1