Search in sources :

Example 46 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class IPPTestCase method assertIntentionNotAvailable.

protected void assertIntentionNotAvailable(Class<? extends IntentionAction> intentionClass) {
    myFixture.configureByFile(getTestName(false) + ".java");
    final List<IntentionAction> result = new SmartList<>();
    for (final IntentionAction intention : myFixture.getAvailableIntentions()) {
        if (intentionClass.isInstance(intention)) {
            result.add(intention);
        } else if (intention instanceof IntentionActionWrapper) {
            final IntentionActionWrapper wrapper = (IntentionActionWrapper) intention;
            if (intentionClass.isInstance(wrapper.getDelegate())) {
                result.add(intention);
            }
        }
    }
    assertEmpty("Intention of class \'" + intentionClass + "\' is available but should not", result);
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) SmartList(com.intellij.util.SmartList) IntentionActionWrapper(com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper)

Example 47 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class HighlightUtil method registerAccessQuickFixAction.

/**
   * make element protected/package-private/public suggestion
   */
static void registerAccessQuickFixAction(@NotNull PsiMember refElement, @NotNull PsiJavaCodeReferenceElement place, @Nullable HighlightInfo errorResult, final PsiElement fileResolveScope) {
    if (errorResult == null)
        return;
    PsiClass accessObjectClass = null;
    PsiElement qualifier = place.getQualifier();
    if (qualifier instanceof PsiExpression) {
        accessObjectClass = (PsiClass) PsiUtil.getAccessObjectClass((PsiExpression) qualifier).getElement();
    }
    registerReplaceInaccessibleFieldWithGetterSetterFix(refElement, place, accessObjectClass, errorResult);
    if (refElement instanceof PsiCompiledElement)
        return;
    PsiModifierList modifierList = refElement.getModifierList();
    if (modifierList == null)
        return;
    PsiClass packageLocalClassInTheMiddle = getPackageLocalClassInTheMiddle(place);
    if (packageLocalClassInTheMiddle != null) {
        IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(packageLocalClassInTheMiddle, PsiModifier.PUBLIC, true, true);
        QuickFixAction.registerQuickFixAction(errorResult, fix);
        return;
    }
    try {
        Project project = refElement.getProject();
        JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
        PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
        modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
        String minModifier = PsiModifier.PACKAGE_LOCAL;
        if (refElement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
            minModifier = PsiModifier.PROTECTED;
        }
        if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
            minModifier = PsiModifier.PUBLIC;
        }
        String[] modifiers = { PsiModifier.PACKAGE_LOCAL, PsiModifier.PROTECTED, PsiModifier.PUBLIC };
        for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
            @PsiModifier.ModifierConstant String modifier = modifiers[i];
            modifierListCopy.setModifierProperty(modifier, true);
            if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, place, accessObjectClass, fileResolveScope)) {
                IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(refElement, modifier, true, true);
                TextRange fixRange = new TextRange(errorResult.startOffset, errorResult.endOffset);
                PsiElement ref = place.getReferenceNameElement();
                if (ref != null) {
                    fixRange = fixRange.union(ref.getTextRange());
                }
                QuickFixAction.registerQuickFixAction(errorResult, fixRange, fix);
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Project(com.intellij.openapi.project.Project) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 48 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class HighlightUtil method getChangeVariableTypeFixes.

@NotNull
public static List<IntentionAction> getChangeVariableTypeFixes(@NotNull PsiVariable parameter, PsiType itemType) {
    if (itemType instanceof PsiMethodReferenceType)
        return Collections.emptyList();
    List<IntentionAction> result = new ArrayList<>();
    if (itemType != null) {
        for (ChangeVariableTypeQuickFixProvider fixProvider : Extensions.getExtensions(ChangeVariableTypeQuickFixProvider.EP_NAME)) {
            Collections.addAll(result, fixProvider.getFixes(parameter, itemType));
        }
    }
    IntentionAction changeFix = getChangeParameterClassFix(parameter.getType(), itemType);
    if (changeFix != null)
        result.add(changeFix);
    return result;
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) ChangeVariableTypeQuickFixProvider(com.intellij.codeInsight.quickfix.ChangeVariableTypeQuickFixProvider)

Example 49 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class PostHighlightingVisitor method processMethod.

@Nullable
private HighlightInfo processMethod(@NotNull final Project project, @NotNull final PsiMethod method, @NotNull PsiIdentifier identifier, @NotNull ProgressIndicator progress, @NotNull GlobalUsageHelper helper) {
    if (UnusedSymbolUtil.isMethodReferenced(myProject, myFile, method, progress, helper))
        return null;
    String key;
    if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
        key = method.isConstructor() ? "private.constructor.is.not.used" : "private.method.is.not.used";
    } else {
        key = method.isConstructor() ? "constructor.is.not.used" : "method.is.not.used";
    }
    String symbolName = HighlightMessageUtil.getSymbolName(method, PsiSubstitutor.EMPTY);
    String message = JavaErrorMessages.message(key, symbolName);
    final HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, message, myDeadCodeInfoType);
    QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(method), myDeadCodeKey);
    SpecialAnnotationsUtilBase.createAddToSpecialAnnotationFixes(method, annoName -> {
        IntentionAction fix = QuickFixFactory.getInstance().createAddToDependencyInjectionAnnotationsFix(project, annoName, "methods");
        QuickFixAction.registerQuickFixAction(highlightInfo, fix);
        return true;
    });
    return highlightInfo;
}
Also used : EmptyIntentionAction(com.intellij.codeInsight.intention.EmptyIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Nullable(org.jetbrains.annotations.Nullable)

Example 50 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class StaticPseudoFunctionalStyleMethodTest method doTest.

private void doTest() {
    myFixture.configureByFile(getTestName(true) + "/test.java");
    myFixture.enableInspections(new StaticPseudoFunctionalStyleMethodInspection());
    boolean isQuickFixFound = false;
    for (IntentionAction action : myFixture.getAvailableIntentions()) {
        if (action instanceof QuickFixWrapper) {
            final LocalQuickFix fix = ((QuickFixWrapper) action).getFix();
            if (fix instanceof StaticPseudoFunctionalStyleMethodInspection.ReplacePseudoLambdaWithLambda) {
                myFixture.launchAction(action);
                isQuickFixFound = true;
                break;
            }
        }
    }
    assertTrue("Quick fix isn't found", isQuickFixFound);
    myFixture.checkResultByFile(getTestName(true) + "/test_after.java");
}
Also used : StaticPseudoFunctionalStyleMethodInspection(com.intellij.codeInspection.java18StreamApi.StaticPseudoFunctionalStyleMethodInspection) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) QuickFixWrapper(com.intellij.codeInspection.ex.QuickFixWrapper)

Aggregations

IntentionAction (com.intellij.codeInsight.intention.IntentionAction)242 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)31 NotNull (org.jetbrains.annotations.NotNull)23 Project (com.intellij.openapi.project.Project)20 TextRange (com.intellij.openapi.util.TextRange)20 Editor (com.intellij.openapi.editor.Editor)17 PsiFile (com.intellij.psi.PsiFile)17 Annotation (com.intellij.lang.annotation.Annotation)16 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)15 ArrayList (java.util.ArrayList)14 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)10 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)9 Pair (com.intellij.openapi.util.Pair)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 EmptyIntentionAction (com.intellij.codeInsight.intention.EmptyIntentionAction)6 AndroidMissingOnClickHandlerInspection (org.jetbrains.android.inspections.AndroidMissingOnClickHandlerInspection)6 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)5 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)5