Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BNF Rule Syntax Highlighting for Custom Language

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));
    }
}
like image 1000
pyrospade Avatar asked Jan 03 '14 05:01

pyrospade


1 Answers

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

like image 180
ignatov Avatar answered Sep 23 '22 07:09

ignatov