Search in sources :

Example 16 with HighlightDisplayKey

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

the class InspectionProfileImpl method addTool.

public void addTool(@Nullable Project project, @NotNull InspectionToolWrapper toolWrapper, @NotNull Map<String, List<String>> dependencies) {
    final String shortName = toolWrapper.getShortName();
    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
    if (key == null) {
        final InspectionEP extension = toolWrapper.getExtension();
        Computable<String> computable = extension == null ? new Computable.PredefinedValueComputable<>(toolWrapper.getDisplayName()) : extension::getDisplayName;
        if (toolWrapper instanceof LocalInspectionToolWrapper) {
            key = HighlightDisplayKey.register(shortName, computable, toolWrapper.getID(), ((LocalInspectionToolWrapper) toolWrapper).getAlternativeID());
        } else {
            key = HighlightDisplayKey.register(shortName, computable);
        }
    }
    if (key == null) {
        LOG.error(shortName + " ; number of initialized tools: " + myTools.size());
        return;
    }
    HighlightDisplayLevel baseLevel = myBaseProfile != null && myBaseProfile.getToolsOrNull(shortName, project) != null ? myBaseProfile.getErrorLevel(key, project) : HighlightDisplayLevel.DO_NOT_SHOW;
    HighlightDisplayLevel defaultLevel = toolWrapper.getDefaultLevel();
    HighlightDisplayLevel level = baseLevel.getSeverity().compareTo(defaultLevel.getSeverity()) > 0 ? baseLevel : defaultLevel;
    boolean enabled = myBaseProfile != null ? myBaseProfile.isToolEnabled(key) : toolWrapper.isEnabledByDefault();
    final ToolsImpl toolsList = new ToolsImpl(toolWrapper, level, !myLockedProfile && enabled, enabled);
    final Element element = myUninitializedSettings.remove(shortName);
    try {
        if (element != null) {
            getPathMacroManager().expandPaths(element);
            toolsList.readExternal(element, getProfileManager(), dependencies);
        } else if (!myUninitializedSettings.containsKey(InspectionElementsMergerBase.getMergedMarkerName(shortName))) {
            final InspectionElementsMergerBase merger = getMerger(shortName);
            Element merged = merger == null ? null : merger.merge(myUninitializedSettings);
            if (merged != null) {
                getPathMacroManager().expandPaths(merged);
                toolsList.readExternal(merged, getProfileManager(), dependencies);
            } else if (isProfileLocked()) {
                // https://youtrack.jetbrains.com/issue/IDEA-158936
                toolsList.setEnabled(false);
                if (toolsList.getNonDefaultTools() == null) {
                    toolsList.getDefaultState().setEnabled(false);
                }
            }
        }
    } catch (InvalidDataException e) {
        LOG.error("Can't read settings for " + toolWrapper, e);
    }
    myTools.put(shortName, toolsList);
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) PsiElement(com.intellij.psi.PsiElement) Element(org.jdom.Element) InspectionEP(com.intellij.codeInspection.InspectionEP)

Example 17 with HighlightDisplayKey

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

the class AndroidLintExternalAnnotator method getIssuesFromInspections.

