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;
}
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");
}
Aggregations