use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class ArgumentListGenerator method generate.
public void generate(@Nullable GrClosureSignature signature, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] clArgs, @NotNull GroovyPsiElement context) {
GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = signature == null ? null : GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, false, false);
if (argInfos == null && signature != null) {
argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, true, true);
}
final PsiSubstitutor substitutor = signature == null ? PsiSubstitutor.EMPTY : signature.getSubstitutor();
if (argInfos == null || NullUtils.hasNull(argInfos)) {
generateSimple(exprs, namedArgs, clArgs, context, substitutor);
return;
}
final GrClosureParameter[] params = signature.getParameters();
final Project project = context.getProject();
myBuilder.append('(');
boolean hasCommaAtEnd = false;
for (int i = 0; i < argInfos.length; i++) {
GrClosureSignatureUtil.ArgInfo<PsiElement> arg = argInfos[i];
if (arg == null)
continue;
final GrClosureParameter param = params[i];
boolean generated = arg.isMultiArg ? generateMultiArg(arg, param, substitutor, project, context) : generateSingeArg(arg, param);
if (generated) {
hasCommaAtEnd = true;
myBuilder.append(", ");
}
}
if (hasCommaAtEnd) {
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
//myBuilder.removeFromTheEnd(2);
}
myBuilder.append(')');
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class ArgumentInstruction method inferMixinType.
@Override
@Nullable
public PsiType inferMixinType() {
PsiElement element = getElement();
assert element != null;
GrCall call = findCall(element);
GrExpression[] arguments = call.getExpressionArguments();
boolean hasNamed = PsiImplUtil.hasNamedArguments(call.getArgumentList());
int index = ArrayUtil.indexOf(arguments, element) + (hasNamed ? 1 : 0);
GroovyResolveResult[] variants = call.getCallVariants((GrReferenceExpression) element);
PsiType result = null;
for (GroovyResolveResult variant : variants) {
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(variant);
if (signature == null)
continue;
if (GrClosureSignatureUtil.mapParametersToArguments(signature, call) != null && !haveNullParameters(call)) {
return null;
}
GrClosureParameter[] parameters = signature.getParameters();
if (index >= parameters.length)
continue;
result = TypesUtil.getLeastUpperBoundNullable(result, parameters[index].getType(), element.getManager());
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method getOptionalParamCount.
public static int getOptionalParamCount(GrClosureParameter[] parameters, boolean hasNamedArgs) {
int count = 0;
int i = 0;
if (hasNamedArgs)
i++;
for (; i < parameters.length; i++) {
GrClosureParameter parameter = parameters[i];
if (parameter.isOptional())
count++;
}
return count;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method generateSimpleSignatures.
public static List<GrClosureSignature> generateSimpleSignatures(@NotNull GrSignature signature) {
final List<GrClosureSignature> result = new ArrayList<>();
signature.accept(new GrRecursiveSignatureVisitor() {
@Override
public void visitClosureSignature(GrClosureSignature signature) {
final GrClosureParameter[] original = signature.getParameters();
final ArrayList<GrClosureParameter> parameters = new ArrayList<>(original.length);
for (GrClosureParameter parameter : original) {
parameters.add(new GrDelegatingClosureParameter(parameter) {
@Override
public boolean isOptional() {
return false;
}
@Nullable
@Override
public GrExpression getDefaultInitializer() {
return null;
}
});
}
final int pcount = signature.isVarargs() ? signature.getParameterCount() - 2 : signature.getParameterCount() - 1;
for (int i = pcount; i >= 0; i--) {
if (original[i].isOptional()) {
result.add(new GrImmediateClosureSignatureImpl(parameters.toArray(new GrClosureParameter[parameters.size()]), signature.getReturnType(), signature.isVarargs(), false));
parameters.remove(i);
}
}
result.add(new GrImmediateClosureSignatureImpl(parameters.toArray(new GrClosureParameter[parameters.size()]), signature.getReturnType(), signature.isVarargs(), false));
}
});
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method mapParametersToArguments.
@Nullable
public static ArgInfo<PsiElement>[] mapParametersToArguments(@NotNull GrClosureSignature signature, @NotNull GrNamedArgument[] namedArgs, @NotNull GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments, @NotNull PsiElement context, boolean partial, boolean eraseArgs) {
List<InnerArg> innerArgs = new ArrayList<>();
boolean hasNamedArgs = namedArgs.length > 0;
GrClosureParameter[] params = signature.getParameters();
if (hasNamedArgs) {
if (params.length == 0)
return null;
PsiType type = params[0].getType();
if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP) || type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
innerArgs.add(new InnerArg(GrMapType.create(context.getResolveScope()), namedArgs));
} else {
return null;
}
}
for (GrExpression expression : expressionArgs) {
PsiType type = expression.getType();
if (partial && expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
type = null;
}
if (eraseArgs) {
type = TypeConversionUtil.erasure(type);
}
innerArgs.add(new InnerArg(type, expression));
}
for (GrClosableBlock closureArgument : closureArguments) {
innerArgs.add(new InnerArg(TypeConversionUtil.erasure(closureArgument.getType()), closureArgument));
}
return mapParametersToArguments(signature, innerArgs, hasNamedArgs, partial, context);
}
Aggregations