private void annotateNumber(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { Annotation annotation = holder.createInfoAnnotation(element, null); annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.NUMBER); } }
private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull TextAttributesKey key) { holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER); String description = ApplicationManager.getApplication().isUnitTestMode() ? key.getExternalName() : null; holder.createInfoAnnotation(element, description).setTextAttributes(key); }
/** * Highlights `textRange` with the given `textAttributesKey`. * * @param textRange textRange in the document to highlight * @param annotationHolder the container which receives annotations created by the plugin. * @param textAttributesKey text attributes to apply to the `node`. */ private void highlight(@NotNull final TextRange textRange, @NotNull AnnotationHolder annotationHolder, @NotNull final TextAttributesKey textAttributesKey) { annotationHolder.createInfoAnnotation(textRange, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER); annotationHolder.createInfoAnnotation(textRange, null).setEnforcedTextAttributes(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(textAttributesKey)); }
private void annotateBinaryData(BashBinaryDataElement element, AnnotationHolder annotationHolder) { Annotation annotation = annotationHolder.createInfoAnnotation(element, null); annotation.setEnforcedTextAttributes(TextAttributes.ERASE_MARKER); annotation = annotationHolder.createInfoAnnotation(element, null); annotation.setTextAttributes(BashSyntaxHighlighter.BINARY_DATA); annotation.setNeedsUpdateOnTyping(false); }
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())); }
protected boolean handleSeparatorElement(@NotNull PsiElement element, @NotNull AnnotationHolder holder, IElementType elementType, CsvFile csvFile) { if (elementType == CsvTypes.COMMA) { TextAttributes textAttributes = holder.getCurrentAnnotationSession().getUserData(TAB_SEPARATOR_HIGHLIGHT_COLOR_KEY); if (!Boolean.TRUE.equals(holder.getCurrentAnnotationSession().getUserData(TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED_KEY))) { String separator = CsvCodeStyleSettings.getCurrentSeparator(csvFile.getProject(), csvFile.getLanguage()); if (CsvEditorSettingsExternalizable.getInstance().isHighlightTabSeparator() && separator.equals(CsvCodeStyleSettings.TAB_SEPARATOR)) { textAttributes = new TextAttributes(null, CsvEditorSettingsExternalizable.getInstance().getTabHighlightColor(), null, null, 0); holder.getCurrentAnnotationSession().putUserData(TAB_SEPARATOR_HIGHLIGHT_COLOR_KEY, textAttributes); holder.getCurrentAnnotationSession().putUserData(TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED_KEY, Boolean.TRUE); } } if (textAttributes != null) { Annotation annotation = holder.createAnnotation( CSV_COLUMN_INFO_SEVERITY, element.getTextRange(), showInfoBalloon(holder.getCurrentAnnotationSession()) ? "↹" : null ); annotation.setEnforcedTextAttributes(textAttributes); annotation.setNeedsUpdateOnTyping(false); } return true; } return false; }
private void annotateHereDoc(BashHereDoc element, AnnotationHolder annotationHolder) { final Annotation annotation = annotationHolder.createInfoAnnotation(element, null); annotation.setTextAttributes(BashSyntaxHighlighter.HERE_DOC); annotation.setNeedsUpdateOnTyping(false); if (element.isEvaluatingVariables()) { highlightVariables(element, annotationHolder); } }
if (!(PsiTreeUtil.getParentOfType(element, GoForStatement.class, GoFunctionLit.class) instanceof GoForStatement)) { Annotation annotation = holder.createErrorAnnotation(element, "Continue statement not inside a for loop"); annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element)); if (GoPsiImplUtil.getBreakStatementOwner(element) == null) { Annotation annotation = holder.createErrorAnnotation(element, "Break statement not inside a for loop, select or switch"); annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element)); Annotation annotation = holder.createErrorAnnotation(result, declaration.getName() + " function must have no arguments and no return values"); annotation.registerFix(new GoEmptySignatureQuickFix(declaration)); Annotation annotation = holder.createErrorAnnotation(parameters, declaration.getName() + " function must have no arguments and no return values"); annotation.registerFix(new GoEmptySignatureQuickFix(declaration)); TextRange r = TextRange.create(secondColon.getTextRange().getStartOffset(), thirdIndex.getTextRange().getEndOffset()); Annotation annotation = holder.createErrorAnnotation(r, "Invalid operation " + slice.getText() + " (3-index slice of string)"); annotation.registerFix(new GoDeleteRangeQuickFix(secondColon, thirdIndex, "Delete third index"));
@Test public void testRangeIssues() { createStoredIssues(5); annotator.apply(psiFile, ctx, holder); for (int i = 0; i < 5; i++) { assertThat(holder.get(i).getStartOffset()).isEqualTo(i); assertThat(holder.get(i).getEndOffset()).isEqualTo(i + 10); assertThat(holder.get(i).getMessage()).contains("issue " + i); assertThat(holder.get(i).getSeverity()).isEqualTo(HighlightSeverity.WARNING); assertThat(holder.get(i).isFileLevelAnnotation()).isFalse(); } }
private void warn( final PsiElement psiElement, final AnnotationHolder annotationHolder, final PsiElement searchableCurrentElement, final String warning) { final PsiReference first = ReferencesSearch.search(searchableCurrentElement).findFirst(); if (first == null) { Annotation annotation = annotationHolder.createWeakWarningAnnotation(psiElement, warning); annotation.setHighlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL); } } }
public void assertAnnotationContains(String filename, String content, String contains) { List<String> matches = new ArrayList<String>(); for (Annotation annotation : getAnnotationsAtCaret(filename, content)) { matches.add(annotation.toString()); if(annotation.getMessage().contains(contains)) { return; } } fail(String.format("Fail matches '%s' with one of %s", contains, matches)); }
public void assertAnnotationNotContains(String filename, String content, String contains) { for (Annotation annotation : getAnnotationsAtCaret(filename, content)) { if(annotation.getMessage().contains(contains)) { fail(String.format("Fail not matching '%s' with '%s'", contains, annotation)); } } }
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); }
/** * Highlights `textRange` with the given `textAttributesKey`. * * @param textRange textRange in the document to highlight * @param annotationHolder the container which receives annotations created by the plugin. * @param textAttributesKey text attributes to apply to the `node`. */ private void highlight(@NotNull final TextRange textRange, @NotNull AnnotationHolder annotationHolder, @NotNull final TextAttributesKey textAttributesKey) { annotationHolder.createInfoAnnotation(textRange, null) .setEnforcedTextAttributes(TextAttributes.ERASE_MARKER); annotationHolder.createInfoAnnotation(textRange, null) .setEnforcedTextAttributes( EditorColorsManager.getInstance().getGlobalScheme().getAttributes(textAttributesKey) ); } }
annotation.setEnforcedTextAttributes( CsvEditorSettingsExternalizable.getInstance().isColumnHighlightingEnabled() ? CsvColorSettings.getTextAttributesOfColumn(columnInfo.getColumnIndex(), holder.getCurrentAnnotationSession()) : null ); annotation.setNeedsUpdateOnTyping(false);
void validate( final String key, final List<Field> availableKeys, final PsiElement psiElement, final AnnotationHolder annotationHolder) { if (shouldIgnore(key, psiElement)) { return; } if (isInvalid(key, availableKeys)) { final Annotation errorAnnotation = annotationHolder.createErrorAnnotation(psiElement, "Invalid key"); errorAnnotation.registerFix(intentionAction); } }
@Test public void testFileLevelIssues() { createFileIssues(5); annotator.apply(psiFile, ctx, holder); assertThat(holder).hasSize(5); for (int i = 0; i < 5; i++) { assertThat(holder.get(i).getStartOffset()).isEqualTo(psiFileRange.getStartOffset()); assertThat(holder.get(i).getEndOffset()).isEqualTo(psiFileRange.getEndOffset()); assertThat(holder.get(i).isFileLevelAnnotation()).isTrue(); assertThat(holder.get(i).getMessage()).contains("issue " + i); assertThat(holder.get(i).getSeverity()).isEqualTo(HighlightSeverity.WARNING); } }
private void warn( final PsiElement psiElement, final AnnotationHolder annotationHolder, final PsiElement searchableCurrentElement, final String warning) { final PsiReference first = ReferencesSearch.search(searchableCurrentElement).findFirst(); if (first == null) { Annotation annotation = annotationHolder.createWeakWarningAnnotation(psiElement, warning); annotation.setHighlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL); } } }
/** * @see TemplateAnnotationAnnotator#annotate */ public void testThatTemplateCreationAnnotationProvidesQuickfix() { PsiFile psiFile = myFixture.configureByText("foobar.php", "<?php\n" + "use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;\n" + "\n" + "class Foobar\n" + "{\n" + " /**\n" + " * @Temp<caret>late(\"foobar.html.twig\")\n" + " */\n" + " public function fooAction()\n" + " {\n" + " }\n" + "}\n" + "" ); PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); PsiElement phpDocTag = psiElement.getParent(); AnnotationHolderImpl annotations = new AnnotationHolderImpl(new AnnotationSession(psiFile)); new TemplateAnnotationAnnotator().annotate(new PhpAnnotationDocTagAnnotatorParameter( PhpIndex.getInstance(getProject()).getAnyByFQN(TwigUtil.TEMPLATE_ANNOTATION_CLASS).iterator().next(), (PhpDocTag) phpDocTag, annotations )); assertNotNull( annotations.stream().findFirst().filter(annotation -> annotation.getMessage().contains("Create Template")) ); }
private void annotateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { Annotation annotation = holder.createInfoAnnotation(element, null); annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.STRING); }