use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitorHelper method getExpressionArgumentsOfCall.
@Nullable
public static List<GrExpression> getExpressionArgumentsOfCall(@NotNull GrArgumentList argumentList) {
final ArrayList<GrExpression> args = ContainerUtil.newArrayList();
for (GroovyPsiElement arg : argumentList.getAllArguments()) {
if (arg instanceof GrSpreadArgument) {
GrExpression spreaded = ((GrSpreadArgument) arg).getArgument();
if (spreaded instanceof GrListOrMap && !((GrListOrMap) spreaded).isMap()) {
Collections.addAll(args, ((GrListOrMap) spreaded).getInitializers());
} else {
return null;
}
} else if (arg instanceof GrExpression) {
args.add((GrExpression) arg);
} else if (arg instanceof GrNamedArgument) {
args.add(((GrNamedArgument) arg).getExpression());
}
}
final PsiElement parent = argumentList.getParent();
if (parent instanceof GrIndexProperty && PsiUtil.isLValue((GroovyPsiElement) parent)) {
args.add(TypeInferenceHelper.getInitializerFor((GrExpression) parent));
} else if (parent instanceof GrMethodCallExpression) {
ContainerUtil.addAll(args, ((GrMethodCallExpression) parent).getClosureArguments());
}
return args;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap 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.auxiliary.GrListOrMap 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.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class TypeInferenceHelper method getInitializerFor.
@Nullable
public static GrExpression getInitializerFor(GrExpression lValue) {
final PsiElement parent = lValue.getParent();
if (parent instanceof GrAssignmentExpression)
return ((GrAssignmentExpression) parent).getRValue();
if (parent instanceof GrTupleExpression) {
final int i = ((GrTupleExpression) parent).indexOf(lValue);
final PsiElement pparent = parent.getParent();
org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.LOG.assertTrue(pparent instanceof GrAssignmentExpression);
final GrExpression rValue = ((GrAssignmentExpression) pparent).getRValue();
if (rValue instanceof GrListOrMap && !((GrListOrMap) rValue).isMap()) {
final GrExpression[] initializers = ((GrListOrMap) rValue).getInitializers();
if (initializers.length < i)
return initializers[i];
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyLanguageInjectionSupport method getTopLevelInjectionTarget.
@NotNull
public static PsiElement getTopLevelInjectionTarget(@NotNull final PsiElement host) {
PsiElement target = host;
PsiElement parent = target.getParent();
for (; parent != null; target = parent, parent = target.getParent()) {
if (parent instanceof GrBinaryExpression)
continue;
if (parent instanceof GrString)
continue;
if (parent instanceof GrParenthesizedExpression)
continue;
if (parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != target)
continue;
if (parent instanceof GrAnnotationArrayInitializer)
continue;
if (parent instanceof GrListOrMap) {
parent = parent.getParent();
continue;
}
break;
}
return target;
}
Aggregations