mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cleanup [java-intentions]: AnnotateOverriddenMethodsIntention: use ModCommands partially
GitOrigin-RevId: 95174defaac20fd1711a77f15fbdfdaf0094232e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1138f6a2d2
commit
f293324310
+25
-35
@@ -5,7 +5,9 @@ import com.intellij.codeInsight.*;
|
||||
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
|
||||
import com.intellij.codeInspection.util.IntentionName;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.modcommand.ActionContext;
|
||||
import com.intellij.modcommand.ModCommand;
|
||||
import com.intellij.modcommand.ModCommandExecutor;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
@@ -126,7 +128,7 @@ public final class AnnotateOverriddenMethodsIntention extends BaseElementAtCaret
|
||||
}
|
||||
final Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(method).findAll();
|
||||
final List<PsiMethod> prepare = new ArrayList<>();
|
||||
final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
|
||||
final ModCommandAwareExternalAnnotationsManager annotationsManager = ModCommandAwareExternalAnnotationsManager.getInstance(project);
|
||||
final Map<PsiMethod, ExternalAnnotationsManager.AnnotationPlace> annotationPlaces = new LinkedHashMap<>();
|
||||
for (PsiMethod overridingMethod : overridingMethods) {
|
||||
annotationPlaces.put(overridingMethod, annotationsManager.chooseAnnotationsPlaceNoUi(overridingMethod));
|
||||
@@ -144,21 +146,13 @@ public final class AnnotateOverriddenMethodsIntention extends BaseElementAtCaret
|
||||
return;
|
||||
}
|
||||
final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
|
||||
try {
|
||||
for (PsiMethod overridingMethod : overridingMethods) {
|
||||
if (parameterIndex == -1) {
|
||||
annotate(overridingMethod, annotationName, attributes, annotationsToRemove, annotationPlaces.get(overridingMethod), annotationsManager);
|
||||
}
|
||||
else {
|
||||
final PsiParameterList parameterList = overridingMethod.getParameterList();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
final PsiParameter parameter = parameters[parameterIndex];
|
||||
annotate(parameter, annotationName, attributes, annotationsToRemove, annotationPlaces.get(overridingMethod), annotationsManager);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
|
||||
//escape on configuring root cancel further annotations
|
||||
ActionContext context = ActionContext.from(editor, element.getContainingFile());
|
||||
for (PsiMethod overridingMethod : overridingMethods) {
|
||||
ModCommandExecutor.executeInteractively(context, getFamilyName(), editor, () -> {
|
||||
PsiModifierListOwner target =
|
||||
parameterIndex == -1 ? overridingMethod : overridingMethod.getParameterList().getParameter(parameterIndex);
|
||||
return annotate(target, annotationName, attributes, annotationsToRemove, annotationPlaces.get(overridingMethod), annotationsManager);
|
||||
});
|
||||
}
|
||||
if (!prepare.isEmpty()) {
|
||||
UndoUtil.markPsiFileForUndo(annotation.getContainingFile());
|
||||
@@ -186,35 +180,31 @@ public final class AnnotateOverriddenMethodsIntention extends BaseElementAtCaret
|
||||
}
|
||||
}
|
||||
|
||||
private static void annotate(PsiModifierListOwner modifierListOwner,
|
||||
String annotationName,
|
||||
PsiNameValuePair[] attributes,
|
||||
List<String> annotationsToRemove,
|
||||
ExternalAnnotationsManager.AnnotationPlace annotationAnnotationPlace,
|
||||
ExternalAnnotationsManager annotationsManager) throws ProcessCanceledException {
|
||||
private static @NotNull ModCommand annotate(@Nullable PsiModifierListOwner modifierListOwner,
|
||||
@NotNull String annotationName,
|
||||
@NotNull PsiNameValuePair @NotNull [] attributes,
|
||||
@NotNull List<String> annotationsToRemove,
|
||||
ExternalAnnotationsManager.@NotNull AnnotationPlace annotationAnnotationPlace,
|
||||
@NotNull ModCommandAwareExternalAnnotationsManager annotationsManager) throws ProcessCanceledException {
|
||||
if (modifierListOwner == null) return ModCommand.nop();
|
||||
PsiAnnotationOwner target = AnnotationTargetUtil.getTarget(modifierListOwner, annotationName);
|
||||
if (target == null || target.hasAnnotation(annotationName)) return;
|
||||
if (target == null || target.hasAnnotation(annotationName)) return ModCommand.nop();
|
||||
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.NOWHERE) {
|
||||
return;
|
||||
return ModCommand.nop();
|
||||
}
|
||||
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.EXTERNAL) {
|
||||
for (String annotationToRemove : annotationsToRemove) {
|
||||
annotationsManager.deannotate(modifierListOwner, annotationToRemove);
|
||||
}
|
||||
try {
|
||||
annotationsManager.annotateExternally(modifierListOwner, annotationName, modifierListOwner.getContainingFile(), attributes);
|
||||
}
|
||||
catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {}
|
||||
return annotationsManager.annotateExternallyModCommand(modifierListOwner, annotationName, attributes, annotationsToRemove);
|
||||
}
|
||||
else {
|
||||
WriteAction.run(() -> {
|
||||
return ModCommand.psiUpdate(modifierListOwner, (owner, updater) -> {
|
||||
PsiAnnotationOwner writableTarget = Objects.requireNonNull(AnnotationTargetUtil.getTarget(owner, annotationName));
|
||||
for (String annotationToRemove : annotationsToRemove) {
|
||||
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(modifierListOwner, annotationToRemove);
|
||||
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(owner, annotationToRemove);
|
||||
if (annotation != null) {
|
||||
annotation.delete();
|
||||
}
|
||||
}
|
||||
final PsiAnnotation inserted = target.addAnnotation(annotationName);
|
||||
final PsiAnnotation inserted = writableTarget.addAnnotation(annotationName);
|
||||
for (PsiNameValuePair pair : attributes) {
|
||||
inserted.setDeclaredAttributeValue(pair.getName(), pair.getValue());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user