package com.reason.ide.highlight;

import org.jetbrains.annotations.NotNull;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.reason.lang.core.psi.PsiInterpolationReference;
import com.reason.lang.core.psi.PsiModule;
import com.reason.lang.core.type.ORTypes;

import static com.intellij.lang.annotation.HighlightSeverity.INFORMATION;
import static com.reason.ide.highlight.ORSyntaxHighlighter.*;

public abstract class ORSyntaxAnnotator implements Annotator {

    private final ORTypes m_types;

    ORSyntaxAnnotator(ORTypes types) {
        m_types = types;
    }

    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
        IElementType elementType = element.getNode().getElementType();

        if (elementType == m_types.C_UPPER_IDENTIFIER) {
            PsiElement parent = element.getParent();
            if (parent instanceof PsiModule) {
                color(holder, element.getNavigationElement(), MODULE_NAME_);
            }
        } else if (elementType == m_types.C_UPPER_SYMBOL) {
            PsiElement nextElement = element.getNextSibling();
            IElementType nextElementType = nextElement == null ? null : nextElement.getNode().getElementType();
            color(holder, element, (nextElementType == m_types.DOT) ? MODULE_NAME_ : VARIANT_NAME_);
        } else if (elementType == m_types.C_VARIANT_DECL) {
            PsiElement identifier = element.getNavigationElement();
            color(holder, identifier, VARIANT_NAME_);
        } else if (elementType == m_types.C_MACRO_NAME) {
            color(holder, element, ANNOTATION_);
        } else if (elementType == m_types.TAG_NAME || elementType == m_types.TAG_LT || elementType == m_types.TAG_GT || elementType == m_types.TAG_AUTO_CLOSE) {
            color(holder, element, MARKUP_TAG_);
        } else if (elementType == m_types.OPTION) {
            color(holder, element, OPTION_);
        } else if (elementType == m_types.PROPERTY_NAME) {
            color(holder, element, MARKUP_ATTRIBUTE_);
        } else if (elementType == m_types.C_INTERPOLATION_PART) {
            color(holder, element, STRING_);
        } else if (element instanceof PsiInterpolationReference) {
            color(holder, element, DefaultLanguageHighlighterColors.IDENTIFIER);
        }
    }

    private void color(AnnotationHolder holder, PsiElement element, TextAttributesKey key) {
        holder.newSilentAnnotation(INFORMATION).
                range(element).
                textAttributes(key).
                create();
    }
}

@@ -380,16 +380,60 @@
 ntType;%0A
+import com.reason.lang.core.psi.PsiInclude;%0A
 import c
@@ -526,16 +526,57 @@
 Module;%0A
+import com.reason.lang.core.psi.PsiOpen;%0A
 import c
@@ -1384,24 +1384,77 @@
 R_SYMBOL) %7B%0A
+            PsiElement parent = element.getParent();%0A
             
@@ -1500,24 +1500,24 @@
 tSibling();%0A
-
             
@@ -1628,38 +1628,40 @@
         
-color(holder, element,
+boolean mightBeVariant =
  (nextEl
@@ -1662,33 +1662,33 @@
 nextElementType 
-=
+!
 = m_types.DOT) ?
@@ -1690,32 +1690,248 @@
 OT) 
-? MODULE_NAME_ : VARIANT
+&& !(parent instanceof PsiOpen) && !(parent instanceof PsiInclude) && !(%0A                    parent instanceof PsiModule && ((PsiModule) parent).getAlias() != null);%0A            color(holder, element, mightBeVariant ? VARIANT_NAME_ : MODULE
 _NAM

Remove deprecated annotation method