package com.haskforce;

import com.haskforce.highlighting.HaskellSyntaxHighlighter;
import com.haskforce.psi.*;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

public class HaskellAnnotator implements Annotator {
    @Override
    public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
        element.accept(new HaskellVisitor() {
            @Override
            public void visitNcomment(@NotNull HaskellNcomment o) {
                super.visitNcomment(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.NCOMMENT);
            }

            @Override
            public void visitPragma(@NotNull HaskellPragma o) {
                super.visitPragma(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.PRAGMA);
            }

            @Override
            public void visitQvarid(@NotNull HaskellQvarid o) {
                super.visitQvarid(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.VARID);
            }

            @Override
            public void visitQinfixvarid(@NotNull HaskellQinfixvarid o) {
                super.visitQinfixvarid(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.INFIXVARID);
            }

            @Override
            public void visitQvarsym(@NotNull HaskellQvarsym o) {
                super.visitQvarsym(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.VARSYM);
            }

            @Override
            public void visitQconsym(@NotNull HaskellQconsym o) {
                super.visitQconsym(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.CONSYM);
            }

            @Override
            public void visitPstringtoken(@NotNull HaskellPstringtoken o) {
                super.visitPstringtoken(o);
                setHighlightingRecursive(o, holder, HaskellSyntaxHighlighter.STRING);
            }

            @Override
            public void visitReservedop(@NotNull HaskellReservedop o) {
                super.visitReservedop(o);
                setHighlighting(o, holder, HaskellSyntaxHighlighter.RESERVEDOP);
            }
        });
    }

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

    private static void setHighlightingRecursive(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
                                                 @NotNull TextAttributesKey key) {
        setHighlighting(element, holder, key);
        for (PsiElement child : element.getChildren()) {
            setHighlightingRecursive(child, holder, key);
        }
    }
}

@@ -839,33 +839,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -1057,33 +1057,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -1273,33 +1273,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -1503,33 +1503,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -1726,33 +1726,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -1945,33 +1945,24 @@
 Highlighting
-Recursive
 (o, holder, 
@@ -2183,25 +2183,16 @@
 lighting
-Recursive
 (o, hold
@@ -2752,578 +2752,97 @@
 tes(
-TextAttributes.ERASE_MARKER);%0A        holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(%0A                EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));%0A    %7D%0A%0A    private static void setHighlightingRecursive(@NotNull PsiElement element, @NotNull AnnotationHolder holder,%0A                                                 @NotNull TextAttributesKey key) %7B%0A        setHighlighting(element, holder, key);%0A        for (PsiElement child : element.getChildren()) %7B%0A            setHighlightingRecursive(child, holder, key);%0A        %7D
+%0A                EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));
 %0A   

Remove setHighlightingRecursive and simplify the body of setHighlighting. Fixes #14.