Java Code Examples for com.intellij.lang.annotation.AnnotationHolder#createAnnotation()

The following examples show how to use com.intellij.lang.annotation.AnnotationHolder#createAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseAnnotator.java    From Custom-Syntax-Highlighter with MIT License 5 votes vote down vote up
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {

  if (element instanceof LeafPsiElement) {
    final TextAttributesKey kind = getKeywordKind(element);
    if (kind == null) {
      return;
    }
    final TextRange textRange = element.getTextRange();
    final TextRange range = new TextRange(textRange.getStartOffset(), textRange.getEndOffset());
    final Annotation annotation = holder.createAnnotation(HighlightSeverity.INFORMATION, range, null);

    annotation.setTextAttributes(kind);
  }
}
 
Example 2
Source File: UnusedParameterOrStateAnnotator.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder annotationHolder) {
  if (element instanceof TemplateBlockMixin) {
    // Abort if values are passed with data="...", parameter are sometimes defined for the sake
    // of added documentation even when not technically used directly in the template body.
    if (element.getText().contains("data=")) {
      return;
    }

    Collection<? extends Variable> variables = Streams
        .concat(ParamUtils.getParamDefinitions(element).stream(),
            ParamUtils.getStateDefinitions(element).stream()).collect(
            ImmutableList.toImmutableList());

    Set<String> usedVariableIdentifiers =
        PsiTreeUtil.findChildrenOfType(element, IdentifierElement.class)
            .stream()
            .map(IdentifierElement::getReferences)
            .flatMap(Arrays::stream)
            .map(PsiReference::getCanonicalText)
            .collect(ImmutableSet.toImmutableSet());

    for (Variable variable : variables) {
      if (!usedVariableIdentifiers.contains(variable.name)) {
        Annotation annotation = annotationHolder
            .createAnnotation(((TemplateBlockMixin) element).isElementBlock() ?
                    HighlightSeverity.WEAK_WARNING : HighlightSeverity.ERROR,
                variable.element.getTextRange(),
                variableType(variable) + " " + variable.name + " is unused.");
        annotation.registerFix(isParameter(variable)
            ? new RemoveUnusedParameterFix(variable.name)
            : new RemoveUnusedStateVarFix(variable.name));
      }
    }
  }
}
 
Example 3
Source File: RamlAnnotator.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(@NotNull PsiFile file, RamlValidationResult annotationResult, @NotNull AnnotationHolder holder)
{
    final List<ErrorNode> errorNodes = annotationResult.getErrorNodes();
    for (ErrorNode errorNode : errorNodes)
    {
        if (file.getVirtualFile().getPath().endsWith(errorNode.getStartPosition().getPath()))
        {
            holder.createAnnotation(HighlightSeverity.ERROR, new TextRange(errorNode.getStartPosition().getIndex(), errorNode.getEndPosition().getIndex()), errorNode.getErrorMessage());
        }
    }

}
 
Example 4
Source File: DocumentationUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Annotates the given comment so that tags like @command, @bis, and @fnc (Arma Intellij Plugin specific tags) are properly annotated
 *
 * @param annotator the annotator
 * @param comment   the comment
 */
public static void annotateDocumentationWithArmaPluginTags(@NotNull AnnotationHolder annotator, @NotNull PsiComment comment) {
	List<String> allowedTags = new ArrayList<>(3);
	allowedTags.add("command");
	allowedTags.add("bis");
	allowedTags.add("fnc");
	Pattern patternTag = Pattern.compile("@([a-zA-Z]+) ([a-zA-Z_0-9]+)");
	Matcher matcher = patternTag.matcher(comment.getText());
	String tag;
	Annotation annotation;
	int startTag, endTag, startArg, endArg;
	while (matcher.find()) {
		if (matcher.groupCount() < 2) {
			continue;
		}
		tag = matcher.group(1);
		if (!allowedTags.contains(tag)) {
			continue;
		}
		startTag = matcher.start(1);
		endTag = matcher.end(1);
		startArg = matcher.start(2);
		endArg = matcher.end(2);
		annotation = annotator.createAnnotation(HighlightSeverity.INFORMATION, TextRange.create(comment.getTextOffset() + startTag - 1, comment.getTextOffset() + endTag), null);
		annotation.setTextAttributes(DefaultLanguageHighlighterColors.DOC_COMMENT_TAG);
		annotation = annotator.createAnnotation(HighlightSeverity.INFORMATION, TextRange.create(comment.getTextOffset() + startArg, comment.getTextOffset() + endArg), null);
		annotation.setTextAttributes(DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE);
	}
}
 
Example 5
Source File: DocumentationUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Annotates the given comment so that tags like @command, @bis, and @fnc (Arma Intellij Plugin specific tags) are properly annotated
 *
 * @param annotator the annotator
 * @param comment   the comment
 */
public static void annotateDocumentationWithArmaPluginTags(@NotNull AnnotationHolder annotator, @NotNull PsiComment comment) {
	List<String> allowedTags = new ArrayList<>(3);
	allowedTags.add("command");
	allowedTags.add("bis");
	allowedTags.add("fnc");
	Pattern patternTag = Pattern.compile("@([a-zA-Z]+) ([a-zA-Z_0-9]+)");
	Matcher matcher = patternTag.matcher(comment.getText());
	String tag;
	Annotation annotation;
	int startTag, endTag, startArg, endArg;
	while (matcher.find()) {
		if (matcher.groupCount() < 2) {
			continue;
		}
		tag = matcher.group(1);
		if (!allowedTags.contains(tag)) {
			continue;
		}
		startTag = matcher.start(1);
		endTag = matcher.end(1);
		startArg = matcher.start(2);
		endArg = matcher.end(2);
		annotation = annotator.createAnnotation(HighlightSeverity.INFORMATION, TextRange.create(comment.getTextOffset() + startTag - 1, comment.getTextOffset() + endTag), null);
		annotation.setTextAttributes(DefaultLanguageHighlighterColors.DOC_COMMENT_TAG);
		annotation = annotator.createAnnotation(HighlightSeverity.INFORMATION, TextRange.create(comment.getTextOffset() + startArg, comment.getTextOffset() + endArg), null);
		annotation.setTextAttributes(DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE);
	}
}