Search in sources :

Example 1 with GroovyFix

use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.

the class ChangeToMethodInspection method getFix.

@Nullable
protected GroovyFix getFix(@NotNull Transformation<?> transformation) {
    return new GroovyFix() {

        @Nls
        @NotNull
        @Override
        public String getFamilyName() {
            return message("replace.with.method.fix", transformation.getMethod());
        }

        @Override
        protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
            PsiElement call = descriptor.getPsiElement().getParent();
            if (!(call instanceof GrExpression))
                return;
            if (!transformation.couldApplyRow((GrExpression) call))
                return;
            transformation.applyRow((GrExpression) call);
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GroovyFix

use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.

the class GrAccessibilityChecker method buildFixes.

static GroovyFix[] buildFixes(PsiElement location, GroovyResolveResult resolveResult) {
    final PsiElement element = resolveResult.getElement();
    if (!(element instanceof PsiMember))
        return GroovyFix.EMPTY_ARRAY;
    final PsiMember refElement = (PsiMember) element;
    if (refElement instanceof PsiCompiledElement)
        return GroovyFix.EMPTY_ARRAY;
    PsiModifierList modifierList = refElement.getModifierList();
    if (modifierList == null)
        return GroovyFix.EMPTY_ARRAY;
    List<GroovyFix> fixes = new ArrayList<>();
    try {
        Project project = refElement.getProject();
        JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
        PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
        assert modifierListCopy != null;
        modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
        String minModifier = PsiModifier.PROTECTED;
        if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
            minModifier = PsiModifier.PUBLIC;
        }
        String[] modifiers = { PsiModifier.PROTECTED, PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL };
        PsiClass accessObjectClass = PsiTreeUtil.getParentOfType(location, PsiClass.class, false);
        if (accessObjectClass == null) {
            final PsiFile file = location.getContainingFile();
            if (!(file instanceof GroovyFile))
                return GroovyFix.EMPTY_ARRAY;
            accessObjectClass = ((GroovyFile) file).getScriptClass();
        }
        for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
            String modifier = modifiers[i];
            modifierListCopy.setModifierProperty(modifier, true);
            if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, location, accessObjectClass, null)) {
                fixes.add(new GrModifierFix(refElement, modifier, true, true, GrModifierFix.MODIFIER_LIST_OWNER));
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return fixes.toArray(new GroovyFix[fixes.size()]);
}
Also used : ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 3 with GroovyFix

use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.

the class GrAccessibilityChecker method registerFixes.

private void registerFixes(GrReferenceElement ref, GroovyResolveResult result, HighlightInfo info) {
    PsiElement element = result.getElement();
    assert element != null;
    ProblemDescriptor descriptor = InspectionManager.getInstance(ref.getProject()).createProblemDescriptor(element, element, "", HighlightInfo.convertSeverityToProblemHighlight(info.getSeverity()), true, LocalQuickFix.EMPTY_ARRAY);
    for (GroovyFix fix : buildFixes(ref, result)) {
        QuickFixAction.registerQuickFixAction(info, new LocalQuickFixAsIntentionAdapter(fix, descriptor), myDisplayKey);
    }
}
Also used : GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFixAsIntentionAdapter(com.intellij.codeInspection.LocalQuickFixAsIntentionAdapter)

Example 4 with GroovyFix

use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.

the class ChangeToOperatorInspection method getFix.

@Nullable
protected GroovyFix getFix(@NotNull Transformation transformation, @NotNull String methodName) {
    return new GroovyFix() {

        @Nls
        @NotNull
        @Override
        public String getFamilyName() {
            return message("replace.with.operator.fix", methodName);
        }

        @Override
        protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
            PsiElement call = descriptor.getPsiElement().getParent();
            if (call == null)
                return;
            call = call.getParent();
            if (!(call instanceof GrMethodCall))
                return;
            GrMethodCall methodCall = (GrMethodCall) call;
            GrExpression invokedExpression = methodCall.getInvokedExpression();
            if (!(invokedExpression instanceof GrReferenceExpression))
                return;
            Options options = getOptions();
            if (!transformation.couldApply(methodCall, options))
                return;
            transformation.apply(methodCall, options);
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GroovyFix

use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix 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

GroovyFix (org.jetbrains.plugins.groovy.codeInspection.GroovyFix)5 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)4 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalQuickFixAsIntentionAdapter (com.intellij.codeInspection.LocalQuickFixAsIntentionAdapter)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrRangeExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)1