Extend simple annotator for json files?

Answered

I am trying to create a plugin which allows me to add annotations to a json file. Annotations in this context simply mean that the file will be parsed for specific text and if the text is found, there will some sort of a visual indicator in the left or right gutter. Ideally this will be very similar to the external annotations feature, which is currently supported for java only ( I think).

I have looked at https://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/annotator.html

I got the sample to work. But if I change the plugin.xml from:
<annotator language="JAVA" implementationClass="com.simpleplugin.SimpleAnnotator"/>
to:
<annotator language="JSON" implementationClass="com.simpleplugin.SimpleAnnotator"/>

The SimpleAnnotator is not called when a json file is opened. I will appreciate any help you can provide.

0
6 comments

Are you sure it's not called?  Did you set a breakpoint at the first line of the function?

The reason that I say that is that the first line in the SimpleAnnotator example that you cite is checking for a `PsiLiteralExpression`, which is a Java-only element type:

/**
* Represents a Java literal expression.
*/
public interface PsiLiteralExpression extends PsiExpression, PsiLiteral {
}

You will have to determine what element type the JSON parser is producing and check for that instead: JsonObject, JsonProperty, JsonStringLiteral, etc.

 

 

0

@Eric Bishton thanks for responding. My apologies. This is working as expected. My annotations are now showing up.

The second problem I am running into is that I need to color code the annotations. I tried to use the different levels of annotations for this. The error and warning annotations show up correctly. But even after I adjusted the inspection level for JSON files to the info level, info level annotations still don't show up. I am sure I am just missing a simple setting somewhere. Could you please point me in the right direction.

 

 

 

0

If you need to set the color, use 'AnnotationHolder().setTextAttributes(TextAttributesKey)`.

You can use the You probably want to create the keys once and save them off as constants for re-use.  It will look something like this: (Sorry, but I'm in a hurry.)

public static final TextAttributesKey KEYWORD =
createTextAttributesKey("JSON_KEYWORD", DefaultLanguageHighlighterColors.KEYWORD);

Then:

public MyAnnotator implements Annotator {
@Override public void annotate(PsiElement node, AnnotationHolder holder) {
if (element intanceof JSONProperty) {
holder.createInfoAnnotation(node, "It works!").setTextAttributes(MyKeys.KEYWORD);
}
}
}

0

This changes the text color. I need to change the color of the highlight ( in the right gutter). Is there a way? Thanks

0

I think you need to implement a LineMarkerProvider.  Docs are at https://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/line_marker_provider.html 

I realize that the documentation talks about putting the marker in the [left-hand] gutter.  However, the IdentifierHighlighterPass (which adds highlights to the scroll bar for usages of the current identifier under the cursor) uses this extension point.

(BTW, technically, the scroll bar is transparent and painted over the highlights.  The scroll-bar code is entirely separate.)

0

Thanks, is it possible to provide a code sample?

0

Please sign in to leave a comment.