BNF Rule Syntax Highlighting for Custom Language

1.8k views Asked by At

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.

Here's a quick and dirty example language - https://github.com/carymrobbins/intellij-plugin-example

SOLUTION

As @ignatov suggests, extend the Annotator class and register it in your plugin.xml. In the example below, we highlight the command elements by defining a visitCommand method.

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));
    }
}
1

There are 1 answers

2
ignatov On BEST ANSWER

Use custom implementation of com.intellij.lang.annotation.Annotator.