Search in sources :

Example 46 with GrReferenceExpression

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

the class RenameAliasImportedMethodProcessor method renameElement.

@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
    boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
    List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
    List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
            propertyAccess.add(usage);
        } else {
            methodAccess.add(usage);
        }
    }
    super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
    final String propertyName;
    if (isGetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
    } else if (isSetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
    } else {
        propertyName = null;
    }
    if (propertyName == null) {
        //it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        for (UsageInfo info : propertyAccess) {
            final PsiElement element = info.getElement();
            if (element instanceof GrReferenceExpression) {
                final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
                String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
                if (isGetter) {
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
                    ((GrReferenceExpression) element).replaceWithExpression(call, true);
                } else {
                    final PsiElement parent = element.getParent();
                    assert parent instanceof GrAssignmentExpression;
                    final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
                    ((GrAssignmentExpression) parent).replaceWithExpression(call, true);
                }
            }
        }
    } else {
        for (UsageInfo usage : propertyAccess) {
            final PsiReference ref = usage.getReference();
            if (ref != null) {
                ref.handleElementRename(propertyName);
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 47 with GrReferenceExpression

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

the class GrIntroduceLocalVariableProcessor method resolveLocalConflicts.

private static void resolveLocalConflicts(@NotNull PsiElement tempContainer, @NotNull String varName) {
    for (PsiElement child : tempContainer.getChildren()) {
        if (child instanceof GrReferenceExpression && !child.getText().contains(".")) {
            PsiReference psiReference = child.getReference();
            if (psiReference != null) {
                final PsiElement resolved = psiReference.resolve();
                if (resolved != null) {
                    String fieldName = getFieldName(resolved);
                    if (fieldName != null && varName.equals(fieldName)) {
                        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tempContainer.getProject());
                        ((GrReferenceExpression) child).replaceWithExpression(factory.createExpressionFromText("this." + child.getText()), true);
                    }
                }
            }
        } else {
            resolveLocalConflicts(child, varName);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 48 with GrReferenceExpression

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

the class CompleteReferenceExpression method getVariantsFromQualifier.

private void getVariantsFromQualifier(@NotNull GrExpression qualifier) {
    Project project = qualifier.getProject();
    final PsiType qualifierType = TypesUtil.boxPrimitiveType(qualifier.getType(), qualifier.getManager(), qualifier.getResolveScope());
    final ResolveState state = ResolveState.initial();
    if (qualifierType == null || PsiType.VOID.equals(qualifierType)) {
        if (qualifier instanceof GrReferenceExpression) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) {
                resolved.processDeclarations(myProcessor, state, null, myRefExpr);
                return;
            }
        }
        getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project);
    } else if (qualifierType instanceof PsiIntersectionType) {
        for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
            getVariantsFromQualifierType(conjunct, project);
        }
    } else if (qualifierType instanceof GrTraitType) {
        // Process trait type conjuncts in reversed order because last applied trait matters.
        PsiType[] conjuncts = ((GrTraitType) qualifierType).getConjuncts();
        for (int i = conjuncts.length - 1; i >= 0; i--) {
            getVariantsFromQualifierType(conjuncts[i], project);
        }
    } else {
        getVariantsFromQualifierType(qualifierType, project);
        if (qualifier instanceof GrReferenceExpression && !PsiUtil.isSuperReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier)) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiClass) {
                ////omitted .class
                GlobalSearchScope scope = myRefExpr.getResolveScope();
                PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope);
                if (javaLangClass != null) {
                    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
                    PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters();
                    if (typeParameters.length == 1) {
                        substitutor = substitutor.put(typeParameters[0], qualifierType);
                    }
                    PsiType javaLangClassType = JavaPsiFacade.getElementFactory(myRefExpr.getProject()).createType(javaLangClass, substitutor);
                    ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr);
                }
            }
        }
    }
}
Also used : GrTraitType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 49 with GrReferenceExpression

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

the class CompleteReferenceExpression method getBindings.

private void getBindings() {
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
    if (containingClass != null)
        return;
    final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
    if (file instanceof GroovyFile) {
        ((GroovyFile) file).accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitAssignmentExpression(@NotNull GrAssignmentExpression expression) {
                super.visitAssignmentExpression(expression);
                final GrExpression value = expression.getLValue();
                if (value instanceof GrReferenceExpression && !((GrReferenceExpression) value).isQualified()) {
                    final PsiElement resolved = ((GrReferenceExpression) value).resolve();
                    if (resolved instanceof GrBindingVariable) {
                        myProcessor.execute(resolved, ResolveState.initial());
                    } else if (resolved == null) {
                        myProcessor.execute(new GrBindingVariable((GroovyFile) file, ((GrReferenceExpression) value).getReferenceName(), true), ResolveState.initial());
                    }
                }
            }

            @Override
            public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
            //don't go into classes
            }
        });
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 50 with GrReferenceExpression

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

the class GdslClosureCompleter method getParameterInfos.

@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
    final ArrayList<ClosureDescriptor> descriptors = new ArrayList<>();
    GrReferenceExpression ref = (GrReferenceExpression) place;
    PsiType qtype = PsiImplUtil.getQualifierType(ref);
    if (qtype == null)
        return null;
    GrExpression qualifier = ref.getQualifier();
    if (qualifier != null) {
        PsiType type = qualifier.getType();
        if (type == null)
            return null;
        processExecutors(qtype, ref, descriptors);
    } else {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
        for (PsiElement parent = ref.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof GrClosableBlock) {
                processExecutors(TypesUtil.createTypeByFQClassName(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, ref), ref, descriptors);
            } else if (parent instanceof GrTypeDefinition) {
                processExecutors(factory.createType(((GrTypeDefinition) parent), PsiType.EMPTY_ARRAY), ref, descriptors);
            }
        }
    }
    for (ClosureDescriptor descriptor : descriptors) {
        if (descriptor.isMethodApplicable(method, ref)) {
            return descriptor.getParameters();
        }
    }
    return null;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ArrayList(java.util.ArrayList) ClosureDescriptor(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

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