support for Groovy 2.2's DelegatesTo#genericIndexType

This commit is contained in:
Max Medvedev
2013-05-24 14:23:18 +02:00
parent ef096d0057
commit d0f62f4c2d
3 changed files with 32 additions and 2 deletions
@@ -34,4 +34,14 @@ public class GrAnnotationUtil {
}
return null;
}
@Nullable
public static Integer inferIntegerAttribute(@NotNull PsiAnnotation annotation, @NotNull String attributeName) {
final PsiAnnotationMemberValue targetValue = annotation.findAttributeValue(attributeName);
if (targetValue instanceof PsiLiteral) {
final Object value = ((PsiLiteral)targetValue).getValue();
if (value instanceof Integer) return (Integer)value;
}
return null;
}
}
@@ -56,7 +56,7 @@ public class GrDelegatesToUtil {
if (signature == null) return null;
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] map = GrClosureSignatureUtil.mapParametersToArguments(
signature, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments(), place, false, true
signature, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments(), place, false, false
);
if (map == null) {
@@ -112,7 +112,18 @@ public class GrDelegatesToUtil {
final int parameter = findTargetParameter(delegatesTo, target);
if (parameter >= 0) {
return map[parameter].type;
final PsiType type = map[parameter].type;
final Integer index = GrAnnotationUtil.inferIntegerAttribute(delegatesTo, "genericTypeIndex");
if (index == null) {
return type;
}
if (type instanceof PsiClassType) {
final PsiType[] parameters = ((PsiClassType)type).getParameters();
if (parameters.length > index) {
return parameters[index];
}
}
return null;
}
}
else if (value instanceof PsiExpression) {
@@ -631,6 +631,15 @@ foo(4) {
''', 'Integer')
}
void testGenericTypeIndex() {
assertScript('''\
def foo(@DelegatesTo.Target def map, @DelegatesTo(genericTypeIndex = 1) Closure c) {}
foo([1:'ab', 2:'cde']) {
sub<caret>string(1)
}
''', 'String')
}
void assertScript(String text, String resolvedClass) {
myFixture.configureByText('_a.groovy', text)