8

我正在尝试使用 Grammar-Kit 插件为 IntelliJ 开发自定义语言插件。我可以轻松地为定义的标记提供语法突出显示,但我无法弄清楚如何在元素或标记父级别突出显示。

这是一个快速而肮脏的示例语言 - https://github.com/carymrobbins/intellij-plugin-example

解决方案

正如@ignatov 建议的那样,扩展 Annotator 类并将其注册到您的plugin.xml. 在下面的示例中,我们command通过定义visitCommand方法来突出显示元素。

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

1 回答 1

5

使用com.intellij.lang.annotation.Annotator.

于 2014-01-04T18:18:16.527 回答