Search in sources :

Example 31 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel in project intellij-community by JetBrains.

the class ToolsImpl method readExternal.

void readExternal(@NotNull Element toolElement, @NotNull InspectionProfileManager profileManager, Map<String, List<String>> dependencies) {
    final String levelName = toolElement.getAttributeValue(LEVEL_ATTRIBUTE);
    final SeverityRegistrar registrar = profileManager.getOwnSeverityRegistrar();
    HighlightDisplayLevel level = levelName != null ? HighlightDisplayLevel.find(registrar.getSeverity(levelName)) : null;
    if (level == null) {
        level = HighlightDisplayLevel.WARNING;
    }
    myDefaultState.setLevel(level);
    final String enabled = toolElement.getAttributeValue(ENABLED_ATTRIBUTE);
    final boolean isEnabled = enabled != null && Boolean.parseBoolean(enabled);
    final String enabledTool = toolElement.getAttributeValue(ENABLED_BY_DEFAULT_ATTRIBUTE);
    myDefaultState.setEnabled(enabledTool != null ? Boolean.parseBoolean(enabledTool) : isEnabled);
    final InspectionToolWrapper toolWrapper = myDefaultState.getTool();
    final List<Element> scopeElements = toolElement.getChildren(ProfileEx.SCOPE);
    if (!scopeElements.isEmpty()) {
        final List<String> scopeNames = new SmartList<>();
        for (Element scopeElement : scopeElements) {
            final String scopeName = scopeElement.getAttributeValue(ProfileEx.NAME);
            if (scopeName == null) {
                continue;
            }
            final NamedScopesHolder scopesHolder = profileManager.getScopesManager();
            NamedScope namedScope = null;
            if (scopesHolder != null) {
                namedScope = scopesHolder.getScope(scopeName);
            }
            final String errorLevel = scopeElement.getAttributeValue(LEVEL_ATTRIBUTE);
            final String enabledInScope = scopeElement.getAttributeValue(ENABLED_ATTRIBUTE);
            final InspectionToolWrapper copyToolWrapper = toolWrapper.createCopy();
            // check if unknown children exists
            if (scopeElement.getAttributes().size() > 3 || !scopeElement.getChildren().isEmpty()) {
                copyToolWrapper.getTool().readSettings(scopeElement);
            }
            HighlightDisplayLevel scopeLevel = errorLevel != null ? HighlightDisplayLevel.find(registrar.getSeverity(errorLevel)) : null;
            if (scopeLevel == null) {
                scopeLevel = level;
            }
            if (namedScope != null) {
                addTool(namedScope, copyToolWrapper, enabledInScope != null && Boolean.parseBoolean(enabledInScope), scopeLevel);
            } else {
                addTool(scopeName, copyToolWrapper, enabledInScope != null && Boolean.parseBoolean(enabledInScope), scopeLevel);
            }
            scopeNames.add(scopeName);
        }
        for (int i = 0; i < scopeNames.size(); i++) {
            String scopeName = scopeNames.get(i);
            List<String> order = dependencies.get(scopeName);
            if (order == null) {
                order = new ArrayList<>();
                dependencies.put(scopeName, order);
            }
            for (int j = i + 1; j < scopeNames.size(); j++) {
                order.add(scopeNames.get(j));
            }
        }
    }
    // check if unknown children exists
    if (toolElement.getAttributes().size() > 4 || toolElement.getChildren().size() > scopeElements.size()) {
        toolWrapper.getTool().readSettings(toolElement);
    }
    myEnabled = isEnabled;
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) PsiElement(com.intellij.psi.PsiElement) Element(org.jdom.Element) SeverityRegistrar(com.intellij.codeInsight.daemon.impl.SeverityRegistrar) SmartList(com.intellij.util.SmartList)

Example 32 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel 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 33 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel 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 34 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel 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

HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)34 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)14 PsiElement (com.intellij.psi.PsiElement)13 Project (com.intellij.openapi.project.Project)7 Element (org.jdom.Element)6 InspectionProfileImpl (com.intellij.codeInspection.ex.InspectionProfileImpl)5 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)5 NotNull (org.jetbrains.annotations.NotNull)5 SeverityRegistrar (com.intellij.codeInsight.daemon.impl.SeverityRegistrar)4 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)4 Annotation (com.intellij.lang.annotation.Annotation)4 Issue (com.android.tools.lint.detector.api.Issue)3 InspectionProfile (com.intellij.codeInspection.InspectionProfile)3 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)3 NamedScope (com.intellij.psi.search.scope.packageSet.NamedScope)3 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)3 Nullable (org.jetbrains.annotations.Nullable)3 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 TextRange (com.intellij.openapi.util.TextRange)2