diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/FixAllAnnotatorQuickfixTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/FixAllAnnotatorQuickfixTest.java index 3c380c41500c..abeeec2b083a 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/FixAllAnnotatorQuickfixTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/FixAllAnnotatorQuickfixTest.java @@ -68,7 +68,7 @@ public class FixAllAnnotatorQuickfixTest extends LightQuickFixTestCase { public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { if (element instanceof PsiMethod) { Annotation annotation = holder.createErrorAnnotation(((PsiMethod)element).getNameIdentifier(), null); - annotation.registerFix(new MyFix()); + annotation.registerUniversalFix(new MyFix(), null, null); annotation.setTextAttributes(CodeInsightColors.DOC_COMMENT_TAG_VALUE); } } diff --git a/platform/lang-api/src/com/intellij/lang/annotation/Annotation.java b/platform/lang-api/src/com/intellij/lang/annotation/Annotation.java index c6c7097609d3..e6a35d34d465 100644 --- a/platform/lang-api/src/com/intellij/lang/annotation/Annotation.java +++ b/platform/lang-api/src/com/intellij/lang/annotation/Annotation.java @@ -17,6 +17,7 @@ package com.intellij.lang.annotation; import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.LocalQuickFix; import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.openapi.editor.HighlighterColors; import com.intellij.openapi.editor.colors.CodeInsightColors; @@ -57,6 +58,7 @@ public final class Annotation implements Segment { private GutterIconRenderer myGutterIconRenderer; @Nullable private String myProblemGroup; + private List myBatchFixes; public static class QuickFixInfo { public final IntentionAction quickFix; @@ -155,6 +157,28 @@ public final class Annotation implements Segment { myQuickFixes.add(new QuickFixInfo(fix, range, key)); } + /** + * Registers a quickfix which would be available during batch mode only, + * in particular during com.intellij.codeInspection.DefaultHighlightVisitorBasedInspection run + */ + public void registerBatchFix(final T fix, @Nullable TextRange range, @Nullable final HighlightDisplayKey key) { + if (range == null) { + range = new TextRange(myStartOffset, myEndOffset); + } + + if (myBatchFixes == null) { + myBatchFixes = new ArrayList(); + } + myBatchFixes.add(new QuickFixInfo(fix, range, key)); + } + + /** + * Register a quickfix which would be available onTheFly and in the batch mode. Should implement both IntentionAction and LocalQuickFix. + */ + public void registerUniversalFix(final T fix, @Nullable TextRange range, @Nullable final HighlightDisplayKey key) { + registerBatchFix(fix, range, key); + registerFix(fix, range, key); + } /** * Sets a flag indicating what happens with the annotation when the user starts typing. * If the parameter is true, the annotation is removed as soon as the user starts typing @@ -277,6 +301,11 @@ public final class Annotation implements Segment { public List getQuickFixes() { return myQuickFixes; } + + @Nullable + public List getBatchFixes() { + return myBatchFixes; + } /** * Returns the description of the annotation (shown in the status bar or by "View | Error Description" action). diff --git a/platform/lang-api/src/com/intellij/lang/annotation/AnnotationHolder.java b/platform/lang-api/src/com/intellij/lang/annotation/AnnotationHolder.java index 0ff69d147af7..f3ede20a8850 100644 --- a/platform/lang-api/src/com/intellij/lang/annotation/AnnotationHolder.java +++ b/platform/lang-api/src/com/intellij/lang/annotation/AnnotationHolder.java @@ -179,4 +179,6 @@ public interface AnnotationHolder { AnnotationSession getCurrentAnnotationSession(); + + boolean isBatchMode(); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/AnnotationHolderImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/AnnotationHolderImpl.java index 972109e5907d..1c9b659f84a0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/AnnotationHolderImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/AnnotationHolderImpl.java @@ -40,8 +40,20 @@ public class AnnotationHolderImpl extends SmartList implements Annot private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl"); private final AnnotationSession myAnnotationSession; + private final boolean myBatchMode; + public AnnotationHolderImpl(@NotNull AnnotationSession session) { + this(session, false); + } + + public AnnotationHolderImpl(@NotNull AnnotationSession session, boolean batchMode) { myAnnotationSession = session; + myBatchMode = batchMode; + } + + @Override + public boolean isBatchMode() { + return myBatchMode; } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DefaultHighlightVisitor.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DefaultHighlightVisitor.java index aa2f2c83ebc6..d2cfee3b6ca0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DefaultHighlightVisitor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DefaultHighlightVisitor.java @@ -56,17 +56,19 @@ public class DefaultHighlightVisitor implements HighlightVisitor, DumbAware { private final boolean myRunAnnotators; private final DumbService myDumbService; private HighlightInfoHolder myHolder; + private boolean myBatchMode; @SuppressWarnings("UnusedDeclaration") public DefaultHighlightVisitor(@NotNull Project project) { - this(project, true, true); + this(project, true, true, false); } - public DefaultHighlightVisitor(@NotNull Project project, boolean highlightErrorElements, boolean runAnnotators) { + public DefaultHighlightVisitor(@NotNull Project project, boolean highlightErrorElements, boolean runAnnotators, boolean batchMode) { myProject = project; myHighlightErrorElements = highlightErrorElements; myRunAnnotators = runAnnotators; myErrorFilters = Extensions.getExtensions(FILTER_EP_NAME, project); myDumbService = DumbService.getInstance(project); + myBatchMode = batchMode; } @Override @@ -80,7 +82,7 @@ public class DefaultHighlightVisitor implements HighlightVisitor, DumbAware { @NotNull final HighlightInfoHolder holder, @NotNull final Runnable action) { myHolder = holder; - myAnnotationHolder = new AnnotationHolderImpl(holder.getAnnotationSession()); + myAnnotationHolder = new AnnotationHolderImpl(holder.getAnnotationSession(), myBatchMode); try { action.run(); } @@ -102,7 +104,7 @@ public class DefaultHighlightVisitor implements HighlightVisitor, DumbAware { } if (myAnnotationHolder.hasAnnotations()) { for (Annotation annotation : myAnnotationHolder) { - myHolder.add(HighlightInfo.fromAnnotation(annotation)); + myHolder.add(HighlightInfo.fromAnnotation(annotation, null, myBatchMode)); } myAnnotationHolder.clear(); } @@ -111,7 +113,7 @@ public class DefaultHighlightVisitor implements HighlightVisitor, DumbAware { @Override @NotNull public HighlightVisitor clone() { - return new DefaultHighlightVisitor(myProject, myHighlightErrorElements, myRunAnnotators); + return new DefaultHighlightVisitor(myProject, myHighlightErrorElements, myRunAnnotators, myBatchMode); } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java index 289c06c46bbf..63b33b02f2be 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java @@ -395,10 +395,10 @@ public class HighlightInfo implements Segment { public static HighlightInfo fromAnnotation(@NotNull Annotation annotation) { - return fromAnnotation(annotation, null); + return fromAnnotation(annotation, null, false); } - public static HighlightInfo fromAnnotation(@NotNull Annotation annotation, @Nullable TextRange fixedRange) { + public static HighlightInfo fromAnnotation(@NotNull Annotation annotation, @Nullable TextRange fixedRange, boolean batchMode) { final TextAttributes forcedAttributes = annotation.getEnforcedTextAttributes(); final TextAttributesKey forcedAttributesKey = forcedAttributes == null ? annotation.getTextAttributes() : null; @@ -409,14 +409,21 @@ public class HighlightInfo implements Segment { annotation.getSeverity(), annotation.isAfterEndOfLine(), annotation.needsUpdateOnTyping(), annotation.isFileLevelAnnotation()); info.setGutterIconRenderer(annotation.getGutterIconRenderer()); info.setProblemGroup(annotation.getProblemGroup()); - List fixes = annotation.getQuickFixes(); + if (batchMode) { + appendFixes(fixedRange, info, annotation.getBatchFixes()); + } else { + appendFixes(fixedRange, info, annotation.getQuickFixes()); + } + return info; + } + + private static void appendFixes(TextRange fixedRange, HighlightInfo info, List fixes) { if (fixes != null) { for (final Annotation.QuickFixInfo quickFixInfo : fixes) { QuickFixAction.registerQuickFixAction(info, fixedRange != null ? fixedRange : quickFixInfo.textRange, quickFixInfo.quickFix, quickFixInfo.key != null ? quickFixInfo.key : HighlightDisplayKey.find(DefaultHighlightVisitorBasedInspection.AnnotatorBasedInspection.ANNOTATOR_SHORT_NAME)); } } - return info; } public static HighlightInfoType convertType(Annotation annotation) { diff --git a/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java b/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java index 733d5385feb3..80b14be94aa8 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java @@ -167,7 +167,7 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl @NotNull @Override protected HighlightVisitor[] createHighlightVisitors() { - return new HighlightVisitor[]{new DefaultHighlightVisitor(project, highlightErrorElements, runAnnotators)}; + return new HighlightVisitor[]{new DefaultHighlightVisitor(project, highlightErrorElements, runAnnotators, true)}; } @Override