Search in sources :

Example 26 with HighlightDisplayKey

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

the class ShowIntentionsPass method collectIntentionsFromDoNotShowLeveledInspections.

/**
   * Can be invoked in EDT, each inspection should be fast
   */
private static void collectIntentionsFromDoNotShowLeveledInspections(@NotNull final Project project, @NotNull final PsiFile hostFile, PsiElement psiElement, final int offset, @NotNull final IntentionsInfo intentions) {
    if (psiElement != null) {
        if (!psiElement.isPhysical()) {
            VirtualFile virtualFile = hostFile.getVirtualFile();
            String text = hostFile.getText();
            LOG.error("not physical: '" + psiElement.getText() + "' @" + offset + psiElement.getTextRange() + " elem:" + psiElement + " (" + psiElement.getClass().getName() + ")" + " in:" + psiElement.getContainingFile() + " host:" + hostFile + "(" + hostFile.getClass().getName() + ")", new Attachment(virtualFile != null ? virtualFile.getPresentableUrl() : "null", text != null ? text : "null"));
        }
        if (DumbService.isDumb(project)) {
            return;
        }
        final List<LocalInspectionToolWrapper> intentionTools = new ArrayList<>();
        final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
        final InspectionToolWrapper[] tools = profile.getInspectionTools(hostFile);
        for (InspectionToolWrapper toolWrapper : tools) {
            if (toolWrapper instanceof LocalInspectionToolWrapper && !((LocalInspectionToolWrapper) toolWrapper).isUnfair()) {
                final HighlightDisplayKey key = HighlightDisplayKey.find(toolWrapper.getShortName());
                if (profile.isToolEnabled(key, hostFile) && HighlightDisplayLevel.DO_NOT_SHOW.equals(profile.getErrorLevel(key, hostFile))) {
                    intentionTools.add((LocalInspectionToolWrapper) toolWrapper);
                }
            }
        }
        if (!intentionTools.isEmpty()) {
            final List<PsiElement> elements = new ArrayList<>();
            PsiElement el = psiElement;
            while (el != null) {
                elements.add(el);
                if (el instanceof PsiFile)
                    break;
                el = el.getParent();
            }
            final Set<String> dialectIds = InspectionEngine.calcElementDialectIds(elements);
            final LocalInspectionToolSession session = new LocalInspectionToolSession(hostFile, 0, hostFile.getTextLength());
            final Processor<LocalInspectionToolWrapper> processor = toolWrapper -> {
                final LocalInspectionTool localInspectionTool = toolWrapper.getTool();
                final HighlightDisplayKey key = HighlightDisplayKey.find(toolWrapper.getShortName());
                final String displayName = toolWrapper.getDisplayName();
                final ProblemsHolder holder = new ProblemsHolder(InspectionManager.getInstance(project), hostFile, true) {

                    @Override
                    public void registerProblem(@NotNull ProblemDescriptor problemDescriptor) {
                        super.registerProblem(problemDescriptor);
                        if (problemDescriptor instanceof ProblemDescriptorBase) {
                            final TextRange range = ((ProblemDescriptorBase) problemDescriptor).getTextRange();
                            if (range != null && range.contains(offset)) {
                                final QuickFix[] fixes = problemDescriptor.getFixes();
                                if (fixes != null) {
                                    for (int k = 0; k < fixes.length; k++) {
                                        final IntentionAction intentionAction = QuickFixWrapper.wrap(problemDescriptor, k);
                                        final HighlightInfo.IntentionActionDescriptor actionDescriptor = new HighlightInfo.IntentionActionDescriptor(intentionAction, null, displayName, null, key, null, HighlightSeverity.INFORMATION);
                                        intentions.intentionsToShow.add(actionDescriptor);
                                    }
                                }
                            }
                        }
                    }
                };
                InspectionEngine.createVisitorAndAcceptElements(localInspectionTool, holder, true, session, elements, dialectIds, InspectionEngine.getDialectIdsSpecifiedForTool(toolWrapper));
                localInspectionTool.inspectionFinished(session, holder);
                return true;
            };
            JobLauncher.getInstance().invokeConcurrentlyUnderProgress(intentionTools, new DaemonProgressIndicator(), false, processor);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) QuickFixWrapper(com.intellij.codeInspection.ex.QuickFixWrapper) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlightingLevelManager(com.intellij.codeInsight.daemon.impl.analysis.HighlightingLevelManager) Logger(com.intellij.openapi.diagnostic.Logger) CommonProcessors(com.intellij.util.CommonProcessors) RangeMarker(com.intellij.openapi.editor.RangeMarker) DumbService(com.intellij.openapi.project.DumbService) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) IntentionHintComponent(com.intellij.codeInsight.intention.impl.IntentionHintComponent) Processors(com.intellij.util.Processors) Set(java.util.Set) TextRange(com.intellij.openapi.util.TextRange) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CleanupAllIntention(com.intellij.codeInspection.actions.CleanupAllIntention) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) TextEditorHighlightingPass(com.intellij.codeHighlighting.TextEditorHighlightingPass) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IntentionManagerSettings(com.intellij.codeInsight.intention.impl.config.IntentionManagerSettings) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) NotNull(org.jetbrains.annotations.NotNull) InspectionProjectProfileManager(com.intellij.profile.codeInspection.InspectionProjectProfileManager) TemplateManagerImpl(com.intellij.codeInsight.template.impl.TemplateManagerImpl) JobLauncher(com.intellij.concurrency.JobLauncher) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) NonNls(org.jetbrains.annotations.NonNls) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) IntentionManager(com.intellij.codeInsight.intention.IntentionManager) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Segment(com.intellij.openapi.util.Segment) InjectedLanguageUtil(com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Iterator(java.util.Iterator) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) com.intellij.codeInspection(com.intellij.codeInspection) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) Editor(com.intellij.openapi.editor.Editor) java.awt(java.awt) ShowIntentionActionsHandler(com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper) Pair(com.intellij.openapi.util.Pair) Attachment(com.intellij.openapi.diagnostic.Attachment) HintManager(com.intellij.codeInsight.hint.HintManager) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ArrayList(java.util.ArrayList) Attachment(com.intellij.openapi.diagnostic.Attachment) PsiFile(com.intellij.psi.PsiFile) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) PsiElement(com.intellij.psi.PsiElement) TextRange(com.intellij.openapi.util.TextRange) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper)

