Search in sources :

Example 1 with LocalInspectionsPass

use of com.intellij.codeInsight.daemon.impl.LocalInspectionsPass in project intellij-community by JetBrains.

the class DomUIFactoryImpl method createDomHighlighter.

@Override
public BackgroundEditorHighlighter createDomHighlighter(final Project project, final PerspectiveFileEditor editor, final DomElement element) {
    return new BackgroundEditorHighlighter() {

        @Override
        @NotNull
        public HighlightingPass[] createPassesForEditor() {
            if (!element.isValid())
                return HighlightingPass.EMPTY_ARRAY;
            final XmlFile psiFile = DomUtil.getFile(element);
            final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
            final Document document = psiDocumentManager.getDocument(psiFile);
            if (document == null)
                return HighlightingPass.EMPTY_ARRAY;
            editor.commit();
            GeneralHighlightingPass ghp = new GeneralHighlightingPass(project, psiFile, document, 0, document.getTextLength(), true, new ProperTextRange(0, document.getTextLength()), null, new DefaultHighlightInfoProcessor());
            LocalInspectionsPass lip = new LocalInspectionsPass(psiFile, document, 0, document.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, new DefaultHighlightInfoProcessor());
            return new HighlightingPass[] { ghp, lip };
        }

        @Override
        @NotNull
        public HighlightingPass[] createPassesForVisibleArea() {
            return createPassesForEditor();
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) LocalInspectionsPass(com.intellij.codeInsight.daemon.impl.LocalInspectionsPass) ProperTextRange(com.intellij.openapi.util.ProperTextRange) BackgroundEditorHighlighter(com.intellij.codeHighlighting.BackgroundEditorHighlighter) Document(com.intellij.openapi.editor.Document) DefaultHighlightInfoProcessor(com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor) HighlightingPass(com.intellij.codeHighlighting.HighlightingPass) GeneralHighlightingPass(com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) GeneralHighlightingPass(com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass)

Example 2 with LocalInspectionsPass

use of com.intellij.codeInsight.daemon.impl.LocalInspectionsPass in project intellij-community by JetBrains.

the class GlobalInspectionContextImpl method inspectFile.

private void inspectFile(@NotNull final PsiFile file, @NotNull final InspectionManager inspectionManager, @NotNull List<Tools> localTools, @NotNull List<Tools> globalSimpleTools, @NotNull final Map<String, InspectionToolWrapper> wrappersMap) {
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
    if (document == null)
        return;
    VirtualFile virtualFile = file.getVirtualFile();
    String url = ProjectUtilCore.displayUrlRelativeToProject(virtualFile, virtualFile.getPresentableUrl(), getProject(), true, false);
    incrementJobDoneAmount(getStdJobDescriptors().LOCAL_ANALYSIS, url);
    final LocalInspectionsPass pass = new LocalInspectionsPass(file, document, 0, file.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, HighlightInfoProcessor.getEmpty());
    try {
        boolean includeDoNotShow = includeDoNotShow(getCurrentProfile());
        final List<LocalInspectionToolWrapper> lTools = getWrappersFromTools(localTools, file, includeDoNotShow);
        List<LocalInspectionToolWrapper> nonExternalAnnotators = lTools.stream().filter(wrapper -> !(wrapper.getTool() instanceof ExternalAnnotatorBatchInspection)).collect(Collectors.toList());
        pass.doInspectInBatch(this, inspectionManager, nonExternalAnnotators);
        List<GlobalInspectionToolWrapper> globalSTools = getWrappersFromTools(globalSimpleTools, file, includeDoNotShow);
        final List<GlobalInspectionToolWrapper> tools = globalSTools.stream().filter(wrapper -> !(wrapper.getTool() instanceof ExternalAnnotatorBatchInspection)).collect(Collectors.toList());
        JobLauncher.getInstance().invokeConcurrentlyUnderProgress(tools, myProgressIndicator, false, toolWrapper -> {
            GlobalSimpleInspectionTool tool = (GlobalSimpleInspectionTool) toolWrapper.getTool();
            ProblemsHolder holder = new ProblemsHolder(inspectionManager, file, false);
            ProblemDescriptionsProcessor problemDescriptionProcessor = getProblemDescriptionProcessor(toolWrapper, wrappersMap);
            tool.checkFile(file, inspectionManager, holder, this, problemDescriptionProcessor);
            InspectionToolPresentation toolPresentation = getPresentation(toolWrapper);
            LocalDescriptorsUtil.addProblemDescriptors(holder.getResults(), false, this, null, CONVERT, toolPresentation);
            return true;
        });
    } catch (ProcessCanceledException e) {
        final Throwable cause = e.getCause();
        if (cause == null) {
            throw e;
        }
        LOG.error("In file: " + file, cause);
    } catch (IndexNotReadyException e) {
        throw e;
    } catch (Throwable e) {
        LOG.error("In file: " + file.getName(), e);
    } finally {
        InjectedLanguageManager.getInstance(getProject()).dropFileCaches(file);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) com.intellij.openapi.util(com.intellij.openapi.util) UIUtil(com.intellij.util.ui.UIUtil) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) MessageType(com.intellij.openapi.ui.MessageType) ToggleAction(com.intellij.openapi.actionSystem.ToggleAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) Document(com.intellij.openapi.editor.Document) HashSet(com.intellij.util.containers.HashSet) DefaultInspectionToolPresentation(com.intellij.codeInspection.ui.DefaultInspectionToolPresentation) THashSet(gnu.trove.THashSet) TripleFunction(com.intellij.util.TripleFunction) com.intellij.openapi.application(com.intellij.openapi.application) ProjectUtilCore(com.intellij.openapi.project.ProjectUtilCore) ProblemHighlightFilter(com.intellij.codeInsight.daemon.ProblemHighlightFilter) FileIndex(com.intellij.openapi.roots.FileIndex) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) InspectionToolPresentation(com.intellij.codeInspection.ui.InspectionToolPresentation) FileModificationService(com.intellij.codeInsight.FileModificationService) com.intellij.openapi.progress(com.intellij.openapi.progress) RefElement(com.intellij.codeInspection.reference.RefElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) PathMacroManager(com.intellij.openapi.components.PathMacroManager) java.util.concurrent(java.util.concurrent) InspectionResultsView(com.intellij.codeInspection.ui.InspectionResultsView) ProjectUtil(com.intellij.openapi.project.ProjectUtil) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) JobLauncherImpl(com.intellij.concurrency.JobLauncherImpl) com.intellij.ui.content(com.intellij.ui.content) java.util(java.util) LocalInspectionsPass(com.intellij.codeInsight.daemon.impl.LocalInspectionsPass) CharsetToolkit(com.intellij.openapi.vfs.CharsetToolkit) CleanupInspectionIntention(com.intellij.codeInspection.actions.CleanupInspectionIntention) PerformAnalysisInBackgroundOption(com.intellij.analysis.PerformAnalysisInBackgroundOption) JobLauncher(com.intellij.concurrency.JobLauncher) GlobalInspectionContextExtension(com.intellij.codeInspection.lang.GlobalInspectionContextExtension) SensitiveProgressWrapper(com.intellij.concurrency.SensitiveProgressWrapper) NonNls(org.jetbrains.annotations.NonNls) ProgressIndicatorUtils(com.intellij.openapi.progress.util.ProgressIndicatorUtils) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) Constructor(java.lang.reflect.Constructor) ThreadDumper(com.intellij.diagnostic.ThreadDumper) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) CoreProgressManager(com.intellij.openapi.progress.impl.CoreProgressManager) ProblemGroup(com.intellij.lang.annotation.ProblemGroup) NotificationGroup(com.intellij.notification.NotificationGroup) ToolWindowId(com.intellij.openapi.wm.ToolWindowId) Project(com.intellij.openapi.project.Project) OutputStreamWriter(java.io.OutputStreamWriter) RefVisitor(com.intellij.codeInspection.reference.RefVisitor) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) StringUtil(com.intellij.openapi.util.text.StringUtil) HighlightInfoProcessor(com.intellij.codeInsight.daemon.impl.HighlightInfoProcessor) AnalysisScope(com.intellij.analysis.AnalysisScope) FileOutputStream(java.io.FileOutputStream) ConcurrencyUtil(com.intellij.util.ConcurrencyUtil) IOException(java.io.IOException) com.intellij.codeInspection(com.intellij.codeInspection) AnalysisUIOptions(com.intellij.analysis.AnalysisUIOptions) GuiUtils(com.intellij.ui.GuiUtils) InspectionTreeState(com.intellij.codeInspection.ui.InspectionTreeState) Disposable(com.intellij.openapi.Disposable) File(java.io.File) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) Element(org.jdom.Element) RefEntity(com.intellij.codeInspection.reference.RefEntity) DefaultInspectionToolPresentation(com.intellij.codeInspection.ui.DefaultInspectionToolPresentation) InspectionToolPresentation(com.intellij.codeInspection.ui.InspectionToolPresentation) Document(com.intellij.openapi.editor.Document) LocalInspectionsPass(com.intellij.codeInsight.daemon.impl.LocalInspectionsPass) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException)

