One way to implement annotations in IntelliJ is to extend an
ExternalAnnotator has the lowest priority and is run after completion of other background tasks. I proposed this method in my original GSoC proposal, but after a thorough discussion with my mentors, we came to a conclusion that this method cannot be used in this particular case. The annotations must not be forced on the user (automatic), but he should have the ability to run the annotations at his will (manual).
This posed 2 major problems,
- IntelliJ has somewhat basic documentation on
ExternalAnnotatorand more information can be gained by tearing down already created plugins, the other method that we wish to implement not so much.
- The problem of creating in-IDE annotations itself.
In this blog post I’ll discuss Problem #2
It is really easy to create annotations, even if not using the
ExternalAnnotator class (it has a built in function to do so).
AnnotationHolderImpl annotationHolder = new AnnotationHolderImpl(new AnnotationSesstion(psiFile))
and we have just created ourselves an
AnnotationHolder object that is bound to our
Now, to create annotations use,
Annotation annotation = annotationHolder.createErrorAnnotation(textRange, message)
It’s as easy as that.
We now have our little annotation objects, but how do we render them in the IDE you ask? Here’s how:
HighlightInfo objects, and call the
setHighlightersToEditor method of
HighlightersUtil class which does its magic.
final HighlighInfo highlightInfo = HighlightInfo.fromAnnotation(annotation) highInfoList.add(highlightInfo) HighlightersUtil.setHighlightersToEditor(project, document, 0, psiFile.getTextLength(), highlightInfoList, editorColorScheme, HIGHLIGHT_GROUP_ID)
And we’re done. The problems are now annotated inside the IDE (simple yellow highlight for warnings or red squigly lines for errors).
We have created our annotations but a situation may arise when we have to remove them. Looks simple enough? Just call a method that removes the created annotations. Yeah, IntelliJ doesn’t provide this. So I still gotta figure it out how to implement that. Maybe I’ll leave that for tomorrow.
Until the next update.