Example 27 with HighlightDisplayKey

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

the class ExternalToolPass method collectInformationWithProgress.

@Override
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
    final FileViewProvider viewProvider = myFile.getViewProvider();
    final Set<Language> relevantLanguages = viewProvider.getLanguages();
    int externalAnnotatorsInRoots = 0;
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
            continue;
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        externalAnnotatorsInRoots += externalAnnotators.size();
    }
    setProgressLimit(externalAnnotatorsInRoots);
    InspectionProfileImpl profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
            continue;
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        if (!externalAnnotators.isEmpty()) {
            DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
            boolean errorFound = daemonCodeAnalyzer.getFileStatusMap().wasErrorFound(myDocument);
            for (ExternalAnnotator externalAnnotator : externalAnnotators) {
                String shortName = externalAnnotator.getPairedBatchInspectionShortName();
                if (shortName != null) {
                    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
                    LOG.assertTrue(key != null, "Paired tool '" + shortName + "' not found for external annotator: " + externalAnnotator);
                    if (!profile.isToolEnabled(key, myFile))
                        continue;
                }
                final Object collectedInfo;
                Editor editor = getEditor();
                if (editor != null) {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot, editor, errorFound);
                } else {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot);
                }
                advanceProgress(1);
                if (collectedInfo != null) {
                    myAnnotator2DataMap.put(externalAnnotator, new MyData(psiRoot, collectedInfo));
                }
            }
        }
    }
}
Also used : ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 28 with HighlightDisplayKey

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

the class OfflineInspectionResultViewTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    HighlightDisplayKey key = HighlightDisplayKey.find(UnusedDeclarationInspectionBase.SHORT_NAME);
    if (key == null) {
        HighlightDisplayKey.register(UnusedDeclarationInspectionBase.SHORT_NAME);
    }
    final InspectionProfileImpl profile = new InspectionProfileImpl("test") {

        @Override
        public boolean isToolEnabled(@Nullable final HighlightDisplayKey key, PsiElement element) {
            return key != null && Comparing.strEqual(key.toString(), UnusedDeclarationInspectionBase.SHORT_NAME);
        }

        @Override
        @NotNull
        public InspectionToolWrapper[] getInspectionTools(PsiElement element) {
            return new InspectionToolWrapper[] { myUnusedToolWrapper };
        }

        @Override
        @NotNull
        public InspectionProfileModifiableModel getModifiableModel() {
            return new InspectionProfileModifiableModel(this) {

                @Override
                @NotNull
                public InspectionToolWrapper[] getInspectionTools(PsiElement element) {
                    return new InspectionToolWrapper[] { myUnusedToolWrapper };
                }

                @Override
                public boolean isToolEnabled(@Nullable HighlightDisplayKey key, PsiElement element) {
                    return key != null && Comparing.strEqual(key.toString(), UnusedDeclarationInspectionBase.SHORT_NAME);
                }
            };
        }
    };
    myView = ViewOfflineResultsAction.showOfflineView(getProject(), parse(), profile, "");
    myUnusedToolWrapper = new GlobalInspectionToolWrapper(new UnusedDeclarationInspection());
    myDataFlowToolWrapper = new LocalInspectionToolWrapper(new EqualsWithItselfInspection());
    final Map<String, Tools> tools = myView.getGlobalInspectionContext().getTools();
    for (InspectionToolWrapper tool : ContainerUtil.ar(myUnusedToolWrapper, myDataFlowToolWrapper)) {
        profile.addTool(getProject(), tool, new THashMap<>());
        tools.put(tool.getShortName(), new ToolsImpl(tool, tool.getDefaultLevel(), true));
        tool.initialize(myView.getGlobalInspectionContext());
    }
}
Also used : HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) EqualsWithItselfInspection(com.siyeh.ig.bugs.EqualsWithItselfInspection) UnusedDeclarationInspection(com.intellij.codeInspection.deadCode.UnusedDeclarationInspection) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement)

