enable fix all for file for annotator problems, should work for localQuickFixes only (IDEA-87100)

This commit is contained in:
Anna Kozlova
2012-06-18 12:07:27 +04:00
parent c51ed9a33f
commit 40e35d7a60
7 changed files with 189 additions and 24 deletions
@@ -0,0 +1,7 @@
// "Fix all 'Annotator' problems" "true"
public class Test {
void fooF() {
}
void barF(){}
}
@@ -0,0 +1,7 @@
// "Fix all 'Annotator' problems" "true"
public class Test {
void f<caret>oo() {
}
void bar(){}
}
@@ -0,0 +1,124 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* User: anna
* Date: 17-Jun-2007
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageAnnotators;
import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.CodeInsightColors;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class FixAllAnnotatorQuickfixTest extends LightQuickFixTestCase {
public void testAnnotator() throws Exception {
Annotator annotator = new MyAnnotator();
Language javaLanguage = StdFileTypes.JAVA.getLanguage();
LanguageAnnotators.INSTANCE.addExplicitExtension(javaLanguage, annotator);
enableInspectionTool(new DefaultHighlightVisitorBasedInspection.AnnotatorBasedInspection());
try {
doAllTests();
}
finally {
LanguageAnnotators.INSTANCE.removeExplicitExtension(javaLanguage, annotator);
}
}
@Override
protected boolean shouldBeAvailableAfterExecution() {
return true;
}
@Override
@NonNls
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/fixAllAnnotator";
}
public static class MyAnnotator implements Annotator {
@Override
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.setTextAttributes(CodeInsightColors.DOC_COMMENT_TAG_VALUE);
}
}
static class MyFix implements IntentionAction, LocalQuickFix {
@NotNull
@Override
public String getText() {
return getName();
}
@NotNull
@Override
public String getName() {
return "MyFix";
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (element != null) {
final PsiElement parent = element.getParent();
if (parent instanceof PsiMethod) {
((PsiMethod)parent).setName(((PsiMethod)parent).getName() + "F");
}
}
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
fail();
}
@Override
public boolean startInWriteAction() {
return true;
}
}
}
}
@@ -20,10 +20,7 @@ import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.codeInspection.CustomSuppressableInspectionTool;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.actions.CleanupInspectionIntention;
import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
@@ -405,7 +402,8 @@ public class HighlightInfo implements Segment {
List<Annotation.QuickFixInfo> fixes = annotation.getQuickFixes();
if (fixes != null) {
for (final Annotation.QuickFixInfo quickFixInfo : fixes) {
QuickFixAction.registerQuickFixAction(info, fixedRange != null? fixedRange : quickFixInfo.textRange, quickFixInfo.quickFix, quickFixInfo.key);
QuickFixAction.registerQuickFixAction(info, fixedRange != null ? fixedRange : quickFixInfo.textRange, quickFixInfo.quickFix,
quickFixInfo.key != null ? quickFixInfo.key : HighlightDisplayKey.find(DefaultHighlightVisitorBasedInspection.AnnotatorBasedInspection.ANNOTATOR_SHORT_NAME));
}
}
return info;
@@ -524,6 +522,13 @@ public class HighlightInfo implements Segment {
newOptions.add(new CleanupInspectionIntention((LocalInspectionToolWrapper)tool, aClass));
} else if (tool instanceof GlobalInspectionToolWrapper) {
wrappedTool = ((GlobalInspectionToolWrapper)tool).getTool();
if (wrappedTool instanceof GlobalSimpleInspectionTool && (myAction instanceof LocalQuickFix || myAction instanceof QuickFixWrapper)) {
Class aClass = myAction.getClass();
if (myAction instanceof QuickFixWrapper) {
aClass = ((QuickFixWrapper)myAction).getFix().getClass();
}
newOptions.add(new CleanupInspectionIntention((GlobalInspectionToolWrapper)tool, aClass));
}
}
if (wrappedTool instanceof CustomSuppressableInspectionTool) {
@@ -49,6 +49,9 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl
}
public static class AnnotatorBasedInspection extends DefaultHighlightVisitorBasedInspection {
public static final String ANNOTATOR_SHORT_NAME = "Annotator";
public AnnotatorBasedInspection() {
super(false, true);
}
@@ -62,7 +65,7 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl
@NotNull
@Override
public String getShortName() {
return "Annotator";
return ANNOTATOR_SHORT_NAME;
}
}
@@ -15,13 +15,13 @@
*/
package com.intellij.codeInspection;
import com.intellij.codeInspection.ex.GlobalInspectionContextImpl;
import com.intellij.codeInspection.ex.InspectionManagerEx;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.codeInspection.ex.*;
import com.intellij.codeInspection.reference.RefManagerImpl;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -31,17 +31,33 @@ import java.util.List;
public class InspectionRunningUtil {
public static List<CommonProblemDescriptor> runInspectionOnFile(final PsiFile file,
final LocalInspectionTool inspectionTool) {
return runInspectionOnFile(file, new LocalInspectionToolWrapper(inspectionTool));
}
public static List<CommonProblemDescriptor> runInspectionOnFile(final PsiFile file, final InspectionTool tool) {
final InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManager.getInstance(file.getProject());
final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false);
final LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(inspectionTool);
tool.initialize(context);
((RefManagerImpl)context.getRefManager()).inspectionReadActionStarted();
try {
tool.processFile(file, true, managerEx, true);
return new ArrayList<CommonProblemDescriptor>(tool.getProblemDescriptors());
if (tool instanceof LocalInspectionToolWrapper) {
((LocalInspectionToolWrapper)tool).processFile(file, true, managerEx, true);
return new ArrayList<CommonProblemDescriptor>(((LocalInspectionToolWrapper)tool).getProblemDescriptors());
}
else if (tool instanceof GlobalInspectionToolWrapper) {
final GlobalInspectionTool globalInspectionTool = ((GlobalInspectionToolWrapper)tool).getTool();
if (globalInspectionTool instanceof GlobalSimpleInspectionTool) {
ProblemsHolder problemsHolder = new ProblemsHolder(managerEx, file, false);
((GlobalSimpleInspectionTool)globalInspectionTool)
.checkFile(file, managerEx, problemsHolder, context, (GlobalInspectionToolWrapper)tool);
return new ArrayList<CommonProblemDescriptor>(((GlobalInspectionToolWrapper)tool).getProblemDescriptors());
}
}
return Collections.emptyList();
}
finally {
((RefManagerImpl)context.getRefManager()).inspectionReadActionFinished();
tool.cleanup();
context.cleanup(managerEx);
}
}
@@ -21,9 +21,7 @@ import com.intellij.codeInsight.intention.EmptyIntentionAction;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.codeInspection.ex.ProblemDescriptorImpl;
import com.intellij.codeInspection.ex.UnfairLocalInspectionTool;
import com.intellij.codeInspection.ex.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
@@ -44,10 +42,10 @@ import java.util.List;
* Date: 21-Feb-2006
*/
public class CleanupInspectionIntention implements IntentionAction, HighPriorityAction {
private final LocalInspectionToolWrapper myTool;
private final InspectionToolWrapper myTool;
private final Class myQuickfixClass;
public CleanupInspectionIntention(final LocalInspectionToolWrapper tool, Class quickFixClass) {
public CleanupInspectionIntention(final InspectionToolWrapper tool, Class quickFixClass) {
myTool = tool;
myQuickfixClass = quickFixClass;
}
@@ -64,12 +62,13 @@ public class CleanupInspectionIntention implements IntentionAction, HighPriority
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!CodeInsightUtilBase.preparePsiElementForWrite(file)) return;
final List<CommonProblemDescriptor> descriptions = ProgressManager.getInstance().runProcess(new Computable<List<CommonProblemDescriptor>>() {
@Override
public List<CommonProblemDescriptor> compute() {
return InspectionRunningUtil.runInspectionOnFile(file, myTool.getTool());
}
}, new EmptyProgressIndicator());
final List<CommonProblemDescriptor> descriptions =
ProgressManager.getInstance().runProcess(new Computable<List<CommonProblemDescriptor>>() {
@Override
public List<CommonProblemDescriptor> compute() {
return InspectionRunningUtil.runInspectionOnFile(file, myTool);
}
}, new EmptyProgressIndicator());
Collections.sort(descriptions, new Comparator<CommonProblemDescriptor>() {
public int compare(final CommonProblemDescriptor o1, final CommonProblemDescriptor o2) {
@@ -95,8 +94,12 @@ public class CleanupInspectionIntention implements IntentionAction, HighPriority
}
}
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
return myQuickfixClass != null && myQuickfixClass != EmptyIntentionAction.class && !(myTool.isUnfair());
return myQuickfixClass != null && myQuickfixClass != EmptyIntentionAction.class && !(myTool instanceof LocalInspectionToolWrapper &&
((LocalInspectionToolWrapper)myTool).isUnfair());
}
public boolean startInWriteAction() {