Search in sources :

Example 71 with GrReferenceExpression

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

the class GrReferenceAdjuster method process.

private static boolean process(@NotNull PsiElement element, int start, int end, boolean addImports, boolean incomplete, boolean useFqInJavadoc, boolean useFqInCode) {
    boolean result = false;
    if (element instanceof GrQualifiedReference<?> && ((GrQualifiedReference) element).resolve() instanceof PsiClass) {
        result = shortenReferenceInner((GrQualifiedReference<?>) element, addImports, incomplete, useFqInJavadoc, useFqInCode);
    } else if (element instanceof GrReferenceExpression && PsiUtil.isSuperReference(((GrReferenceExpression) element).getQualifier())) {
        result = shortenReferenceInner((GrReferenceExpression) element, addImports, incomplete, useFqInJavadoc, useFqInCode);
    }
    PsiElement child = element.getFirstChild();
    while (child != null) {
        final TextRange range = child.getTextRange();
        if (start < range.getEndOffset() && range.getStartOffset() < end) {
            result |= process(child, start, end, addImports, incomplete, useFqInJavadoc, useFqInCode);
        }
        child = child.getNextSibling();
    }
    return result;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 72 with GrReferenceExpression

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

the class GroovyMapValueTypeEnhancer method getReferenceType.

@Override
public PsiType getReferenceType(GrReferenceExpression ref, @Nullable PsiElement resolved) {
    if (resolved != null)
        return null;
    GrExpression qualifierExpression = ref.getQualifierExpression();
    if (qualifierExpression == null)
        return null;
    PsiType mapType = qualifierExpression.getType();
    if (!InheritanceUtil.isInheritor(mapType, CommonClassNames.JAVA_UTIL_MAP)) {
        return null;
    }
    PsiElement qResolved;
    if (qualifierExpression instanceof GrReferenceExpression) {
        qResolved = ((GrReferenceExpression) qualifierExpression).resolve();
    } else if (qualifierExpression instanceof GrMethodCall) {
        qResolved = ((GrMethodCall) qualifierExpression).resolveMethod();
    } else {
        return null;
    }
    String key = ref.getReferenceName();
    if (key == null)
        return null;
    for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
        PsiType type = provider.getValueType(qualifierExpression, qResolved, key);
        if (type != null) {
            return type;
        }
    }
    if (mapType instanceof GrMapType) {
        return ((GrMapType) mapType).getTypeByStringKey(key);
    }
    return null;
}
Also used : GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyMapContentProvider(org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 73 with GrReferenceExpression

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

the class GroovyConstructorNamedArgumentProvider method getNamedArguments.

@Override
public void getNamedArguments(@NotNull GrCall call, @NotNull GroovyResolveResult resolveResult, @Nullable String argumentName, boolean forCompletion, @NotNull Map<String, NamedArgumentDescriptor> result) {
    if (!(call instanceof GrNewExpression))
        return;
    PsiElement resolve = resolveResult.getElement();
    if (resolve != null) {
        if (!(resolve instanceof PsiMethod))
            return;
        PsiMethod method = (PsiMethod) resolve;
        if (!method.isConstructor())
            return;
    }
    GrNewExpression newCall = (GrNewExpression) call;
    GrArgumentList argumentList = newCall.getArgumentList();
    if (argumentList == null)
        return;
    GrExpression[] expressionArguments = argumentList.getExpressionArguments();
    if (expressionArguments.length > 1 || (expressionArguments.length == 1 && !(expressionArguments[0] instanceof GrReferenceExpression))) {
        return;
    }
    for (GroovyResolveResult newResult : newCall.multiResolveClass()) {
        PsiElement element = newResult.getElement();
        if (!(element instanceof PsiClass))
            continue;
        PsiClass aClass = (PsiClass) element;
        if (!isClassHasConstructorWithMap(aClass))
            continue;
        PsiClassType classType = JavaPsiFacade.getElementFactory(aClass.getProject()).createType(aClass);
        processClass(call, classType, argumentName, result);
    }
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 74 with GrReferenceExpression

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

the class GrNamedArgumentSearchVisitor method visitReferenceExpression.

@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
    if (myFirstArgumentName.equals(referenceExpression.getReferenceName()) && !referenceExpression.isQualified()) {
        PsiElement parent = referenceExpression.getParent();
        if (parent instanceof GrReferenceExpression) {
            GrReferenceExpression parentRef = (GrReferenceExpression) parent;
            PsiElement parentParent = parentRef.getParent();
            if (parentParent instanceof GrMethodCallExpression) {
                if (METHOD_NAMES.contains(parentRef.getReferenceName())) {
                    extractArguments(((GrMethodCallExpression) parentParent).getArgumentList());
                }
            } else {
                add(parentRef.getReferenceName());
            }
        } else if (parent instanceof GrIndexProperty) {
            GrIndexProperty indexProperty = (GrIndexProperty) parent;
            extractArguments(indexProperty.getArgumentList());
        }
    }
    super.visitReferenceExpression(referenceExpression);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 75 with GrReferenceExpression

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

the class GroovyReadWriteAccessDetector method getExpressionAccess.

@NotNull
@Override
public Access getExpressionAccess(@NotNull PsiElement expression) {
    if (expression instanceof GrExpression) {
        GrExpression expr = (GrExpression) expression;
        boolean readAccess = PsiUtil.isAccessedForReading(expr);
        boolean writeAccess = PsiUtil.isAccessedForWriting(expr);
        if (!writeAccess && expr instanceof GrReferenceExpression) {
            //when searching usages of fields, should show all found setters as a "only write usage"
            PsiElement actualReferee = ((GrReferenceExpression) expr).resolve();
            if (actualReferee instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) actualReferee)) {
                writeAccess = true;
                readAccess = false;
            }
        }
        if (writeAccess && readAccess)
            return Access.ReadWrite;
        return writeAccess ? Access.Write : Access.Read;
    } else if (expression instanceof PsiExpression) {
        PsiExpression expr = (PsiExpression) expression;
        boolean readAccess = com.intellij.psi.util.PsiUtil.isAccessedForReading(expr);
        boolean writeAccess = com.intellij.psi.util.PsiUtil.isAccessedForWriting(expr);
        if (!writeAccess && expr instanceof PsiReferenceExpression) {
            //when searching usages of fields, should show all found setters as a "only write usage"
            PsiElement actualReferee = ((PsiReferenceExpression) expr).resolve();
            if (actualReferee instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) actualReferee)) {
                writeAccess = true;
                readAccess = false;
            }
        }
        if (writeAccess && readAccess)
            return Access.ReadWrite;
        return writeAccess ? Access.Write : Access.Read;
    } else {
        return Access.Read;
    }
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11