use of org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType in project intellij-community by JetBrains.
the class ClosureParameterEnhancer method findTypeForIteration.
@Contract("null,_ -> null")
@Nullable
public static PsiType findTypeForIteration(@Nullable PsiType type, @NotNull PsiElement context) {
final PsiManager manager = context.getManager();
final GlobalSearchScope resolveScope = context.getResolveScope();
if (type instanceof PsiArrayType) {
return TypesUtil.boxPrimitiveType(((PsiArrayType) type).getComponentType(), manager, resolveScope);
}
if (type instanceof GrTupleType) {
PsiType[] types = ((GrTupleType) type).getParameters();
return types.length == 1 ? types[0] : null;
}
if (type instanceof GrRangeType) {
return ((GrRangeType) type).getIterationType();
}
PsiType fromIterator = findTypeFromIteratorMethod(type, context);
if (fromIterator != null) {
return fromIterator;
}
PsiType extracted = PsiUtil.extractIterableTypeParameter(type, true);
if (extracted != null) {
return extracted;
}
if (TypesUtil.isClassType(type, JAVA_LANG_STRING) || TypesUtil.isClassType(type, JAVA_IO_FILE)) {
return PsiType.getJavaLangString(manager, resolveScope);
}
if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
return getEntryForMap(type, manager.getProject(), resolveScope);
}
return type;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType in project intellij-community by JetBrains.
the class GroovyRangeTypeCheckInspection method buildFix.
@Override
protected GroovyFix buildFix(@NotNull PsiElement location) {
final GrRangeExpression range = (GrRangeExpression) location;
final PsiType type = range.getType();
final List<GroovyFix> fixes = new ArrayList<>(3);
if (type instanceof GrRangeType) {
PsiType iterationType = ((GrRangeType) type).getIterationType();
if (!(iterationType instanceof PsiClassType))
return null;
final PsiClass psiClass = ((PsiClassType) iterationType).resolve();
if (!(psiClass instanceof GrTypeDefinition))
return null;
final GroovyResolveResult[] nexts = ResolveUtil.getMethodCandidates(iterationType, "next", range);
final GroovyResolveResult[] previouses = ResolveUtil.getMethodCandidates(iterationType, "previous", range);
final GroovyResolveResult[] compareTos = ResolveUtil.getMethodCandidates(iterationType, "compareTo", range, iterationType);
if (countImplementations(psiClass, nexts) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("next", (GrTypeDefinition) psiClass));
}
if (countImplementations(psiClass, previouses) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("previous", (GrTypeDefinition) psiClass));
}
if (!InheritanceUtil.isInheritor(iterationType, CommonClassNames.JAVA_LANG_COMPARABLE) || countImplementations(psiClass, compareTos) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddClassToExtendsFix((GrTypeDefinition) psiClass, CommonClassNames.JAVA_LANG_COMPARABLE));
}
return new GroovyFix() {
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
for (GroovyFix fix : fixes) {
fix.applyFix(project, descriptor);
}
}
@NotNull
@Override
public String getName() {
return GroovyInspectionBundle.message("fix.class", psiClass.getName());
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Fix range class";
}
};
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType in project intellij-community by JetBrains.
the class GrRangeExpressionTypeCalculator method fun.
@Override
public PsiType fun(GrOperatorExpression e) {
final PsiType type = super.fun(e);
if (type != null)
return type;
final PsiType ltype = e.getLeftType();
final PsiType rtype = e.getRightType();
return new GrRangeType(e.getResolveScope(), JavaPsiFacade.getInstance(e.getProject()), ltype, rtype);
}
Aggregations