7. Annotator
An Annotator
helps highlight and annotate any code based on specific rules. This section adds annotation functionality to support the Simple Language in the context of Java code.
Required Project Configuration Changes
Classes defined in this step of the tutorial depend on com.intellij.psi.PsiLiteralExpression
(the PSI representation for String literals in Java code) at runtime. Using PsiLiteralExpression
introduces a dependency on com.intellij.java
.
Beginning in version 2019.2, a dependency on Java plugin must be declared explicitly. First, add a dependency on the Java plugin in the Gradle build script:
Then, declare the dependency in plugin.xml (use code insight)
Define an Annotator
The SimpleAnnotator
subclasses Annotator
. Consider a literal string that starts with "simple:" as a prefix of a Simple Language key. It isn't part of the Simple Language, but it is a useful convention for detecting Simple Language keys embedded as string literals in other languages, like Java. Annotate the simple:key
literal expression, and differentiate between a well-formed vs. an unresolved property.
Register the Annotator
Using the com.intellij.annotator
extension point in the plugin configuration file, register the Simple Language annotator class for JAVA
language:
Run the Project
Run the plugin by using the Gradle runIde
task.
As a test, define the following Java file containing a Simple Language prefix:value
pair:
Open this Java file in an IDE Development Instance running the simple_language_plugin
to check if the IDE resolves a property:
If the property is an undefined name, the annotator flags the code with an error.
Try changing the Simple Language color settings to differentiate the annotation from the default language color settings.