Example 29 with HighlightDisplayKey

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

the class PostHighlightingVisitor method collectHighlights.

void collectHighlights(@NotNull HighlightInfoHolder result, @NotNull ProgressIndicator progress) {
    DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
    FileStatusMap fileStatusMap = daemonCodeAnalyzer.getFileStatusMap();
    InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    boolean unusedSymbolEnabled = profile.isToolEnabled(myDeadCodeKey, myFile);
    GlobalUsageHelper globalUsageHelper = myRefCountHolder.getGlobalUsageHelper(myFile, myDeadCodeInspection, unusedSymbolEnabled);
    boolean errorFound = false;
    if (unusedSymbolEnabled) {
        final FileViewProvider viewProvider = myFile.getViewProvider();
        final Set<Language> relevantLanguages = viewProvider.getLanguages();
        for (Language language : relevantLanguages) {
            progress.checkCanceled();
            PsiElement psiRoot = viewProvider.getPsi(language);
            if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
                continue;
            List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(psiRoot, 0, myFile.getTextLength());
            for (PsiElement element : elements) {
                progress.checkCanceled();
                if (element instanceof PsiIdentifier) {
                    PsiIdentifier identifier = (PsiIdentifier) element;
                    HighlightInfo info = processIdentifier(identifier, progress, globalUsageHelper);
                    if (info != null) {
                        errorFound |= info.getSeverity() == HighlightSeverity.ERROR;
                        result.add(info);
                    }
                }
            }
        }
    }
    HighlightDisplayKey unusedImportKey = HighlightDisplayKey.find(UnusedImportLocalInspection.SHORT_NAME);
    if (isUnusedImportEnabled(unusedImportKey)) {
        PsiImportList importList = ((PsiJavaFile) myFile).getImportList();
        if (importList != null) {
            final PsiImportStatementBase[] imports = importList.getAllImportStatements();
            for (PsiImportStatementBase statement : imports) {
                progress.checkCanceled();
                final HighlightInfo info = processImport(statement, unusedImportKey);
                if (info != null) {
                    errorFound |= info.getSeverity() == HighlightSeverity.ERROR;
                    result.add(info);
                }
            }
        }
    }
    if (errorFound) {
        fileStatusMap.setErrorFoundFlag(myProject, myDocument, true);
    }
    optimizeImportsOnTheFlyLater(progress);
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Language(com.intellij.lang.Language)

Example 30 with HighlightDisplayKey

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

the class HighlightInfo method appendFixes.

private static void appendFixes(@Nullable TextRange fixedRange, @NotNull HighlightInfo info, @Nullable List<Annotation.QuickFixInfo> fixes) {
    if (fixes != null) {
        for (final Annotation.QuickFixInfo quickFixInfo : fixes) {
            TextRange range = fixedRange != null ? fixedRange : quickFixInfo.textRange;
            HighlightDisplayKey key = quickFixInfo.key != null ? quickFixInfo.key : HighlightDisplayKey.find(ANNOTATOR_INSPECTION_SHORT_NAME);
            info.registerFix(quickFixInfo.quickFix, null, HighlightDisplayKey.getDisplayNameByKey(key), range, key);
        }
    }
}
Also used : HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Annotation(com.intellij.lang.annotation.Annotation)

Aggregations

HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)42 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)15 NotNull (org.jetbrains.annotations.NotNull)12 Project (com.intellij.openapi.project.Project)10 PsiElement (com.intellij.psi.PsiElement)10 InspectionProfileImpl (com.intellij.codeInspection.ex.InspectionProfileImpl)7 Nullable (org.jetbrains.annotations.Nullable)7 InspectionProfile (com.intellij.codeInspection.InspectionProfile)6 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)5 Annotation (com.intellij.lang.annotation.Annotation)4 ArrayList (java.util.ArrayList)4 Issue (com.android.tools.lint.detector.api.Issue)3 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)3 PsiFile (com.intellij.psi.PsiFile)3 NamedScope (com.intellij.psi.search.scope.packageSet.NamedScope)3 Element (org.jdom.Element)3 Issue (com.android.tools.klint.detector.api.Issue)2 SeverityRegistrar (com.intellij.codeInsight.daemon.impl.SeverityRegistrar)2 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)2 QuickFix (com.intellij.codeInspection.QuickFix)2