diff --git a/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java b/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java index 9fb5aa36ccc6..9337a22bd768 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/DefaultHighlightVisitorBasedInspection.java @@ -29,6 +29,7 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiFile; +import com.intellij.util.PairConsumer; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,12 +87,41 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl @Override public void checkFile(@NotNull PsiFile file, - @NotNull InspectionManager manager, + @NotNull final InspectionManager manager, @NotNull ProblemsHolder problemsHolder, - @NotNull GlobalInspectionContext globalContext, - @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) { - PsiElementVisitor visitor = new MyPsiElementVisitor(manager, globalContext, problemDescriptionsProcessor, highlightErrorElements,runAnnotators); - file.accept(visitor); + @NotNull final GlobalInspectionContext globalContext, + @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) { + runGeneralHighlighting(file, highlightErrorElements, runAnnotators, new PairConsumer() { + @Override + public void consume(PsiFile file, HighlightInfo info) { + TextRange range = new TextRange(info.startOffset, info.endOffset); + PsiElement element = file.findElementAt(info.startOffset); + + while (element != null && !element.getTextRange().contains(range)) { + element = element.getParent(); + } + + if (element == null) { + element = file; + } + GlobalInspectionUtil.createProblem( + element, + info.description, + HighlightInfo.convertType(info.type), + range.shiftRight(-element.getNode().getStartOffset()), + manager, + problemDescriptionsProcessor, + globalContext + ); + } + }); + } + + public static void runGeneralHighlighting(PsiFile file, + final boolean highlightErrorElements, + final boolean runAnnotators, + PairConsumer consumer) { + file.accept(new MyPsiElementVisitor(highlightErrorElements, runAnnotators, consumer)); } @Nls @@ -102,22 +132,16 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl } private static class MyPsiElementVisitor extends PsiElementVisitor { - private final InspectionManager myManager; - private final GlobalInspectionContext myGlobalContext; - private final ProblemDescriptionsProcessor myProblemDescriptionsProcessor; private final boolean highlightErrorElements; private final boolean runAnnotators; + private final PairConsumer myConsumer; - public MyPsiElementVisitor(final InspectionManager manager, - final GlobalInspectionContext globalContext, - final ProblemDescriptionsProcessor problemDescriptionsProcessor, - boolean highlightErrorElements, - boolean runAnnotators) { - myManager = manager; - myGlobalContext = globalContext; - myProblemDescriptionsProcessor = problemDescriptionsProcessor; + public MyPsiElementVisitor(boolean highlightErrorElements, + boolean runAnnotators, final PairConsumer consumer) { this.highlightErrorElements = highlightErrorElements; this.runAnnotators = runAnnotators; + myConsumer = consumer; + } @Override @@ -147,27 +171,9 @@ public abstract class DefaultHighlightVisitorBasedInspection extends GlobalSimpl if (info == null) return true; if (info.type == HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT) return true; if (info.severity == HighlightSeverity.INFORMATION) return true; - ProblemHighlightType problemHighlightType = HighlightInfo.convertType(info.type); - TextRange range = new TextRange(info.startOffset, info.endOffset); - PsiElement element = file.findElementAt(info.startOffset); - while (element != null && !element.getTextRange().contains(range)) { - element = element.getParent(); - } + myConsumer.consume(file, info); - if (element == null) { - element = file; - } - - GlobalInspectionUtil.createProblem( - element, - info.description, - problemHighlightType, - range.shiftRight(-element.getNode().getStartOffset()), - myManager, - myProblemDescriptionsProcessor, - myGlobalContext - ); return true; } }; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java b/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java index 6ee9c3ecaa68..988e3cd4b874 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java @@ -63,17 +63,8 @@ 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 InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManagerEx.getInstance(project); - final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false); - final LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(myTool); - tool.initialize(context); - ((RefManagerImpl)context.getRefManager()).inspectionReadActionStarted(); - ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(new Runnable() { - public void run() { - tool.processFile(file, true, managerEx, true); - } - }, new EmptyProgressIndicator()); - final List descriptions = new ArrayList(tool.getProblemDescriptors()); + final List descriptions = runInspectionOnFile(file, myTool); + Collections.sort(descriptions, new Comparator() { public int compare(final CommonProblemDescriptor o1, final CommonProblemDescriptor o2) { final ProblemDescriptorImpl d1 = (ProblemDescriptorImpl)o1; @@ -95,8 +86,27 @@ public class CleanupInspectionIntention implements IntentionAction, HighPriority } } } - ((RefManagerImpl)context.getRefManager()).inspectionReadActionFinished(); - context.cleanup(managerEx); + } + + public static List runInspectionOnFile(final PsiFile file, + final LocalInspectionTool inspectionTool) { + 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 { + ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(new Runnable() { + public void run() { + tool.processFile(file, true, managerEx, true); + } + }, new EmptyProgressIndicator()); + return new ArrayList(tool.getProblemDescriptors()); + } + finally { + ((RefManagerImpl)context.getRefManager()).inspectionReadActionFinished(); + context.cleanup(managerEx); + } } public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {