Search in sources :

Example 26 with GrOpenBlock

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

the class SpockUtils method createVariableMap.

// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return Collections.emptyMap();
    PsiElement elementUnderLabel = null;
    PsiElement elementAfterLabel = null;
    main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrLabeledStatement) {
            GrLabeledStatement l = (GrLabeledStatement) e;
            elementAfterLabel = l.getNextSibling();
            while (true) {
                GrStatement statement = l.getStatement();
                if ("where".equals(l.getName())) {
                    elementUnderLabel = statement;
                    break main;
                }
                if (statement instanceof GrLabeledStatement) {
                    l = (GrLabeledStatement) statement;
                    continue;
                }
                break;
            }
        }
    }
    if (elementUnderLabel == null)
        return Collections.emptyMap();
    Map<String, SpockVariableDescriptor> res = new HashMap<>();
    PsiElement e = elementUnderLabel;
    while (e != null) {
        if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
            GrBinaryExpression shift = (GrBinaryExpression) e;
            GrExpression leftOperand = shift.getLeftOperand();
            GrExpression rightOperand = shift.getRightOperand();
            if (leftOperand instanceof GrReferenceExpression) {
                String name = getNameByReference(leftOperand);
                if (name != null) {
                    SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
                    descriptor.addExpressionOfCollection(rightOperand);
                    res.put(name, descriptor);
                }
            } else if (leftOperand instanceof GrListOrMap) {
                GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
                SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
                if (rightOperand instanceof GrListOrMap) {
                    for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
                        if (expression instanceof GrListOrMap) {
                            add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
                        } else {
                            for (SpockVariableDescriptor variable : variables) {
                                if (variable != null) {
                                    variable.addExpressionOfCollection(expression);
                                }
                            }
                        }
                    }
                }
            }
        } else if (e instanceof GrAssignmentExpression) {
            GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
            GrExpression lValue = assExpr.getLValue();
            String name = getNameByReference(lValue);
            if (name != null) {
                res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
            }
        } else if (isOrStatement(e)) {
            // See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
            List<GrExpression> variableDefinitions = new ArrayList<>();
            splitOr(variableDefinitions, (GrExpression) e);
            SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
            List<GrExpression> row = new ArrayList<>();
            PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
            while (isOrStatement(rowElement)) {
                row.clear();
                splitOr(row, (GrExpression) rowElement);
                add(variables, row);
                rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
            }
            e = rowElement;
            continue;
        }
        e = getNext(e, elementUnderLabel, elementAfterLabel);
    }
    return res;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement)

Example 27 with GrOpenBlock

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

the class ControlFlowUtils method openBlockCompletesWithStatement.

public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
    GroovyPsiElement elementToCheck = statement;
    while (true) {
        if (elementToCheck == null)
            return false;
        final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
        if (container == null)
            return false;
        if (isLoop(container))
            return false;
        if (container instanceof GrCaseSection) {
            final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (container == sections[sections.length - 1])
                return false;
        }
        if (container instanceof GrCodeBlock) {
            if (elementToCheck instanceof GrStatement) {
                final GrCodeBlock codeBlock = (GrCodeBlock) container;
                if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
                    return false;
                }
            }
            if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
                if (container.equals(body)) {
                    return true;
                }
                elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
            } else {
                elementToCheck = container;
            }
        } else {
            elementToCheck = container;
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 28 with GrOpenBlock

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

the class ControlFlowUtils method tryStatementMayReturnNormally.

private static boolean tryStatementMayReturnNormally(@NotNull GrTryCatchStatement tryStatement) {
    final GrFinallyClause finallyBlock = tryStatement.getFinallyClause();
    if (finallyBlock != null) {
        if (!openBlockMayCompleteNormally(finallyBlock.getBody())) {
            return false;
        }
    }
    final GrOpenBlock tryBlock = tryStatement.getTryBlock();
    if (openBlockMayCompleteNormally(tryBlock)) {
        return true;
    }
    for (GrCatchClause catchClause : tryStatement.getCatchClauses()) {
        if (openBlockMayCompleteNormally(catchClause.getBody())) {
            return true;
        }
    }
    return false;
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 29 with GrOpenBlock

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

the class ControlFlowUtils method blockCompletesWithStatement.

public static boolean blockCompletesWithStatement(@NotNull GrBlockStatement body, @NotNull GrStatement statement) {
    GrStatement statementToCheck = statement;
    while (true) {
        if (statementToCheck == null) {
            return false;
        }
        final PsiElement container = statementToCheck.getParent();
        if (container == null) {
            return false;
        }
        if (container instanceof GrLoopStatement) {
            return false;
        } else if (container instanceof GrCaseSection) {
            final GrCaseSection caseSection = (GrCaseSection) container;
            if (!statementIsLastInBlock(caseSection, statementToCheck))
                return false;
            final PsiElement parent = container.getParent();
            assert parent instanceof GrSwitchStatement;
            final GrSwitchStatement switchStatement = (GrSwitchStatement) parent;
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (ArrayUtil.getLastElement(sections) != caseSection) {
                return false;
            }
        } else if (container instanceof GrOpenBlock) {
            final GrOpenBlock block = (GrOpenBlock) container;
            if (!statementIsLastInBlock(block, statementToCheck))
                return false;
            final PsiElement parent = block.getParent();
            if (parent instanceof GrBlockStatement) {
                final GrBlockStatement blockStatement = (GrBlockStatement) parent;
                if (blockStatement == body)
                    return true;
            }
        } else if (container instanceof GrClosableBlock) {
            return false;
        }
        statementToCheck = getContainingStatement(statementToCheck);
    }
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 30 with GrOpenBlock

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

the class EquivalenceChecker method blockStatementsAreEquivalent.

private static boolean blockStatementsAreEquivalent(@NotNull GrBlockStatement statement1, @NotNull GrBlockStatement statement2) {
    final GrOpenBlock block1 = statement1.getBlock();
    final GrOpenBlock block2 = statement2.getBlock();
    return openBlocksAreEquivalent(block1, block2);
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3