Search in sources :

Example 1 with GrRangeType

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;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GrRangeType

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;
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)

Example 3 with GrRangeType

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);
}
Also used : GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) PsiType(com.intellij.psi.PsiType)

Aggregations

GrRangeType (org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Project (com.intellij.openapi.project.Project)1 PsiType (com.intellij.psi.PsiType)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ArrayList (java.util.ArrayList)1 Contract (org.jetbrains.annotations.Contract)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrRangeExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1 GrTupleType (org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType)1