Search in sources :

Example 6 with RefManagerImpl

use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.

the class GlobalInspectionContextImpl method cleanup.

private void cleanup(@NotNull final AnalysisScope scope, @NotNull InspectionProfile profile, @Nullable final Runnable postRunnable, @Nullable final String commandName) {
    setCurrentScope(scope);
    final int fileCount = scope.getFileCount();
    final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
    final SearchScope searchScope = ReadAction.compute(scope::toSearchScope);
    final TextRange range;
    if (searchScope instanceof LocalSearchScope) {
        final PsiElement[] elements = ((LocalSearchScope) searchScope).getScope();
        range = elements.length == 1 ? ReadAction.compute(elements[0]::getTextRange) : null;
    } else {
        range = null;
    }
    final Iterable<Tools> inspectionTools = ContainerUtil.filter(profile.getAllEnabledInspectionTools(getProject()), tools -> {
        assert tools != null;
        return tools.getTool().getTool() instanceof CleanupLocalInspectionTool;
    });
    boolean includeDoNotShow = includeDoNotShow(profile);
    final RefManagerImpl refManager = (RefManagerImpl) getRefManager();
    refManager.inspectionReadActionStarted();
    List<ProblemDescriptor> descriptors = new ArrayList<>();
    Set<PsiFile> files = new HashSet<>();
    try {
        scope.accept(new PsiElementVisitor() {

            private int myCount;

            @Override
            public void visitFile(PsiFile file) {
                if (progressIndicator != null) {
                    progressIndicator.setFraction((double) ++myCount / fileCount);
                }
                if (isBinary(file))
                    return;
                final List<LocalInspectionToolWrapper> lTools = new ArrayList<>();
                for (final Tools tools : inspectionTools) {
                    final InspectionToolWrapper tool = tools.getEnabledTool(file, includeDoNotShow);
                    if (tool instanceof LocalInspectionToolWrapper) {
                        lTools.add((LocalInspectionToolWrapper) tool);
                        tool.initialize(GlobalInspectionContextImpl.this);
                    }
                }
                if (!lTools.isEmpty()) {
                    try {
                        final LocalInspectionsPass pass = new LocalInspectionsPass(file, PsiDocumentManager.getInstance(getProject()).getDocument(file), range != null ? range.getStartOffset() : 0, range != null ? range.getEndOffset() : file.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, HighlightInfoProcessor.getEmpty());
                        Runnable runnable = () -> pass.doInspectInBatch(GlobalInspectionContextImpl.this, InspectionManager.getInstance(getProject()), lTools);
                        ApplicationManager.getApplication().runReadAction(runnable);
                        final Set<ProblemDescriptor> localDescriptors = new TreeSet<>(CommonProblemDescriptor.DESCRIPTOR_COMPARATOR);
                        for (LocalInspectionToolWrapper tool : lTools) {
                            InspectionToolPresentation toolPresentation = getPresentation(tool);
                            for (CommonProblemDescriptor descriptor : toolPresentation.getProblemDescriptors()) {
                                if (descriptor instanceof ProblemDescriptor) {
                                    localDescriptors.add((ProblemDescriptor) descriptor);
                                }
                            }
                        }
                        if (searchScope instanceof LocalSearchScope) {
                            for (Iterator<ProblemDescriptor> iterator = localDescriptors.iterator(); iterator.hasNext(); ) {
                                final ProblemDescriptor descriptor = iterator.next();
                                final TextRange infoRange = descriptor instanceof ProblemDescriptorBase ? ((ProblemDescriptorBase) descriptor).getTextRange() : null;
                                if (infoRange != null && !((LocalSearchScope) searchScope).containsRange(file, infoRange)) {
                                    iterator.remove();
                                }
                            }
                        }
                        if (!localDescriptors.isEmpty()) {
                            descriptors.addAll(localDescriptors);
                            files.add(file);
                        }
                    } finally {
                        myPresentationMap.clear();
                    }
                }
            }
        });
    } finally {
        refManager.inspectionReadActionFinished();
    }
    if (files.isEmpty()) {
        GuiUtils.invokeLaterIfNeeded(() -> {
            if (commandName != null) {
                NOTIFICATION_GROUP.createNotification(InspectionsBundle.message("inspection.no.problems.message", scope.getFileCount(), scope.getDisplayName()), MessageType.INFO).notify(getProject());
            }
            if (postRunnable != null) {
                postRunnable.run();
            }
        }, ModalityState.defaultModalityState());
        return;
    }
    Runnable runnable = () -> {
        if (!FileModificationService.getInstance().preparePsiElementsForWrite(files))
            return;
        CleanupInspectionIntention.applyFixesNoSort(getProject(), "Code Cleanup", descriptors, null);
        if (postRunnable != null) {
            postRunnable.run();
        }
    };
    TransactionGuard.submitTransaction(getProject(), runnable);
}
Also used : DefaultInspectionToolPresentation(com.intellij.codeInspection.ui.DefaultInspectionToolPresentation) InspectionToolPresentation(com.intellij.codeInspection.ui.InspectionToolPresentation) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) LocalInspectionsPass(com.intellij.codeInsight.daemon.impl.LocalInspectionsPass) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 7 with RefManagerImpl

