Search in sources :

Example 21 with HighlightDisplayLevel

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

the class InspectionValidatorWrapper method runInspectionTool.

private static Map<ProblemDescriptor, HighlightDisplayLevel> runInspectionTool(final PsiFile file, final LocalInspectionTool inspectionTool, final HighlightDisplayLevel level) {
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<>();
    for (ProblemDescriptor descriptor : runInspectionOnFile(file, inspectionTool)) {
        final ProblemHighlightType highlightType = descriptor.getHighlightType();
        final HighlightDisplayLevel highlightDisplayLevel;
        if (highlightType == ProblemHighlightType.WEAK_WARNING) {
            highlightDisplayLevel = HighlightDisplayLevel.WEAK_WARNING;
        } else if (highlightType == ProblemHighlightType.INFORMATION) {
            highlightDisplayLevel = HighlightDisplayLevel.DO_NOT_SHOW;
        } else {
            highlightDisplayLevel = level;
        }
        problemsMap.put(descriptor, highlightDisplayLevel);
    }
    return problemsMap;
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 22 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel in project android 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();
    if (DumbService.isDumb(project))
        return;
    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 (LintIdeIssueRegistry.CUSTOM_ERROR == issue || LintIdeIssueRegistry.CUSTOM_WARNING == issue) {
                        Issue original = LintIdeClient.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 (issue == DeprecationDetector.ISSUE || issue == GradleDetector.DEPRECATED) {
                        annotation.setHighlightType(ProblemHighlightType.LIKE_DEPRECATED);
                    }
                    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.lint.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 23 with HighlightDisplayLevel

use of com.intellij.codeHighlighting.HighlightDisplayLevel in project android by JetBrains.

the class LintNotificationPanel method updateExplanation.

private void updateExplanation(@Nullable IssueData selected) {
    // We have the capability to show markup text here, e.g.
    // myExplanationPane.setContentType(UIUtil.HTML_MIME)
    // and then use an HtmlBuilder to populate it with for
    // example issue.getExplanation(HTML). However, the builtin
    // HTML formatter ends up using a bunch of weird fonts etc
    // so the dialog just ends up looking tacky.
    String headerFontColor = HtmlBuilderHelper.getHeaderFontColor();
    HtmlBuilder builder = new HtmlBuilder();
    builder.openHtmlBody();
    if (selected != null) {
        builder.addHeading("Message: ", headerFontColor);
        builder.add(selected.message).newline();
        // Look for quick fixes
        AndroidLintInspectionBase inspection = selected.inspection;
        AndroidLintQuickFix[] quickFixes = inspection.getQuickFixes(selected.startElement, selected.endElement, selected.message);
        IntentionAction[] intentions = inspection.getIntentions(selected.startElement, selected.endElement);
        builder.addHeading("Suggested Fixes:", headerFontColor).newline();
        builder.beginList();
        for (final AndroidLintQuickFix fix : quickFixes) {
            builder.listItem();
            builder.addLink(fix.getName(), myLinkManager.createRunnableLink(() -> {
                myPopup.cancel();
                // TODO: Pull in editor context?
                WriteCommandAction.runWriteCommandAction(selected.startElement.getProject(), () -> {
                    fix.apply(selected.startElement, selected.endElement, AndroidQuickfixContexts.BatchContext.getInstance());
                });
            }));
        }
        for (final IntentionAction fix : intentions) {
            builder.listItem();
            builder.addLink(fix.getText(), myLinkManager.createRunnableLink(() -> {
                NlModel model = myScreenView.getModel();
                Editor editor = PsiEditorUtil.Service.getInstance().findEditorByPsiElement(selected.startElement);
                if (editor != null) {
                    editor.getCaretModel().getCurrentCaret().moveToOffset(selected.startElement.getTextOffset());
                    myPopup.cancel();
                    WriteCommandAction.runWriteCommandAction(model.getProject(), () -> {
                        fix.invoke(model.getProject(), editor, model.getFile());
                    });
                }
            }));
        }
        final SuppressLintIntentionAction suppress = new SuppressLintIntentionAction(selected.issue, selected.startElement);
        builder.listItem();
        builder.addLink(suppress.getText(), myLinkManager.createRunnableLink(() -> {
            myPopup.cancel();
            WriteCommandAction.runWriteCommandAction(selected.startElement.getProject(), () -> {
                suppress.invoke(selected.startElement.getProject(), null, myScreenView.getModel().getFile());
            });
        }));
        builder.endList();
        Issue issue = selected.issue;
        builder.addHeading("Priority: ", headerFontColor);
        builder.addHtml(String.format("%1$d / 10", issue.getPriority()));
        builder.newline();
        builder.addHeading("Category: ", headerFontColor);
        builder.add(issue.getCategory().getFullName());
        builder.newline();
        builder.addHeading("Severity: ", headerFontColor);
        builder.beginSpan();
        // Use converted level instead of *default* severity such that we match any user configured overrides
        HighlightDisplayLevel level = selected.level;
        builder.add(StringUtil.capitalize(level.getName().toLowerCase(Locale.US)));
        builder.endSpan();
        builder.newline();
        builder.addHeading("Explanation: ", headerFontColor);
        String description = issue.getBriefDescription(HTML);
        builder.addHtml(description);
        if (!description.isEmpty() && Character.isLetter(description.charAt(description.length() - 1))) {
            builder.addHtml(".");
        }
        builder.newline();
        String explanationHtml = issue.getExplanation(HTML);
        builder.addHtml(explanationHtml);
        List<String> moreInfo = issue.getMoreInfo();
        builder.newline();
        int count = moreInfo.size();
        if (count > 1) {
            builder.addHeading("More Info: ", headerFontColor);
            builder.beginList();
        }
        for (String uri : moreInfo) {
            if (count > 1) {
                builder.listItem();
            }
            builder.addLink(uri, uri);
        }
        if (count > 1) {
            builder.endList();
        }
        builder.newline();
    }
    builder.closeHtmlBody();
    try {
        myExplanationPane.read(new StringReader(builder.getHtml()), null);
        HtmlBuilderHelper.fixFontStyles(myExplanationPane);
        myExplanationPane.setCaretPosition(0);
    } catch (IOException ignore) {
    // can't happen for internal string reading
    }
}
Also used : AndroidLintInspectionBase(org.jetbrains.android.inspections.lint.AndroidLintInspectionBase) Issue(com.android.tools.lint.detector.api.Issue) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) HtmlBuilder(com.android.utils.HtmlBuilder) NlModel(com.android.tools.idea.uibuilder.model.NlModel) SuppressLintIntentionAction(com.android.tools.idea.lint.SuppressLintIntentionAction) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) IOException(java.io.IOException) RelativePoint(com.intellij.ui.awt.RelativePoint) SuppressLintIntentionAction(com.android.tools.idea.lint.SuppressLintIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) StringReader(java.io.StringReader) Editor(com.intellij.openapi.editor.Editor)

