Search in sources :

Example 1 with GrRangeExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression 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 2 with GrRangeExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression in project intellij-community by JetBrains.

the class GroovyRangeTypeCheckTest method doTest.

public void doTest() {
    myFixture.configureByFile(getTestName(false) + ".groovy");
    final int offset = myFixture.getEditor().getCaretModel().getOffset();
    final PsiElement atCaret = myFixture.getFile().findElementAt(offset);
    final GrRangeExpression range = PsiTreeUtil.getParentOfType(atCaret, GrRangeExpression.class);
    final GroovyRangeTypeCheckInspection inspection = new GroovyRangeTypeCheckInspection();
    final GroovyFix fix = inspection.buildFix(range);
    LocalQuickFix[] fixes = { fix };
    final ProblemDescriptor descriptor = InspectionManager.getInstance(getProject()).createProblemDescriptor(range, "bla-bla", false, fixes, ProblemHighlightType.WEAK_WARNING);
    WriteCommandAction.runWriteCommandAction(null, () -> {
        fix.applyFix(myFixture.getProject(), descriptor);
        PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
    });
    myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
Also used : GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 GrRangeExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyFix (org.jetbrains.plugins.groovy.codeInspection.GroovyFix)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1 GrRangeType (org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType)1