From cba414b385d176e405d8305dbda07f7348c424d2 Mon Sep 17 00:00:00 2001 From: Artemiy Sartakov Date: Thu, 23 Apr 2020 12:02:48 +0700 Subject: [PATCH] Make externalAnnotation EP dynamic GitOrigin-RevId: 23c3281b2e870ccf0f322be680c6f455712966ae --- java/java-impl/src/META-INF/JavaPlugin.xml | 2 +- .../impl/AnnotateIntentionAction.java | 75 +++++++++---------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml index d8607b39d00c..8a6a944c4eeb 100644 --- a/java/java-impl/src/META-INF/JavaPlugin.xml +++ b/java/java-impl/src/META-INF/JavaPlugin.xml @@ -74,7 +74,7 @@ - + diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/AnnotateIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/AnnotateIntentionAction.java index c2ea26c0d4b0..8377fcc19840 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/AnnotateIntentionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/AnnotateIntentionAction.java @@ -5,7 +5,6 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.ExternalAnnotationsManagerImpl; import com.intellij.codeInsight.externalAnnotation.AnnotationProvider; -import com.intellij.codeInsight.intention.AddAnnotationFix; import com.intellij.codeInsight.intention.AddAnnotationPsiFix; import com.intellij.codeInsight.intention.LowPriorityAction; import com.intellij.java.JavaBundle; @@ -31,8 +30,7 @@ import java.util.Optional; import java.util.stream.Collectors; public class AnnotateIntentionAction extends BaseIntentionAction implements LowPriorityAction { - private AnnotationProvider myAnnotationProvider; - private boolean mySingleMode; + private String mySingleAnnotationName; @Override @NotNull @@ -56,19 +54,25 @@ public class AnnotateIntentionAction extends BaseIntentionAction implements LowP */ @TestOnly public boolean selectSingle(Editor editor, PsiFile file, String annotationShortName) { - if (mySingleMode) { + if (mySingleAnnotationName != null) { throw new IllegalStateException(); } - mySingleMode = true; + mySingleAnnotationName = annotationShortName; final PsiModifierListOwner owner = AddAnnotationPsiFix.getContainer(file, editor.getCaretModel().getOffset()); if (owner == null || owner.getModifierList() == null || !ExternalAnnotationsManagerImpl.areExternalAnnotationsApplicable(owner)) { return false; } - Optional provider = availableAnnotations(owner, file.getProject()) + return canAnnotateWith(file, owner, annotationShortName); + } + + private static boolean canAnnotateWith(PsiFile file, PsiModifierListOwner owner, String annotationShortName) { + return getProviderFor(file, owner, annotationShortName).isPresent(); + } + + private static Optional getProviderFor(PsiFile file, PsiModifierListOwner owner, String annotationShortName) { + return availableAnnotations(owner, file.getProject()) .filter(p -> StringUtil.getShortName(p.getName(file.getProject())).equals(annotationShortName)) .collect(MoreCollectors.onlyOne()); - myAnnotationProvider = provider.orElse(null); - return provider.isPresent(); } private static boolean alreadyAnnotated(PsiModifierListOwner owner, AnnotationProvider p, Project project) { @@ -83,17 +87,14 @@ public class AnnotateIntentionAction extends BaseIntentionAction implements LowP if (owner == null || owner.getModifierList() == null || !ExternalAnnotationsManagerImpl.areExternalAnnotationsApplicable(owner)) { return false; } - if (mySingleMode) { - return myAnnotationProvider != null && availableAnnotations(owner, project).has(myAnnotationProvider); - } List annotations = availableAnnotations(owner, project).limit(2).collect(Collectors.toList()); if (annotations.isEmpty()) return false; + if (mySingleAnnotationName != null && canAnnotateWith(file, owner, mySingleAnnotationName)) return true; if (annotations.size() == 1) { - myAnnotationProvider = annotations.get(0); - setText(AddAnnotationPsiFix.calcText(owner, myAnnotationProvider.getName(project))); + String providerName = annotations.get(0).getName(project); + setText(AddAnnotationPsiFix.calcText(owner, providerName)); } else { - myAnnotationProvider = null; setText(AddAnnotationPsiFix.calcText(owner, null)); } return true; @@ -103,33 +104,31 @@ public class AnnotateIntentionAction extends BaseIntentionAction implements LowP public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException { final PsiModifierListOwner owner = AddAnnotationPsiFix.getContainer(file, editor.getCaretModel().getOffset()); assert owner != null; - if (myAnnotationProvider != null) { - if (alreadyAnnotated(owner, myAnnotationProvider, project)) return; - AddAnnotationFix fix = myAnnotationProvider.createFix(owner); - fix.invoke(project, editor, file); + if (mySingleAnnotationName != null) { + getProviderFor(file, owner, mySingleAnnotationName) + .ifPresent(provider -> provider.createFix(owner).invoke(project, editor, file)); + return; } - else { - List annotations = availableAnnotations(owner, project).collect(Collectors.toList()); - if (annotations.isEmpty()) return; - JBPopupFactory.getInstance().createListPopup( - new BaseListPopupStep(JavaBundle.message("annotate.intention.chooser.title"), annotations) { - @Override - public PopupStep onChosen(final AnnotationProvider selectedValue, final boolean finalChoice) { - return doFinalStep(() -> selectedValue.createFix(owner).invoke(project, editor, file)); - } + List annotations = availableAnnotations(owner, project).collect(Collectors.toList()); + if (annotations.isEmpty()) return; + JBPopupFactory.getInstance().createListPopup( + new BaseListPopupStep(JavaBundle.message("annotate.intention.chooser.title"), annotations) { + @Override + public PopupStep onChosen(final AnnotationProvider selectedValue, final boolean finalChoice) { + return doFinalStep(() -> selectedValue.createFix(owner).invoke(project, editor, file)); + } - @Override - public boolean isSpeedSearchEnabled() { - return true; - } + @Override + public boolean isSpeedSearchEnabled() { + return true; + } - @Override - @NotNull - public String getTextFor(final AnnotationProvider value) { - return value.getName(project); - } - }).showInBestPositionFor(editor); - } + @Override + @NotNull + public String getTextFor(final AnnotationProvider value) { + return value.getName(project); + } + }).showInBestPositionFor(editor); } @Override