Search in sources :

Example 6 with GrIndexProperty

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

the class TypeInferenceHelper method getInitializerTypeFor.

@Nullable
public static PsiType getInitializerTypeFor(PsiElement element) {
    final PsiElement parent = skipParentheses(element.getParent(), true);
    if (parent instanceof GrAssignmentExpression) {
        if (element instanceof GrIndexProperty) {
            final GrExpression rvalue = ((GrAssignmentExpression) parent).getRValue();
            //don't try to infer assignment type in case of index property because of infinite recursion (example: a[2]+=4)
            return rvalue != null ? rvalue.getType() : null;
        }
        return ((GrAssignmentExpression) parent).getType();
    }
    if (parent instanceof GrTupleExpression) {
        GrTupleExpression list = (GrTupleExpression) parent;
        if (list.getParent() instanceof GrAssignmentExpression) {
            // multiple assignment
            final GrExpression rValue = ((GrAssignmentExpression) list.getParent()).getRValue();
            int idx = list.indexOf(element);
            if (idx >= 0 && rValue != null) {
                PsiType rType = rValue.getType();
                if (rType instanceof GrTupleType) {
                    PsiType[] componentTypes = ((GrTupleType) rType).getComponentTypes();
                    if (idx < componentTypes.length)
                        return componentTypes[idx];
                    return null;
                }
                return PsiUtil.extractIterableTypeParameter(rType, false);
            }
        }
    }
    if (parent instanceof GrUnaryExpression && TokenSets.POSTFIX_UNARY_OP_SET.contains(((GrUnaryExpression) parent).getOperationTokenType())) {
        return ((GrUnaryExpression) parent).getType();
    }
    return null;
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrIndexProperty

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

the class GroovyTypeCheckVisitor method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(@NotNull GrAssignmentExpression assignment) {
    super.visitAssignmentExpression(assignment);
    final GrExpression lValue = assignment.getLValue();
    if (lValue instanceof GrIndexProperty)
        return;
    if (!PsiUtil.mightBeLValue(lValue))
        return;
    final IElementType opToken = assignment.getOperationTokenType();
    if (opToken != GroovyTokenTypes.mASSIGN)
        return;
    final GrExpression rValue = assignment.getRValue();
    if (rValue == null)
        return;
    if (lValue instanceof GrReferenceExpression && ((GrReferenceExpression) lValue).resolve() instanceof GrReferenceExpression) {
        //lvalue is not-declared variable
        return;
    }
    if (lValue instanceof GrTupleExpression) {
        processTupleAssignment(((GrTupleExpression) lValue), rValue);
    } else {
        PsiType lValueNominalType = lValue.getNominalType();
        final PsiType targetType = PsiImplUtil.isSpreadAssignment(lValue) ? extractIterableTypeParameter(lValueNominalType, false) : lValueNominalType;
        if (targetType != null) {
            processAssignment(targetType, rValue, lValue, "cannot.assign", assignment, ApplicableTo.ASSIGNMENT);
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty)

Example 8 with GrIndexProperty

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

the class PsiUtil method getArgumentTypes.

@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
    PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
    if (parent instanceof GrIndexProperty) {
        GrIndexProperty index = (GrIndexProperty) parent;
        PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
        if (isLValue(index) && argTypes != null) {
            PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
            PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
            return ArrayUtil.append(argTypes, initializer);
        } else {
            return argTypes;
        }
    }
    if (parent instanceof GrCall) {
        GrCall call = (GrCall) parent;
        GrNamedArgument[] namedArgs = call.getNamedArguments();
        GrExpression[] expressions = call.getExpressionArguments();
        GrClosableBlock[] closures = call.getClosureArguments();
        return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
        if (argList == null) {
            return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        } else {
            return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        }
    } else if (parent instanceof GrBinaryExpression) {
        GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
        PsiType type = right != null ? right.getType() : null;
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    } else if (parent instanceof GrAssignmentExpression) {
        PsiType type = ((GrAssignmentExpression) parent).getType();
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrIndexProperty

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

the class IndexedExpressionConversionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
    final GrArgumentList argList = (GrArgumentList) arrayIndexExpression.getLastChild();
    assert argList != null;
    final GrExpression[] arguments = argList.getExpressionArguments();
    final PsiElement parent = element.getParent();
    final GrExpression arrayExpression = arrayIndexExpression.getInvokedExpression();
    if (!(parent instanceof GrAssignmentExpression)) {
        rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
        return;
    }
    final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
    final GrExpression rhs = assignmentExpression.getRValue();
    if (rhs.equals(element)) {
        rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
    } else {
        rewriteAsSetAt(assignmentExpression, arrayExpression, arguments[0], rhs);
    }
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Example 10 with GrIndexProperty

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

the class IndexedExpressionConversionPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrIndexProperty))
        return false;
    if (ErrorUtil.containsError(element))
        return false;
    final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
    final PsiElement lastChild = arrayIndexExpression.getLastChild();
    if (!(lastChild instanceof GrArgumentList))
        return false;
    final GrArgumentList argList = (GrArgumentList) lastChild;
    final GrExpression[] arguments = argList.getExpressionArguments();
    if (arguments.length != 1)
        return false;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrAssignmentExpression)) {
        return true;
    }
    final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
    final GrExpression rvalue = assignmentExpression.getRValue();
    if (rvalue == null)
        return false;
    if (rvalue.equals(element))
        return true;
    final IElementType operator = assignmentExpression.getOperationTokenType();
    return GroovyTokenTypes.mASSIGN.equals(operator);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrIndexProperty (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty)11 IElementType (com.intellij.psi.tree.IElementType)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 GrBindingVariable (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable)2 ASTNode (com.intellij.lang.ASTNode)1 PsiType (com.intellij.psi.PsiType)1 LightElement (com.intellij.psi.impl.light.LightElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrSpreadArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1