Example 24 with HighlightDisplayLevel

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

the class MultiScopeSeverityIcon method paintIcon.

@Override
public void paintIcon(final Component c, final Graphics g, final int i, final int j) {
    final int partWidth = getIconWidth() / myScopeToAverageSeverityMap.size();
    final Collection<HighlightDisplayLevel> values = myScopeToAverageSeverityMap.values();
    int idx = 0;
    for (final HighlightDisplayLevel level : values) {
        final Icon icon = level.getIcon();
        g.setColor(icon instanceof ColoredIcon ? ((ColoredIcon) icon).getColor() : getMixedSeverityColor());
        final int x = i + partWidth * idx;
        g.fillRect(x, j, partWidth, getIconHeight());
        idx++;
    }
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) ColoredIcon(com.intellij.codeHighlighting.HighlightDisplayLevel.ColoredIcon) ColoredIcon(com.intellij.codeHighlighting.HighlightDisplayLevel.ColoredIcon)

Example 25 with HighlightDisplayLevel

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

the class SeverityRegistrar method getRendererIconByIndex.

public Icon getRendererIconByIndex(int i) {
    final HighlightSeverity severity = getSeverityByIndex(i);
    HighlightDisplayLevel level = HighlightDisplayLevel.find(severity);
    if (level != null) {
        return level.getIcon();
    }
    return HighlightDisplayLevel.createIconByMask(myRendererColors.get(severity.getName()));
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel)

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