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