title: BNF Rule Syntax Highlighting for Custom Language
body: <p>I'm trying to develop a custom language plugin for IntelliJ using the Grammar-Kit plugin.  I am easily able to provide syntax highlighting for tokens defined, but I cannot figure out how to highlight at the element or token-parent level.</p>
  
  <p>Here's a quick and dirty example language - <a href="https://github.com/carymrobbins/intellij-plugin-example" rel="noreferrer">https://github.com/carymrobbins/intellij-plugin-example</a></p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>As @ignatov suggests, extend the Annotator class and register it in your <code>plugin.xml</code>.  In the example below, we highlight the <code>command</code> elements by defining a <code>visitCommand</code> method.</p>
  
  <pre><code>public class SimpleAnnotator implements Annotator {
      @Override
      public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
          element.accept(new SimpleVisitor() {
              @Override
              public void visitCommand(@NotNull SimpleCommand o) {
                  super.visitCommand(o);
                  setHighlighting(o, holder, SimpleSyntaxHighlighter.COMMAND);
              }
          });
      }
  
      private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
                                          @NotNull TextAttributesKey key) {
          holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
          holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(
                  EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));
      }
  }
  </code></pre>