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;
}
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;
}
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;
}
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);
}
}
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());
}
Aggregations