Search in sources :

Example 46 with GroovyResolveResult

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

the class GppClosureParameterTypeProvider method getClosureParameterType.

@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
    final PsiElement parent = closure.getParent();
    if (parent instanceof GrNamedArgument) {
        final Pair<PsiMethod, PsiSubstitutor> pair = getOverriddenMethod((GrNamedArgument) parent);
        if (pair != null) {
            final PsiParameter[] parameters = pair.first.getParameterList().getParameters();
            if (parameters.length > index) {
                return pair.second.substitute(parameters[index].getType());
            }
            return null;
        }
    }
    if (parent instanceof GrListOrMap) {
        final GrListOrMap list = (GrListOrMap) parent;
        if (!list.isMap()) {
            final PsiType listType = list.getType();
            final int argIndex = Arrays.asList(list.getInitializers()).indexOf(closure);
            assert argIndex >= 0;
            if (listType instanceof GrTupleType) {
                for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
                    if (!(type instanceof PsiClassType))
                        continue;
                    final GroovyResolveResult[] candidates = PsiUtil.getConstructorCandidates((PsiClassType) type, ((GrTupleType) listType).getComponentTypes(), closure);
                    for (GroovyResolveResult resolveResult : candidates) {
                        final PsiElement method = resolveResult.getElement();
                        if (!(method instanceof PsiMethod) || !((PsiMethod) method).isConstructor())
                            continue;
                        final PsiParameter[] parameters = ((PsiMethod) method).getParameterList().getParameters();
                        if (parameters.length <= argIndex)
                            continue;
                        final PsiType toCastTo = resolveResult.getSubstitutor().substitute(parameters[argIndex].getType());
                        final PsiType suggestion = getSingleMethodParameterType(toCastTo, index, closure);
                        if (suggestion != null)
                            return suggestion;
                    }
                }
            }
            return null;
        }
    }
    for (PsiType constraint : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
        final PsiType suggestion = getSingleMethodParameterType(constraint, index, closure);
        if (suggestion != null) {
            return suggestion;
        }
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 47 with GroovyResolveResult

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

the class GppExpectedTypesContributor method addExpectedConstructorParameters.

private static List<TypeConstraint> addExpectedConstructorParameters(GrListOrMap list, GrExpression[] args, GrExpression arg) {
    PsiType[] argTypes = ContainerUtil.map2Array(args, PsiType.class, (NullableFunction<GrExpression, PsiType>) grExpression -> grExpression.getType());
    final ArrayList<TypeConstraint> result = new ArrayList<>();
    for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
        if (type instanceof PsiClassType) {
            for (GroovyResolveResult resolveResult : PsiUtil.getConstructorCandidates((PsiClassType) type, argTypes, list)) {
                final PsiElement method = resolveResult.getElement();
                if (method instanceof PsiMethod && ((PsiMethod) method).isConstructor()) {
                    final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, list, false, true, GrNamedArgument.EMPTY_ARRAY, args, GrClosableBlock.EMPTY_ARRAY);
                    if (map != null) {
                        final Pair<PsiParameter, PsiType> pair = map.get(arg);
                        if (pair != null) {
                            result.add(SubtypeConstraint.create(pair.second));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) GroovyExpectedTypesContributor(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesContributor) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) List(java.util.List) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair) SubtypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SubtypeConstraint) com.intellij.psi(com.intellij.psi) GroovyExpectedTypesProvider(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesProvider) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) Pair(com.intellij.openapi.util.Pair)

Example 48 with GroovyResolveResult

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

the class LiteralConstructorReference method multiResolve.

@NotNull
@Override
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
    PsiClassType type = getConstructedClassType();
    if (type == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    final PsiClassType.ClassResolveResult classResolveResult = type.resolveGenerics();
    final GroovyResolveResult[] constructorCandidates = PsiUtil.getConstructorCandidates(type, getCallArgumentTypes(), getElement());
    if (constructorCandidates.length == 0) {
        final GroovyResolveResult result = GroovyResolveResultImpl.from(classResolveResult);
        if (result != GroovyResolveResult.EMPTY_RESULT)
            return new GroovyResolveResult[] { result };
    }
    return constructorCandidates;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with GroovyResolveResult

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

the class GrClosureType method create.

public static GrClosureType create(GroovyResolveResult[] results, GroovyPsiElement context) {
    List<GrClosureSignature> signatures = new ArrayList<>();
    for (GroovyResolveResult result : results) {
        if (result.getElement() instanceof PsiMethod) {
            signatures.add(GrClosureSignatureUtil.createSignature((PsiMethod) result.getElement(), result.getSubstitutor()));
        }
    }
    final GlobalSearchScope resolveScope = context.getResolveScope();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(context.getProject());
    if (signatures.size() == 1) {
        return create(signatures.get(0), resolveScope, facade, LanguageLevel.JDK_1_5, true);
    } else {
        return create(GrClosureSignatureUtil.createMultiSignature(signatures.toArray(new GrClosureSignature[signatures.size()])), resolveScope, facade, LanguageLevel.JDK_1_5, true);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 50 with GroovyResolveResult

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

the class GrAnnotationImpl method getNameReferenceElement.

@Override
@Nullable
public PsiJavaCodeReferenceElement getNameReferenceElement() {
    final GroovyResolveResult resolveResult = getClassReference().advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    if (!(resolved instanceof PsiClass))
        return null;
    return new LightClassReference(getManager(), getClassReference().getText(), (PsiClass) resolved, resolveResult.getSubstitutor());
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) LightClassReference(com.intellij.psi.impl.light.LightClassReference) Nullable(org.jetbrains.annotations.Nullable)

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