use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.

the class ViewOfflineResultsAction method showOfflineView.

@NotNull
public static InspectionResultsView showOfflineView(@NotNull Project project, @NotNull Map<String, Map<String, Set<OfflineProblemDescriptor>>> resMap, @NotNull InspectionProfileImpl inspectionProfile, @NotNull String title) {
    final AnalysisScope scope = new AnalysisScope(project);
    final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project);
    final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false);
    context.setExternalProfile(inspectionProfile);
    context.setCurrentScope(scope);
    context.initializeTools(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    final InspectionResultsView view = new InspectionResultsView(context, new OfflineInspectionRVContentProvider(resMap, project));
    ((RefManagerImpl) context.getRefManager()).startOfflineView();
    context.addView(view, title, true);
    view.update();
    return view;
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) GlobalInspectionContextImpl(com.intellij.codeInspection.ex.GlobalInspectionContextImpl) OfflineInspectionRVContentProvider(com.intellij.codeInspection.offlineViewer.OfflineInspectionRVContentProvider) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) InspectionManagerEx(com.intellij.codeInspection.ex.InspectionManagerEx) InspectionResultsView(com.intellij.codeInspection.ui.InspectionResultsView) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with RefManagerImpl

use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.

the class GlobalInspectionContextImpl method runGlobalTools.

private void runGlobalTools(@NotNull final AnalysisScope scope, @NotNull final InspectionManager inspectionManager, @NotNull List<Tools> globalTools, boolean isOfflineInspections) {
    LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed() || isOfflineInspections, "Must not run under read action, too unresponsive");
    final List<InspectionToolWrapper> needRepeatSearchRequest = new ArrayList<>();
    final boolean canBeExternalUsages = !(scope.getScopeType() == AnalysisScope.PROJECT && scope.isIncludeTestSource());
    for (Tools tools : globalTools) {
        for (ScopeToolState state : tools.getTools()) {
            final InspectionToolWrapper toolWrapper = state.getTool();
            final GlobalInspectionTool tool = (GlobalInspectionTool) toolWrapper.getTool();
            final InspectionToolPresentation toolPresentation = getPresentation(toolWrapper);
            try {
                if (tool.isGraphNeeded()) {
                    try {
                        ((RefManagerImpl) getRefManager()).findAllDeclarations();
                    } catch (Throwable e) {
                        getStdJobDescriptors().BUILD_GRAPH.setDoneAmount(0);
                        throw e;
                    }
                }
                ApplicationManager.getApplication().runReadAction(() -> {
                    tool.runInspection(scope, inspectionManager, this, toolPresentation);
                    //skip phase when we are sure that scope already contains everything, unused declaration though needs to proceed with its suspicious code
                    if ((canBeExternalUsages || tool.getAdditionalJobs(this) != null) && tool.queryExternalUsagesRequests(inspectionManager, this, toolPresentation)) {
                        needRepeatSearchRequest.add(toolWrapper);
                    }
                });
            } catch (ProcessCanceledException | IndexNotReadyException e) {
                throw e;
            } catch (Throwable e) {
                LOG.error(e);
            }
        }
    }
    for (GlobalInspectionContextExtension extension : myExtensions.values()) {
        try {
            extension.performPostRunActivities(needRepeatSearchRequest, this);
        } catch (ProcessCanceledException | IndexNotReadyException e) {
            throw e;
        } catch (Throwable e) {
            LOG.error(e);
        }
    }
    addProblemsToView(globalTools);
}
Also used : DefaultInspectionToolPresentation(com.intellij.codeInspection.ui.DefaultInspectionToolPresentation) InspectionToolPresentation(com.intellij.codeInspection.ui.InspectionToolPresentation) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) GlobalInspectionContextExtension(com.intellij.codeInspection.lang.GlobalInspectionContextExtension) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException)

