Search in sources :

Example 36 with HighlightDisplayKey

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

the class XmlImportOptimizer method processFile.

@NotNull
@Override
public CollectingInfoRunnable processFile(final PsiFile file) {
    return new CollectingInfoRunnable() {

        int myRemovedNameSpaces = 0;

        @Override
        public void run() {
            XmlFile xmlFile = (XmlFile) file;
            Project project = xmlFile.getProject();
            HighlightDisplayKey key = HighlightDisplayKey.find(myInspection.getShortName());
            if (!InspectionProjectProfileManager.getInstance(project).getCurrentProfile().isToolEnabled(key, xmlFile))
                return;
            ProblemsHolder holder = new ProblemsHolder(InspectionManager.getInstance(project), xmlFile, false);
            final XmlElementVisitor visitor = (XmlElementVisitor) myInspection.buildVisitor(holder, false);
            new PsiRecursiveElementVisitor() {

                @Override
                public void visitElement(PsiElement element) {
                    if (element instanceof XmlAttribute) {
                        visitor.visitXmlAttribute((XmlAttribute) element);
                    } else {
                        super.visitElement(element);
                    }
                }
            }.visitFile(xmlFile);
            ProblemDescriptor[] results = holder.getResultsArray();
            List<ProblemDescriptor> list = ContainerUtil.filter(results, myCondition);
            Map<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fixes = new LinkedHashMap<>();
            for (ProblemDescriptor result : list) {
                for (QuickFix fix : result.getFixes()) {
                    if (fix instanceof XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) {
                        fixes.put((XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) fix, result);
                    }
                }
            }
            SmartPsiElementPointer<XmlTag> pointer = null;
            for (Map.Entry<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fix : fixes.entrySet()) {
                pointer = fix.getKey().doFix(project, fix.getValue(), false);
                myRemovedNameSpaces++;
            }
            if (pointer != null) {
                XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix.reformatStartTag(project, pointer);
            }
        }

        @Nullable
        @Override
        public String getUserNotificationInfo() {
            return myRemovedNameSpaces > 0 ? "Removed " + myRemovedNameSpaces + " namespace" + (myRemovedNameSpaces > 1 ? "s" : "") : null;
        }
    };
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) XmlUnusedNamespaceInspection(com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) LinkedHashMap(java.util.LinkedHashMap) Project(com.intellij.openapi.project.Project) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with HighlightDisplayKey

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

the class DomHighlightingLiteTest method registerInspectionKey.

private static void registerInspectionKey(MyDomElementsInspection inspection) {
    final String shortName = inspection.getShortName();
    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
    if (key == null) {
        HighlightDisplayKey.register(shortName);
    }
}
Also used : HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey)

Example 38 with HighlightDisplayKey

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

the class AndroidLintExternalAnnotator method apply.

