BNF Rule Syntax Highlighting for Custom Language
Asked Answered
E

1

8

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));
    }
}
East answered 3/1, 2014 at 5:29 Comment(0)
M
5

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

Mezzorilievo answered 4/1, 2014 at 18:18 Comment(2)
Perfect. For those that are interested, check my question for how to do this.East
How does it compare, performance-wise, with token-based highlighting (i.e. using SyntaxHighlighter)?Genarogendarme

© 2022 - 2024 — McMap. All rights reserved.