void (long priorityRange, @NotNull TextRange restrictRange, @NotNull InspectionContext context, @NotNull Consumer<? super InspectionContext> afterProcessCallback) { List<? extends PsiElement> elements = context.elements(); Map<Class<?>, Collection<Class<?>>> targetPsiClasses = InspectionVisitorsOptimizer.getTargetPsiClasses(elements); InspectionProblemHolder holder = context.holder; int resultCount = holder.getResultCount(); PsiElement favoriteElement = holder.myFavoriteElement; // accept favoriteElement only if it belongs to the correct inside/outside list if (favoriteElement != null && context.isVisible() == TextRangeScalarUtil.intersects(favoriteElement.getTextRange(), priorityRange) && restrictRange.contains(favoriteElement.getTextRange())) { holder.myFavoriteElement = null; // null the element to make sure it will hold the new favorite after this method finished // run first for the element we know resulted in the diagnostics during the previous run favoriteElement.accept(context.visitor); if (holder.getResultCount() != resultCount && resultCount != -1) { holder.myFavoriteElement = favoriteElement; resultCount = -1; // mark as "new favorite element is stored" } } if (context.acceptingPsiTypes == InspectionVisitorsOptimizer.ALL_ELEMENTS_VISIT_LIST) { for (int i = 0; i < elements.size(); i++) { PsiElement element = elements.get(i); if (element == favoriteElement) continue; // already visited ProgressManager.checkCanceled(); element.accept(context.visitor); if (resultCount != -1 && holder.getResultCount() != resultCount) { holder.myFavoriteElement = element; resultCount = -1; // mark as "new favorite element is stored" } } } else { Set<Class<?>> accepts = InspectionVisitorsOptimizer.getVisitorAcceptClasses(targetPsiClasses, context.acceptingPsiTypes); if (accepts != null && !accepts.isEmpty()) { for (int i = 0; i < elements.size(); i++) { PsiElement element = elements.get(i); if (element == favoriteElement) continue; // already visited if (accepts.contains(element.getClass())) { ProgressManager.checkCanceled(); element.accept(context.visitor); if (resultCount != -1 && holder.getResultCount() != resultCount) { holder.myFavoriteElement = element; resultCount = -1; // mark as "new favorite element is stored" } } } } } afterProcessCallback.accept(context); }