package org.jetbrains.plugins.groovy.annotator;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.psi.PsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.bodies.GrClassBody;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrReferenceElement;

/**
 * @author ven
 */
public class GroovyAnnotator implements Annotator {
  private GroovyAnnotator() {}

  public static final GroovyAnnotator INSTANCE = new GroovyAnnotator();

  public void annotate(PsiElement element, AnnotationHolder holder) {
    if (element instanceof GrReferenceElement) {
      checkReferenceElement(element, holder, (GrReferenceElement) element);
    } else if (element instanceof GrTypeDefinition) {
      checkTypeDefinition(holder, (GrTypeDefinition) element);
    }
  }

  private void checkTypeDefinition(AnnotationHolder holder, GrTypeDefinition typeDefinition) {
    if (typeDefinition.getParent() instanceof GrClassBody) {
      holder.createErrorAnnotation(typeDefinition.getNameIdentifier(), "Inner classes are not supported in Groovy");
    }
  }

  private void checkReferenceElement(PsiElement element, AnnotationHolder holder, GrReferenceElement refElement) {
    if (!refElement.isSoft() && refElement.getReferenceName() != null) {
        PsiElement resolved = refElement.resolve();
        if (resolved == null) {
          String message = "Cannot resolve symbol " + refElement.getReferenceName();
          holder.createErrorAnnotation(element, message);
        }
      }
  }
}


@@ -167,22 +167,127 @@
 lij.
-psi.PsiElement
+lang.annotation.Annotation;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.codeInspection.ProblemHighlightType
 ;%0Aim
@@ -1683,24 +1683,48 @@
 );%0A         
+ Annotation annotation =
  holder.crea
@@ -1760,16 +1760,97 @@
 ssage);%0A
+          annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);%0A
         

set error style