Search in sources :

Example 1 with PsiElementPredicate

use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.

the class GrRedundantElseIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
                return false;
            final PsiElement parent = element.getParent();
            if (!(parent instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = (GrIfStatement) parent;
            final GrStatement branch = ifStatement.getThenBranch();
            final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
            if (flowOwner == null)
                return false;
            final Instruction[] flow = flowOwner.getControlFlow();
            for (Instruction instruction : flow) {
                if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
                    for (Instruction pred : instruction.allPredecessors()) {
                        final PsiElement predElement = pred.getElement();
                        if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiElementPredicate

use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.

the class ConvertSimpleGetterToPropertyIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            PsiElement parent = element.getParent();
            if (!(parent instanceof GrMethod) || ((GrMethod) parent).getNameIdentifierGroovy() != element)
                return false;
            GrMethod method = (GrMethod) parent;
            GrOpenBlock block = method.getBlock();
            if (block == null)
                return false;
            GrStatement[] statements = block.getStatements();
            if (statements.length != 1)
                return false;
            if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
                return false;
            if (GroovyPropertyUtils.findFieldForAccessor(method, true) != null)
                return false;
            GrStatement statement = statements[0];
            if (!(statement instanceof GrReturnStatement && ((GrReturnStatement) statement).getReturnValue() != null || statement instanceof GrExpression && !PsiType.VOID.equals(((GrExpression) statement).getType()))) {
                return false;
            }
            return true;
        }
    };
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiElementPredicate

use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.

the class ReplaceIfWithTernaryIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement e) {
            if (!e.getNode().getElementType().equals(GroovyTokenTypes.kIF))
                return false;
            if (!(e.getParent() instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = (GrIfStatement) e.getParent();
            final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
            final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
            if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression && ((GrAssignmentExpression) thenBranch).getRValue() != null && ((GrAssignmentExpression) elseBranch).getRValue() != null) {
                final GrExpression lvalue1 = ((GrAssignmentExpression) thenBranch).getLValue();
                final GrExpression lvalue2 = ((GrAssignmentExpression) elseBranch).getLValue();
                return EquivalenceChecker.expressionsAreEquivalent(lvalue1, lvalue2);
            }
            if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement && ((GrReturnStatement) thenBranch).getReturnValue() != null && ((GrReturnStatement) elseBranch).getReturnValue() != null) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiElementPredicate

use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.

the class SimplifyTernaryOperatorIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrConditionalExpression)) {
                return false;
            }
            GrConditionalExpression condExp = (GrConditionalExpression) element;
            PsiType condType = condExp.getType();
            if (condType == null || !PsiType.BOOLEAN.isConvertibleFrom(condType)) {
                return false;
            }
            GrExpression thenBranch = condExp.getThenBranch();
            GrExpression elseBranch = condExp.getElseBranch();
            Object thenVal = GroovyConstantExpressionEvaluator.evaluate(thenBranch);
            if (Boolean.TRUE.equals(thenVal) && elseBranch != null) {
                return true;
            }
            Object elseVal = GroovyConstantExpressionEvaluator.evaluate(elseBranch);
            if (thenBranch != null && Boolean.FALSE.equals(elseVal)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiElementPredicate

use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.

the class GrSplitDeclarationIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (element instanceof GrVariableDeclaration) {
                GrVariableDeclaration decl = (GrVariableDeclaration) element;
                GrVariable[] variables = decl.getVariables();
                if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
                    if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
                        myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
                    } else {
                        myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
                    }
                    return true;
                } else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
                    myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)11 PsiElement (com.intellij.psi.PsiElement)10 NotNull (org.jetbrains.annotations.NotNull)10 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 PsiClass (com.intellij.psi.PsiClass)1 PsiFile (com.intellij.psi.PsiFile)1 PsiType (com.intellij.psi.PsiType)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)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