Search in sources :

Example 1 with GrCaseLabel

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel 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 GrCaseLabel

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

the class SwitchStatementGenerator method writeCondition.

private static void writeCondition(StringBuilder builder, ExpressionContext context, GrCaseSection section, GrCaseLabel[] labels, GrExpression[] args) {
    builder.append("if (");
    for (GrCaseLabel label : labels) {
        if (label.isDefault()) {
            builder.append("true");
        } else {
            GenerationUtil.invokeMethodByName(label.getValue(), "isCase", args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, new ExpressionGenerator(builder, context), section);
        }
        builder.append("||");
    }
    builder.delete(builder.length() - 2, builder.length());
    builder.append(") ");
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)

Example 3 with GrCaseLabel

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

the class EquivalenceChecker method caseClausesAreEquivalent.

private static boolean caseClausesAreEquivalent(GrCaseSection clause1, GrCaseSection clause2) {
    final GrCaseLabel[] label1 = clause1.getCaseLabels();
    final GrCaseLabel[] label2 = clause2.getCaseLabels();
    if (label1.length != label2.length)
        return false;
    for (int i = 0; i < label1.length; i++) {
        GrCaseLabel l1 = label1[i];
        GrCaseLabel l2 = label2[i];
        if (!expressionsAreEquivalent(l1.getValue(), l2.getValue())) {
            return false;
        }
    }
    final GrStatement[] statements1 = clause1.getStatements();
    final GrStatement[] statements2 = clause2.getStatements();
    if (statements1.length != statements2.length) {
        return false;
    }
    for (int i = 0; i < statements1.length; i++) {
        if (!statementsAreEquivalent(statements1[i], statements2[i])) {
            return false;
        }
    }
    return false;
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)

Example 4 with GrCaseLabel

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

the class GroovyStatementMover method allRanges.

private List<LineRange> allRanges(final GroovyPsiElement scope, final boolean stmtLevel, final boolean topLevel) {
    final ArrayList<LineRange> result = new ArrayList<>();
    scope.accept(new PsiRecursiveElementVisitor() {

        int lastStart = -1;

        private void addRange(int endLine) {
            if (lastStart >= 0) {
                result.add(new LineRange(lastStart, endLine));
            }
            lastStart = endLine;
        }

        @Override
        public void visitElement(PsiElement element) {
            if (stmtLevel && element instanceof GrCodeBlock) {
                final PsiElement lBrace = ((GrCodeBlock) element).getLBrace();
                if (nlsAfter(lBrace)) {
                    assert lBrace != null;
                    addRange(new LineRange(lBrace).endLine);
                }
                addChildRanges(((GrCodeBlock) element).getStatements());
                final PsiElement rBrace = ((GrCodeBlock) element).getRBrace();
                if (nlsAfter(rBrace)) {
                    assert rBrace != null;
                    final int endLine = new LineRange(rBrace).endLine;
                    if (lastStart >= 0) {
                        for (int i = lastStart + 1; i < endLine; i++) {
                            addRange(i);
                        }
                    }
                }
            } else if (stmtLevel && element instanceof GrCaseSection) {
                final GrCaseLabel[] allLabels = ((GrCaseSection) element).getCaseLabels();
                final GrCaseLabel label = allLabels[0];
                if (nlsAfter(label)) {
                    addRange(new LineRange(label).endLine);
                }
                addChildRanges(((GrCaseSection) element).getStatements());
            } else if (element instanceof GroovyFileBase) {
                addChildRanges(((GroovyFileBase) element).getTopStatements());
            } else if (!stmtLevel && !topLevel && element instanceof GrTypeDefinitionBody) {
                addChildRanges(((GrTypeDefinitionBody) element).getMemberDeclarations());
            } else {
                super.visitElement(element);
            }
        }

        private boolean shouldDigInside(GroovyPsiElement statement) {
            if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
                return false;
            }
            if (statement instanceof GrVariableDeclaration && !stmtLevel) {
                return false;
            }
            return true;
        }

        private void addChildRanges(GroovyPsiElement[] statements) {
            for (int i = 0; i < statements.length; i++) {
                GroovyPsiElement statement = statements[i];
                if (nlsAfter(statement)) {
                    final LineRange range = getLineRange(statement);
                    if ((i == 0 || isStatement(statements[i - 1])) && isStatement(statement)) {
                        for (int j = lastStart; j < range.startLine; j++) {
                            addRange(j + 1);
                        }
                    }
                    lastStart = range.startLine;
                    if (shouldDigInside(statement)) {
                        statement.accept(this);
                    }
                    addRange(range.endLine);
                }
            }
        }
    });
    return result;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 5 with GrCaseLabel

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

the class SwitchStatementGenerator method generateCaseSection.

private static void generateCaseSection(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull ExpressionContext innerContext, @NotNull GrCaseSection section) {
    for (GrCaseLabel label : section.getCaseLabels()) {
        writeLabel(builder, context, label);
    }
    final GrStatement[] statements = section.getStatements();
    CodeBlockGenerator generator = new CodeBlockGenerator(builder, innerContext);
    for (GrStatement statement : statements) {
        statement.accept(generator);
        builder.append("\n");
    }
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)7 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 NotNull (org.jetbrains.annotations.NotNull)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)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 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrSwitchStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1