Search in sources :

Example 46 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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 47 with GrStatement

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

the class ReachingDefinitionsCollector method addClosureUsages.

private static void addClosureUsages(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrStatement first, final GrStatement last, GrControlFlowOwner flowOwner) {
    flowOwner.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
            addUsagesInClosure(imap, omap, closure, first, last);
            super.visitClosure(closure);
        }

        private void addUsagesInClosure(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrClosableBlock closure, final GrStatement first, final GrStatement last) {
            closure.accept(new GroovyRecursiveElementVisitor() {

                @Override
                public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
                    if (refExpr.isQualified()) {
                        return;
                    }
                    PsiElement resolved = refExpr.resolve();
                    if (!(resolved instanceof GrVariable)) {
                        return;
                    }
                    GrVariable variable = (GrVariable) resolved;
                    if (PsiTreeUtil.isAncestor(closure, variable, true)) {
                        return;
                    }
                    if (variable instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(closure, ((ClosureSyntheticParameter) variable).getClosure(), false)) {
                        return;
                    }
                    String name = variable.getName();
                    if (!(variable instanceof GrField)) {
                        if (!isInFragment(first, last, resolved)) {
                            if (isInFragment(first, last, closure)) {
                                addVariable(name, imap, variable.getManager(), variable.getType());
                            }
                        } else {
                            if (!isInFragment(first, last, closure)) {
                                addVariable(name, omap, variable.getManager(), variable.getType());
                            }
                        }
                    }
                }
            });
        }
    });
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) NotNull(org.jetbrains.annotations.NotNull) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 48 with GrStatement

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

the class PsiImplUtil method replaceExpression.

@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
    PsiElement oldParent = oldExpr.getParent();
    if (oldParent == null)
        throw new PsiInvalidElementAccessException(oldExpr);
    if (!(oldExpr instanceof GrApplicationStatement)) {
        newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
    }
    // Remove unnecessary parentheses
    if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
        return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
    }
    //regexes cannot be after identifier , try to replace it with simple string
    if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
        final PsiElement copy = newExpr.copy();
        final GrLiteral regex = getRegexAtTheBeginning(copy);
        LOG.assertTrue(regex != null);
        final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
        if (regex == copy) {
            return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
        } else {
            regex.replace(stringLiteral);
            return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
        }
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
    if (oldParent instanceof GrStringInjection) {
        if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
            return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
        } else {
            GrClosableBlock block = factory.createClosureFromText("{foo}");
            oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
            GrStatement[] statements = block.getStatements();
            return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        }
    }
    if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
        final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
        GrStringUtil.wrapInjection(stringInjection);
        assert stringInjection != null;
        final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        return (GrExpression) replaced;
    }
    //check priorities    
    if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
        GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
        if (newExpr != addedParenth) {
            return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
        }
    }
    //we should add the expression in arg list
    if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
        return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
    }
    newExpr = (GrExpression) oldExpr.replace(newExpr);
    // to find target parenthesised expression.
    if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
        int parentCount = 0;
        PsiElement element = oldParent;
        while (element != null && !(element instanceof GrCommandArgumentList)) {
            if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
                break;
            if (element instanceof PsiFile)
                break;
            final PsiElement parent = element.getParent();
            if (parent == null)
                break;
            if (!isFirstChild(element))
                break;
            element = parent;
            parentCount++;
        }
        if (element instanceof GrCommandArgumentList) {
            final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
            final PsiElement parent = commandArgList.getParent();
            LOG.assertTrue(parent instanceof GrApplicationStatement);
            final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
            final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
            PsiElement result = newCall.getArgumentList().getAllArguments()[0];
            for (int i = 0; i < parentCount; i++) {
                result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
            }
            LOG.assertTrue(result instanceof GrParenthesizedExpression);
            return (GrExpression) result;
        }
    }
    return newExpr;
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with GrStatement

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

the class GrIfStatementImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    GrStatement elseBranch = getElseBranch();
    if (elseBranch != null && child == elseBranch.getNode()) {
        PsiElement elseKeywordElement = findChildByType(GroovyTokenTypes.kELSE);
        if (elseKeywordElement != null) {
            super.deleteChildInternal(elseKeywordElement.getNode());
        }
    }
    super.deleteChildInternal(child);
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 50 with GrStatement

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

the class GrStringUtil method checkGStringInjectionForUnnecessaryBraces.

public static boolean checkGStringInjectionForUnnecessaryBraces(GrStringInjection injection) {
    final GrClosableBlock block = injection.getClosableBlock();
    if (block == null)
        return false;
    final GrStatement[] statements = block.getStatements();
    if (statements.length != 1)
        return false;
    if (!(statements[0] instanceof GrReferenceExpression))
        return false;
    return checkBraceIsUnnecessary(statements[0], injection.getNextSibling());
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6