Example 9 with RefManagerImpl

use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.

the class QuickFixAction method doApplyFix.

private void doApplyFix(@NotNull final Project project, @NotNull final CommonProblemDescriptor[] descriptors, @NotNull Set<VirtualFile> readOnlyFiles, @NotNull final GlobalInspectionContextImpl context) {
    if (!FileModificationService.getInstance().prepareVirtualFilesForWrite(project, readOnlyFiles))
        return;
    final RefManagerImpl refManager = (RefManagerImpl) context.getRefManager();
    final boolean initial = refManager.isInProcess();
    refManager.inspectionReadActionFinished();
    try {
        final Set<PsiElement> ignoredElements = new HashSet<>();
        performFixesInBatch(project, descriptors, context, ignoredElements);
        refreshViews(project, ignoredElements, myToolWrapper);
    } finally {
        //to make offline view lazy
        if (initial)
            refManager.inspectionReadActionStarted();
    }
}
Also used : RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) PsiElement(com.intellij.psi.PsiElement) THashSet(gnu.trove.THashSet)

Example 10 with RefManagerImpl

use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.

the class QuickFixAction method doApplyFix.

private void doApplyFix(@NotNull final RefEntity[] refElements, @NotNull InspectionResultsView view) {
    final RefManagerImpl refManager = (RefManagerImpl) view.getGlobalInspectionContext().getRefManager();
    final boolean initial = refManager.isInProcess();
    refManager.inspectionReadActionFinished();
    try {
        final boolean[] refreshNeeded = { false };
        if (refElements.length > 0) {
            final Project project = refElements[0].getRefManager().getProject();
            CommandProcessor.getInstance().executeCommand(project, () -> {
                CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
                ApplicationManager.getApplication().runWriteAction(() -> {
                    refreshNeeded[0] = applyFix(refElements);
                });
            }, getTemplatePresentation().getText(), null);
        }
        if (refreshNeeded[0]) {
            refreshViews(view.getProject(), refElements, myToolWrapper);
        }
    } finally {
        //to make offline view lazy
        if (initial)
            refManager.inspectionReadActionStarted();
    }
}
Also used : Project(com.intellij.openapi.project.Project) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl)

Aggregations

RefManagerImpl (com.intellij.codeInspection.reference.RefManagerImpl)10 AnalysisScope (com.intellij.analysis.AnalysisScope)3 RefElement (com.intellij.codeInspection.reference.RefElement)3 DefaultInspectionToolPresentation (com.intellij.codeInspection.ui.DefaultInspectionToolPresentation)3 InspectionToolPresentation (com.intellij.codeInspection.ui.InspectionToolPresentation)3 Project (com.intellij.openapi.project.Project)3 THashSet (gnu.trove.THashSet)3 NotNull (org.jetbrains.annotations.NotNull)3 LocalInspectionsPass (com.intellij.codeInsight.daemon.impl.LocalInspectionsPass)2 GlobalInspectionContextExtension (com.intellij.codeInspection.lang.GlobalInspectionContextExtension)2 RefEntity (com.intellij.codeInspection.reference.RefEntity)2 RefVisitor (com.intellij.codeInspection.reference.RefVisitor)2 InspectionResultsView (com.intellij.codeInspection.ui.InspectionResultsView)2 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)2 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)2 SearchScope (com.intellij.psi.search.SearchScope)2 HashSet (com.intellij.util.containers.HashSet)2 AnalysisUIOptions (com.intellij.analysis.AnalysisUIOptions)1 PerformAnalysisInBackgroundOption (com.intellij.analysis.PerformAnalysisInBackgroundOption)1 FileModificationService (com.intellij.codeInsight.FileModificationService)1