Search in sources :

Example 71 with GrExpression

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

the class GroovyIntroduceParameterUtil method processChangedMethodCall.

public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
    if (!(element.getParent() instanceof GrMethodCallExpression)) {
        LOG.error(element.getParent());
        return;
    }
    GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final String name = settings.getName();
    LOG.assertTrue(name != null);
    GrExpression expression = factory.createExpressionFromText(name, null);
    final GrArgumentList argList = methodCall.getArgumentList();
    final PsiElement[] exprs = argList.getAllArguments();
    if (exprs.length > 0) {
        argList.addAfter(expression, exprs[exprs.length - 1]);
    } else {
        argList.add(expression);
    }
    removeParametersFromCall(methodCall, settings);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 72 with GrExpression

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

the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.

@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
    if (callExpression == null)
        return true;
    GrArgumentList argList = callExpression.getArgumentList();
    GrExpression[] oldArgs = argList.getExpressionArguments();
    final GrExpression anchor;
    if (!data.getMethodToSearchFor().isVarArgs()) {
        anchor = getLast(oldArgs);
    } else {
        final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
        if (parameters.length > oldArgs.length) {
            anchor = getLast(oldArgs);
        } else {
            final int lastNonVararg = parameters.length - 2;
            anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
        }
    }
    PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
    if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
        argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
    } else {
        final PsiElement _expr = data.getParameterInitializer().getExpression();
        PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
        new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newArg can be replaced by OldReferenceResolver
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        removeParamsFromUnresolvedCall(callExpression, data);
    } else {
        removeParametersFromCall(actualArgs, data.getParametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
    return false;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 73 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression 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 74 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression 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)

Example 75 with GrExpression

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

the class GrRefactoringConflictsUtil method checkUsedElements.

public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
    final Set<PsiMember> moving = new HashSet<>(membersToMove);
    if (abstractMethods != null) {
        moving.addAll(abstractMethods);
    }
    if (scope instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                GrExpression qualifier = refExpr.getQualifierExpression();
                PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
            }
        }
    } else if (scope instanceof GrNewExpression) {
        final GrNewExpression newExpression = (GrNewExpression) scope;
        final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
        if (anonymousClass != null) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
            }
        } else {
            final PsiMethod refElement = newExpression.resolveMethod();
            if (refElement != null) {
                if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                    RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
                }
            }
        }
    } else if (scope instanceof GrCodeReferenceElement) {
        GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
            }
        }
    }
    for (PsiElement child : scope.getChildren()) {
        if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
            continue;
        checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
    }
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16