private static Optional<Annotation> createAnnotation(AnnotationHolder holder, PsiFile psiFile, SonarIssue issue) { HighlightSeverity severity = SonarToIjSeverityMapping.toHighlightSeverity(issue.getSeverity()); Annotation annotation; if (issue.getLine() == null) { annotation = createAnnotation(holder,issue.formattedMessage(),psiFile,severity); annotation.setFileLevelAnnotation(true); } else { Optional<PsiElement> startElement = Finders.findFirstElementAtLine(psiFile,issue.getLine()); if (!startElement.isPresent()) { // There is no AST element on this line. Maybe a tabulation issue on a blank line? annotation = createAnnotation( holder, issue.formattedMessage(), Finders.getLineRange(psiFile,issue.getLine()), severity ); } else if (startElement.get().isValid()) { TextRange lineRange = Finders.getLineRange(startElement.get()); annotation = createAnnotation(holder,issue.formattedMessage(),lineRange,severity); } else { annotation = null; } } return Optional.ofNullable(annotation); }
private void addAnnotation(LiveIssue issue, AnnotationHolder annotationHolder) { TextRange textRange; if (issue.getRange() != null) { textRange = createTextRange(issue.getRange()); } else { textRange = issue.psiFile().getTextRange(); } String htmlMsg = getHtmlMessage(issue); Annotation annotation = annotationHolder .createAnnotation(getSeverity(issue.getSeverity()), textRange, issue.getMessage(), htmlMsg); annotation.registerFix(new DisableRuleQuickFix(issue.getRuleKey())); if (!issue.flows().isEmpty()) { annotation.registerFix(new ShowLocationsIntention(issue.getRange(), issue.getMessage(), issue.flows())); } if (issue.getRange() == null) { annotation.setFileLevelAnnotation(true); } else { annotation.setTextAttributes(getTextAttrsKey(issue.getSeverity())); } /* * 3 possible ways to set text attributes and error stripe color: * - enforce text attributes ({@link Annotation#setEnforcedTextAttributes}) and we need to set everything * manually (including error stripe color). This won't be configurable in a standard way and won't change based on used color scheme * - rely on one of the default attributes by giving a key {@link com.intellij.openapi.editor.colors.CodeInsightColors} or your own * key (SonarLintTextAttributes) to Annotation#setTextAttributes * - let Annotation#getTextAttributes decide it based on highlight type and severity. */ annotation.setHighlightType(getType(issue.getSeverity())); }