@Override
public void apply(@NotNull PsiFile file, State state, @NotNull AnnotationHolder holder) {
    if (state.isDirty()) {
        return;
    }
    final Project project = file.getProject();
    for (ProblemData problemData : state.getProblems()) {
        final Issue issue = problemData.getIssue();
        final String message = problemData.getMessage();
        final TextRange range = problemData.getTextRange();
        if (range.getStartOffset() == range.getEndOffset()) {
            continue;
        }
        final Pair<AndroidLintInspectionBase, HighlightDisplayLevel> pair = AndroidLintUtil.getHighlighLevelAndInspection(project, issue, file);
        if (pair == null) {
            continue;
        }
        final AndroidLintInspectionBase inspection = pair.getFirst();
        HighlightDisplayLevel displayLevel = pair.getSecond();
        if (inspection != null) {
            final HighlightDisplayKey key = HighlightDisplayKey.find(inspection.getShortName());
            if (key != null) {
                final PsiElement startElement = file.findElementAt(range.getStartOffset());
                final PsiElement endElement = file.findElementAt(range.getEndOffset() - 1);
                if (startElement != null && endElement != null && !inspection.isSuppressedFor(startElement)) {
                    if (problemData.getConfiguredSeverity() != null) {
                        HighlightDisplayLevel configuredLevel = AndroidLintInspectionBase.toHighlightDisplayLevel(problemData.getConfiguredSeverity());
                        if (configuredLevel != null) {
                            displayLevel = configuredLevel;
                        }
                    }
                    final Annotation annotation = createAnnotation(holder, message, range, displayLevel, issue);
                    for (AndroidLintQuickFix fix : inspection.getQuickFixes(startElement, endElement, message)) {
                        if (fix.isApplicable(startElement, endElement, AndroidQuickfixContexts.EditorContext.TYPE)) {
                            annotation.registerFix(new MyFixingIntention(fix, startElement, endElement));
                        }
                    }
                    for (IntentionAction intention : inspection.getIntentions(startElement, endElement)) {
                        annotation.registerFix(intention);
                    }
                    String id = key.getID();
                    if (IntellijLintIssueRegistry.CUSTOM_ERROR == issue || IntellijLintIssueRegistry.CUSTOM_WARNING == issue) {
                        Issue original = IntellijLintClient.findCustomIssue(message);
                        if (original != null) {
                            id = original.getId();
                        }
                    }
                    annotation.registerFix(new SuppressLintIntentionAction(id, startElement));
                    annotation.registerFix(new MyDisableInspectionFix(key));
                    annotation.registerFix(new MyEditInspectionToolsSettingsAction(key, inspection));
                    if (INCLUDE_IDEA_SUPPRESS_ACTIONS) {
                        final SuppressQuickFix[] suppressActions = inspection.getBatchSuppressActions(startElement);
                        for (SuppressQuickFix action : suppressActions) {
                            if (action.isAvailable(project, startElement)) {
                                ProblemHighlightType type = annotation.getHighlightType();
                                annotation.registerFix(action, null, key, InspectionManager.getInstance(project).createProblemDescriptor(startElement, endElement, message, type, true, LocalQuickFix.EMPTY_ARRAY));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiElement(com.intellij.psi.PsiElement)

Example 39 with HighlightDisplayKey

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

the class AndroidLintUtil method getHighlighLevelAndInspection.

@Nullable
public static Pair<AndroidLintInspectionBase, HighlightDisplayLevel> getHighlighLevelAndInspection(@NotNull Project project, @NotNull Issue issue, @NotNull PsiElement context) {
    final String inspectionShortName = AndroidLintInspectionBase.getInspectionShortNameByIssue(project, issue);
    if (inspectionShortName == null) {
        return null;
    }
    final HighlightDisplayKey key = HighlightDisplayKey.find(inspectionShortName);
    if (key == null) {
        return null;
    }
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(context.getProject()).getInspectionProfile();
    if (!profile.isToolEnabled(key, context)) {
        if (!issue.isEnabledByDefault()) {
        // Lint will skip issues (and not report them) for issues that have been disabled,
        // except for those issues that are explicitly enabled via Gradle. Therefore, if
        // we get this far, lint has found this issue to be explicitly enabled, so we let
        // that setting override a local enabled/disabled state in the IDE profile.
        } else {
            return null;
        }
    }
    final AndroidLintInspectionBase inspection = (AndroidLintInspectionBase) profile.getUnwrappedTool(inspectionShortName, context);
    if (inspection == null)
        return null;
    final HighlightDisplayLevel errorLevel = profile.getErrorLevel(key, context);
    return Pair.create(inspection, errorLevel != null ? errorLevel : HighlightDisplayLevel.WARNING);
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) InspectionProfile(com.intellij.codeInspection.InspectionProfile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with HighlightDisplayKey

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

the class HtmlMissingClosingTagInspectionTest method highlightTest.

protected void highlightTest(@Language("HTML") String code) {
    final LocalInspectionTool inspection = getInspection();
    myFixture.enableInspections(inspection);
    final HighlightDisplayKey displayKey = HighlightDisplayKey.find(inspection.getShortName());
    final Project project = myFixture.getProject();
    final InspectionProfileImpl currentProfile = ProjectInspectionProfileManager.getInstance(project).getCurrentProfile();
    final HighlightDisplayLevel errorLevel = currentProfile.getErrorLevel(displayKey, null);
    if (errorLevel == HighlightDisplayLevel.DO_NOT_SHOW) {
        currentProfile.setErrorLevel(displayKey, HighlightDisplayLevel.WARNING, project);
    }
    myFixture.configureByText(HtmlFileType.INSTANCE, code);
    myFixture.testHighlighting();
}
Also used : Project(com.intellij.openapi.project.Project) InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool)

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