package org.intellij.lang.regexp;

import com.intellij.codeInsight.lookup.PresentableLookupValue;
import com.intellij.lang.ASTNode;
import com.intellij.lang.documentation.QuickDocumentationProvider;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.intellij.lang.regexp.psi.RegExpElement;
import org.intellij.lang.regexp.psi.RegExpGroup;
import org.intellij.lang.regexp.psi.RegExpProperty;
import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl;
import org.jetbrains.annotations.Nullable;

/**
 * Created by IntelliJ IDEA.
 * User: vnikolaenko
 * Date: 17.09.2008
 * Time: 19:24:29
 * To change this template use File | Settings | File Templates.
 */
public class RegExpDocumentationProvider extends QuickDocumentationProvider {
  @Override
  @Nullable
  public String generateDoc(PsiElement element, PsiElement originalElement) {
    if (element instanceof RegExpProperty) {
      final RegExpProperty prop = (RegExpProperty)element;
      final ASTNode node = prop.getCategoryNode();
      if (node != null) {
        final String elementName = node.getText();
        for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) {
          if (stringArray[0].equals(elementName)) {
            if (prop.isNegated()) {
              return "Property block stands for characters not matching " + stringArray[1];
            } else {
              return "Property block stands for " + "" + stringArray[1];
            }
          }
        }
      }
    }
    return null;
  }

  @Override
  public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) {
    if (element instanceof RegExpProperty) {
      final String s;
      if (object instanceof PresentableLookupValue) {
        s = ((PresentableLookupValue)object).getPresentation();
      } else if (object instanceof String) {
        s = (String)object;
      } else {
        return null;
      }
      final Project project = element.getProject();
      final JavaPsiFacade f = JavaPsiFacade.getInstance(project);
      if (s.startsWith("java")) {
        final PsiClass charClass = f.findClass("java.lang.Character", GlobalSearchScope.allScope(project));
        if (charClass != null) {
          final PsiMethod[] methods = charClass.findMethodsByName("is" + s.substring("java".length()), false);
          return methods.length > 0 ? methods[0] : null;
        }
      } else if (s.matches("In[\\p{Upper}_]+")) {
        final PsiClass charClass = f.findClass("java.lang.Character.UnicodeBlock", GlobalSearchScope.allScope(project));
        if (charClass != null) {
          return charClass.findFieldByName(s.substring(2), false);
        }
      } else {
        return f.findClass("java.util.regex.Pattern", GlobalSearchScope.allScope(project));
      }
    }
    return super.getDocumentationElementForLookupItem(psiManager, object, element);
  }

  @Nullable
  public String getQuickNavigateInfo(PsiElement element) {
    if (element instanceof RegExpGroup) {
      return "Capturing Group: " + ((RegExpElement)element).getUnescapedText();
    } else {
      return null;
    }
  }
}

@@ -32,71 +32,8 @@
 p;%0A%0A
-import com.intellij.codeInsight.lookup.PresentableLookupValue;%0A
 impo
@@ -153,58 +153,22 @@
 lij.
-openapi.project.Project;%0Aimport com.intellij.psi.*
+psi.PsiElement
 ;%0Aim
@@ -193,32 +193,18 @@
 psi.
-search.GlobalSearchScope
+PsiManager
 ;%0Aim
@@ -1571,32 +1571,39 @@
 ment element) %7B%0A
+    /*%0A
     if (element 
@@ -2753,32 +2753,41 @@
 ;%0A      %7D%0A    %7D%0A
+      */%0A
     return super

comment out Java-dependent code