Search in sources :

Example 6 with ExternalAnnotationsManager

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

the class DeannotateIntentionAction method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    final PsiModifierListOwner listOwner = getContainer(editor, file);
    LOG.assertTrue(listOwner != null);
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
    final PsiAnnotation[] externalAnnotations = annotationsManager.findExternalAnnotations(listOwner);
    LOG.assertTrue(externalAnnotations != null && externalAnnotations.length > 0);
    if (externalAnnotations.length == 1) {
        deannotate(externalAnnotations[0], project, file, annotationsManager, listOwner);
        return;
    }
    JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<PsiAnnotation>(CodeInsightBundle.message("deannotate.intention.chooser.title"), externalAnnotations) {

        @Override
        public PopupStep onChosen(final PsiAnnotation selectedValue, final boolean finalChoice) {
            deannotate(selectedValue, project, file, annotationsManager, listOwner);
            return PopupStep.FINAL_CHOICE;
        }

        @Override
        @NotNull
        public String getTextFor(final PsiAnnotation value) {
            final String qualifiedName = value.getQualifiedName();
            LOG.assertTrue(qualifiedName != null);
            return qualifiedName;
        }
    }).showInBestPositionFor(editor);
}
Also used : BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 7 with ExternalAnnotationsManager

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

the class AnnotateOverriddenMethodsIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element) {
    final PsiAnnotation annotation = (PsiAnnotation) element;
    final String annotationName = annotation.getQualifiedName();
    if (annotationName == null) {
        return;
    }
    final Project project = element.getProject();
    final NullableNotNullManager notNullManager = NullableNotNullManager.getInstance(project);
    final List<String> notNulls = notNullManager.getNotNulls();
    final List<String> nullables = notNullManager.getNullables();
    final List<String> annotationsToRemove;
    if (notNulls.contains(annotationName)) {
        annotationsToRemove = nullables;
    } else if (nullables.contains(annotationName)) {
        annotationsToRemove = notNulls;
    } else {
        annotationsToRemove = Collections.emptyList();
    }
    final PsiElement parent = annotation.getParent();
    final PsiElement grandParent = parent.getParent();
    final PsiMethod method;
    final int parameterIndex;
    if (!(grandParent instanceof PsiMethod)) {
        if (!(grandParent instanceof PsiParameter)) {
            return;
        }
        final PsiParameter parameter = (PsiParameter) grandParent;
        final PsiElement greatGrandParent = grandParent.getParent();
        if (!(greatGrandParent instanceof PsiParameterList)) {
            return;
        }
        final PsiParameterList parameterList = (PsiParameterList) greatGrandParent;
        parameterIndex = parameterList.getParameterIndex(parameter);
        final PsiElement greatGreatGrandParent = greatGrandParent.getParent();
        if (!(greatGreatGrandParent instanceof PsiMethod)) {
            return;
        }
        method = (PsiMethod) greatGreatGrandParent;
    } else {
        parameterIndex = -1;
        method = (PsiMethod) grandParent;
    }
    final Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(method).findAll();
    final List<PsiMethod> prepare = new ArrayList<>();
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
    for (PsiMethod overridingMethod : overridingMethods) {
        if (annotationsManager.chooseAnnotationsPlace(overridingMethod) == ExternalAnnotationsManager.AnnotationPlace.IN_CODE) {
            prepare.add(overridingMethod);
        }
    }
    if (!FileModificationService.getInstance().preparePsiElementsForWrite(prepare)) {
        return;
    }
    final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    try {
        for (PsiMethod overridingMethod : overridingMethods) {
            if (parameterIndex == -1) {
                annotate(overridingMethod, annotationName, attributes, annotationsToRemove, annotationsManager);
            } else {
                final PsiParameterList parameterList = overridingMethod.getParameterList();
                final PsiParameter[] parameters = parameterList.getParameters();
                final PsiParameter parameter = parameters[parameterIndex];
                annotate(parameter, annotationName, attributes, annotationsToRemove, annotationsManager);
            }
        }
    } catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
    //escape on configuring root cancel further annotations
    }
    if (!prepare.isEmpty()) {
        UndoUtil.markPsiFileForUndo(annotation.getContainingFile());
    }
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Aggregations

ExternalAnnotationsManager (com.intellij.codeInsight.ExternalAnnotationsManager)7 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NonNull (com.android.annotations.NonNull)1 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 ArrayList (java.util.ArrayList)1 MagicConstant (org.intellij.lang.annotations.MagicConstant)1 NotNull (org.jetbrains.annotations.NotNull)1