Search in sources :

Example 71 with GroovyResolveResult

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

the class ResolveUtil method filterSameSignatureCandidates.

/**
   * The point is that we do not want to see repeating methods in completion.
   * Candidates can have multiple toString() methods (e.g. from Object and from some inheritor) and we want to show only one.
   */
public static GroovyResolveResult[] filterSameSignatureCandidates(Collection<? extends GroovyResolveResult> candidates) {
    if (candidates.size() == 0)
        return GroovyResolveResult.EMPTY_ARRAY;
    if (candidates.size() == 1)
        return candidates.toArray(new GroovyResolveResult[candidates.size()]);
    final List<GroovyResolveResult> result = new ArrayList<>();
    final Iterator<? extends GroovyResolveResult> allIterator = candidates.iterator();
    result.add(allIterator.next());
    Outer: while (allIterator.hasNext()) {
        final GroovyResolveResult currentResult = allIterator.next();
        final PsiMethod currentMethod;
        final PsiSubstitutor currentSubstitutor;
        if (currentResult instanceof GroovyMethodResult) {
            final GroovyMethodResult currentMethodResult = (GroovyMethodResult) currentResult;
            currentMethod = currentMethodResult.getElement();
            currentSubstitutor = currentMethodResult.getSubstitutor(false);
        } else if (currentResult.getElement() instanceof PsiMethod) {
            currentMethod = (PsiMethod) currentResult.getElement();
            currentSubstitutor = currentResult.getSubstitutor();
        } else {
            result.add(currentResult);
            continue;
        }
        Inner: for (Iterator<GroovyResolveResult> resultIterator = result.iterator(); resultIterator.hasNext(); ) {
            final GroovyResolveResult otherResult = resultIterator.next();
            final PsiMethod otherMethod;
            final PsiSubstitutor otherSubstitutor;
            if (otherResult instanceof GroovyMethodResult) {
                final GroovyMethodResult otherMethodResult = (GroovyMethodResult) otherResult;
                otherMethod = otherMethodResult.getElement();
                otherSubstitutor = otherMethodResult.getSubstitutor(false);
            } else if (otherResult.getElement() instanceof PsiMethod) {
                otherMethod = (PsiMethod) otherResult.getElement();
                otherSubstitutor = otherResult.getSubstitutor();
            } else {
                continue Inner;
            }
            if (dominated(currentMethod, currentSubstitutor, otherMethod, otherSubstitutor)) {
                // then do not add current method to result and skip rest other methods
                continue Outer;
            } else if (dominated(otherMethod, otherSubstitutor, currentMethod, currentSubstitutor)) {
                // if other method is dominated by current method
                // then remove other from result
                resultIterator.remove();
            }
        }
        result.add(currentResult);
    }
    return result.toArray(new GroovyResolveResult[result.size()]);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyMethodResult(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyMethodResult)

Example 72 with GroovyResolveResult

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

the class ResolveUtil method resolveExistingElement.

@Nullable
public static <T> T resolveExistingElement(PsiElement place, ResolverProcessor processor, Class<? extends T>... classes) {
    treeWalkUp(place, processor, true);
    final GroovyResolveResult[] candidates = processor.getCandidates();
    for (GroovyResolveResult candidate : candidates) {
        final PsiElement element = candidate.getElement();
        if (element == place)
            continue;
        for (Class<? extends T> clazz : classes) {
            if (//noinspection unchecked
            clazz.isInstance(element))
                return (T) element;
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with GroovyResolveResult

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

the class ResolveUtil method getMethodCandidates.

@NotNull
public static GroovyResolveResult[] getMethodCandidates(@NotNull PsiType thisType, @Nullable String methodName, @NotNull PsiElement place, boolean resolveClosures, boolean allVariants, @Nullable PsiType... argumentTypes) {
    if (methodName == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    thisType = TypesUtil.boxPrimitiveType(thisType, place.getManager(), place.getResolveScope());
    MethodResolverProcessor processor = new MethodResolverProcessor(methodName, place, false, thisType, argumentTypes, PsiType.EMPTY_ARRAY, allVariants);
    final ResolveState state = ResolveState.initial().put(ClassHint.RESOLVE_CONTEXT, place);
    processAllDeclarations(thisType, processor, state, place);
    boolean hasApplicableMethods = processor.hasApplicableCandidates();
    final GroovyResolveResult[] methodCandidates = processor.getCandidates();
    if (hasApplicableMethods && methodCandidates.length == 1)
        return methodCandidates;
    final GroovyResolveResult[] allPropertyCandidates;
    if (resolveClosures) {
        PropertyResolverProcessor propertyResolver = new PropertyResolverProcessor(methodName, place);
        processAllDeclarations(thisType, propertyResolver, state, place);
        allPropertyCandidates = propertyResolver.getCandidates();
    } else {
        allPropertyCandidates = GroovyResolveResult.EMPTY_ARRAY;
    }
    List<GroovyResolveResult> propertyCandidates = new ArrayList<>(allPropertyCandidates.length);
    for (GroovyResolveResult candidate : allPropertyCandidates) {
        final PsiElement resolved = candidate.getElement();
        if (!(resolved instanceof GrField))
            continue;
        final PsiType type = ((GrField) resolved).getTypeGroovy();
        if (isApplicableClosureType(type, argumentTypes, place)) {
            propertyCandidates.add(candidate);
        }
    }
    for (GroovyResolveResult candidate : propertyCandidates) {
        final PsiElement element = candidate.getElement();
        if (element instanceof GrField) {
            final PsiClass containingClass = ((PsiField) element).getContainingClass();
            if (containingClass != null && PsiTreeUtil.isContextAncestor(containingClass, place, true)) {
                return new GroovyResolveResult[] { candidate };
            }
        }
    }
    List<GroovyResolveResult> allCandidates = new ArrayList<>();
    if (hasApplicableMethods) {
        ContainerUtil.addAll(allCandidates, methodCandidates);
    }
    ContainerUtil.addAll(allCandidates, propertyCandidates);
    //search for getters
    for (String getterName : GroovyPropertyUtils.suggestGettersName(methodName)) {
        AccessorResolverProcessor getterResolver = new AccessorResolverProcessor(getterName, methodName, place, true, thisType, PsiType.EMPTY_ARRAY);
        processAllDeclarations(thisType, getterResolver, state, place);
        //can be only one candidate
        final GroovyResolveResult[] candidates = getterResolver.getCandidates();
        final List<GroovyResolveResult> applicable = new ArrayList<>();
        for (GroovyResolveResult candidate : candidates) {
            PsiMethod method = (PsiMethod) candidate.getElement();
            assert method != null;
            final PsiType type = org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.getSmartReturnType(method);
            if (isApplicableClosureType(type, argumentTypes, place)) {
                applicable.add(candidate);
            }
        }
        if (applicable.size() == 1) {
            return applicable.toArray(new GroovyResolveResult[applicable.size()]);
        }
        ContainerUtil.addAll(allCandidates, applicable);
    }
    if (!allCandidates.isEmpty()) {
        return allCandidates.toArray(new GroovyResolveResult[allCandidates.size()]);
    } else if (!hasApplicableMethods) {
        return methodCandidates;
    }
    return GroovyResolveResult.EMPTY_ARRAY;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 74 with GroovyResolveResult

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

the class ResolveUtil method getCallVariants.

public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
    final PsiElement parent = place.getParent();
    GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
    if (parent instanceof GrCallExpression) {
        variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
    } else if (parent instanceof GrConstructorInvocation) {
        final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
        if (clazz != null) {
            final PsiMethod[] constructors = clazz.getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
        if (element instanceof PsiClass) {
            final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (place instanceof GrReferenceExpression) {
        variants = ((GrReferenceExpression) place).getSameNameVariants();
    }
    return variants;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 75 with GroovyResolveResult

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

the class ResolveUtil method getConstructorResolveResult.

private static GroovyResolveResult[] getConstructorResolveResult(PsiMethod[] constructors, PsiElement place) {
    GroovyResolveResult[] variants = new GroovyResolveResult[constructors.length];
    for (int i = 0; i < constructors.length; i++) {
        final boolean isAccessible = PsiUtil.isAccessible(constructors[i], place, null);
        variants[i] = new GroovyResolveResultImpl(constructors[i], isAccessible);
    }
    return variants;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6