@NotNull
static List<Issue> getIssuesFromInspections(@NotNull Project project, @Nullable PsiElement context) {
    final List<Issue> result = new ArrayList<Issue>();
    final IssueRegistry fullRegistry = new IntellijLintIssueRegistry();
    for (Issue issue : fullRegistry.getIssues()) {
        final String inspectionShortName = AndroidLintInspectionBase.getInspectionShortNameByIssue(project, issue);
        if (inspectionShortName == null) {
            continue;
        }
        final HighlightDisplayKey key = HighlightDisplayKey.find(inspectionShortName);
        if (key == null) {
            continue;
        }
        final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
        final boolean enabled = context != null ? profile.isToolEnabled(key, context) : profile.isToolEnabled(key);
        if (!enabled) {
            continue;
        } else if (!issue.isEnabledByDefault()) {
            // If an issue is marked as not enabled by default, lint won't run it, even if it's in the set
            // of issues provided by an issue registry. Since in the IDE we're enforcing the enabled-state via
            // inspection profiles, mark the issue as enabled to allow users to turn on a lint check directly
            // via the inspections UI.
            issue.setEnabledByDefault(true);
        }
        result.add(issue);
    }
    return result;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ArrayList(java.util.ArrayList) IssueRegistry(com.android.tools.klint.client.api.IssueRegistry) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with HighlightDisplayKey

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

the class ResourceTypeInspection method getSuppressActions.

@NotNull
@Override
public SuppressIntentionAction[] getSuppressActions(final PsiElement element) {
    // The suppress actions are hardcoded to use the suppress id "ResourceType",
    // e.g. the inspection id for the whole ResourceTypeInspection.
    // However, we'd really like to have more specific resource id's instead;
    // in particular, the same ones used by the command line version of the same
    // check in lint (in SupportAnnotationDetector).
    //
    // We can't change the id used by the SuppressQuickFix, and we can't just
    // replace the SuppressQuickFix array passed to convertBatchToSuppressIntentionActions
    // because the SuppressQuickFix interface ony gives us the element, not the
    // warning error message (and we need to use the error message to figure out
    // the corresponding lint issue type for an inspection message).
    //
    // Therefore, we wrap the each SuppressIntentionAction with a delegator,
    // and when the user actually invokes the SuppressIntentionAction, we
    // look up the current message, and if we can find the corresponding
    // lint issue we create a new SuppressQuickFix (with the right id),
    // wrap it, and invoke that action instead.
    String shortName = getShortName();
    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
    SuppressQuickFix[] actions = SuppressManager.getInstance().createBatchSuppressActions(key);
    SuppressIntentionAction[] suppressActions = SuppressIntentionActionFromFix.convertBatchToSuppressIntentionActions(actions);
    if (suppressActions.length == actions.length) {
        int index = 0;
        List<SuppressIntentionAction> replaced = Lists.newArrayListWithExpectedSize(suppressActions.length);
        for (SuppressIntentionAction action : suppressActions) {
            replaced.add(new MyDelegatingSuppressAction(action, actions[index++]));
        }
        return replaced.toArray(new SuppressIntentionAction[replaced.size()]);
    }
    return suppressActions;
}
Also used : HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with HighlightDisplayKey

use of com.intellij.codeInsight.daemon.HighlightDisplayKey in project android 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()).getCurrentProfile();
    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 20 with HighlightDisplayKey

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

the class NlLintHighlightingPass method getAnnotations.

@NotNull
private static LintAnnotationsModel getAnnotations(@NotNull NlModel model, @NotNull ProgressIndicator progress) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    LintAnnotationsModel lintModel = new LintAnnotationsModel();
    XmlFile xmlFile = model.getFile();
    AndroidLintExternalAnnotator annotator = new AndroidLintExternalAnnotator();
    State state = annotator.collectInformation(xmlFile);
    if (state != null) {
        state = annotator.doAnnotate(state);
    }
    if (state == null) {
        return lintModel;
    }
    for (ProblemData problemData : state.getProblems()) {
        if (progress.isCanceled()) {
            break;
        }
        TextRange range = problemData.getTextRange();
        final PsiElement startElement = xmlFile.findElementAt(range.getStartOffset());
        final PsiElement endElement = xmlFile.findElementAt(range.getEndOffset());
        if (startElement == null || endElement == null) {
            continue;
        }
        NlComponent component = model.findViewByPsi(startElement);
        if (component == null) {
            continue;
        }
        Issue issue = problemData.getIssue();
        Pair<AndroidLintInspectionBase, HighlightDisplayLevel> pair = AndroidLintUtil.getHighlighLevelAndInspection(xmlFile.getProject(), issue, xmlFile);
        if (pair == null) {
            continue;
        }
        AndroidLintInspectionBase inspection = pair.getFirst();
        if (inspection == null) {
            continue;
        }
        HighlightDisplayLevel level = pair.getSecond();
        HighlightDisplayKey key = HighlightDisplayKey.find(inspection.getShortName());
        if (key == null) {
            continue;
        }
        lintModel.addIssue(component, issue, problemData.getMessage(), inspection, level, startElement, endElement);
    }
    return lintModel;
}
Also used : Issue(com.android.tools.lint.detector.api.Issue) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) XmlFile(com.intellij.psi.xml.XmlFile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) TextRange(com.intellij.openapi.util.TextRange) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

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