Example 3 with LocalInspectionsPass

use of com.intellij.codeInsight.daemon.impl.LocalInspectionsPass 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)

Aggregations

LocalInspectionsPass (com.intellij.codeInsight.daemon.impl.LocalInspectionsPass)3 RefManagerImpl (com.intellij.codeInspection.reference.RefManagerImpl)2 DefaultInspectionToolPresentation (com.intellij.codeInspection.ui.DefaultInspectionToolPresentation)2 InspectionToolPresentation (com.intellij.codeInspection.ui.InspectionToolPresentation)2 Document (com.intellij.openapi.editor.Document)2 AnalysisScope (com.intellij.analysis.AnalysisScope)1 AnalysisUIOptions (com.intellij.analysis.AnalysisUIOptions)1 PerformAnalysisInBackgroundOption (com.intellij.analysis.PerformAnalysisInBackgroundOption)1 BackgroundEditorHighlighter (com.intellij.codeHighlighting.BackgroundEditorHighlighter)1 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)1 FileModificationService (com.intellij.codeInsight.FileModificationService)1 ProblemHighlightFilter (com.intellij.codeInsight.daemon.ProblemHighlightFilter)1 DefaultHighlightInfoProcessor (com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor)1 GeneralHighlightingPass (com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass)1 HighlightInfoProcessor (com.intellij.codeInsight.daemon.impl.HighlightInfoProcessor)1 com.intellij.codeInspection (com.intellij.codeInspection)1 CleanupInspectionIntention (com.intellij.codeInspection.actions.CleanupInspectionIntention)1 GlobalInspectionContextExtension (com.intellij.codeInspection.lang.GlobalInspectionContextExtension)1 RefElement (com.intellij.codeInspection.reference.RefElement)1 RefEntity (com.intellij.codeInspection.reference.RefEntity)1