Search in sources :

Example 6 with PsiElementPredicate

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

the class GrSetStrongTypeIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            PsiElement parent = element.getParent();
            PsiElement pparent;
            if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
                pparent = parent.getParent();
            } else if (isModifierListOfVarDecl(element, parent)) {
                pparent = parent;
            } else {
                return false;
            }
            if (pparent instanceof GrVariableDeclaration) {
                if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
                    return false;
                GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
                for (GrVariable variable : variables) {
                    if (isVarDeclaredWithInitializer(variable))
                        return true;
                }
            } else if (pparent instanceof GrForInClause) {
                final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
                return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
            } else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
                return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
            } else {
                final GrVariable variable = (GrVariable) parent;
                return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
            }
            return false;
        }

        private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
        }

        private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
        }

        private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PsiElementPredicate

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

the class GrAliasImportIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (element instanceof GrReferenceExpression) {
                GroovyResolveResult result = ((GrReferenceExpression) element).advancedResolve();
                PsiElement context = result.getCurrentFileResolveContext();
                if (!(context instanceof GrImportStatement))
                    return false;
                GrImportStatement importStatement = (GrImportStatement) context;
                if (!importStatement.isStatic() || importStatement.isAliasedImport())
                    return false;
                return true;
            } else if (element instanceof GrImportStatement) {
                final GrImportStatement importStatement = (GrImportStatement) element;
                if (!importStatement.isStatic())
                    return false;
                if (importStatement.isOnDemand())
                    return false;
                if (importStatement.isAliasedImport())
                    return false;
                return true;
            }
            return false;
        }
    };
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PsiElementPredicate

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

the class FlipIfIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!element.getNode().getElementType().equals(GroovyTokenTypes.kIF))
                return false;
            if (!(element.getParent() instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = DefaultGroovyMethods.asType(element.getParent(), GrIfStatement.class);
            final GrIfStatement elseIf = getElseIf(ifStatement);
            return elseIf != null && checkIf(ifStatement) && checkIf(elseIf);
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PsiElementPredicate

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

the class ImportOnDemandIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrReferenceElement))
                return false;
            final GrReferenceElement ref = (GrReferenceElement) element;
            final PsiElement parent = ref.getParent();
            if (!(parent instanceof GrReferenceElement))
                return false;
            final PsiElement resolved = ref.resolve();
            if (resolved == null)
                return false;
            return resolved instanceof PsiClass;
        }
    };
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) PsiElement(com.intellij.psi.PsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PsiElementPredicate

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

the class ConvertJavaStyleArrayIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrMethodCallExpression))
                return false;
            final GrExpression expression = ((GrMethodCallExpression) element).getInvokedExpression();
            if (!(expression instanceof GrNewExpression))
                return false;
            if (((GrNewExpression) expression).getArrayCount() == 0)
                return false;
            if (!((GrMethodCallExpression) element).getArgumentList().getText().trim().isEmpty())
                return false;
            final GrClosableBlock[] closureArguments = ((GrMethodCallExpression) element).getClosureArguments();
            if (closureArguments.length != 1)
                return false;
            final GrClosableBlock block = closureArguments[0];
            if (block.getLBrace() == null || block.getRBrace() == null)
                return false;
            return true;
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) 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