package com.intellij.structuralsearch;

import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiCodeFragment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.GroovyFileType;
import org.jetbrains.plugins.groovy.debugger.fragments.GroovyCodeFragment;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.template.GroovyTemplateContextType;

/**
 * @author Eugene.Kudelevsky
 */
public class GroovyStructuralSearchProfile extends StructuralSearchProfileBase {
  public static final String FILE_CONTEXT = "File";
  public static final String CLASS_CONTEXT = "Class";

  private static final TokenSet VARIABLE_DELIMETERS = TokenSet.create(GroovyTokenTypes.mCOMMA, GroovyTokenTypes.mSEMI);

  @NotNull
  @Override
  protected String[] getVarPrefixes() {
    return new String[]{"_$_____"};
  }

  @NotNull
  @Override
  public String[] getContextNames() {
    return new String[]{FILE_CONTEXT, CLASS_CONTEXT};
  }

  @NotNull
  @Override
  protected LanguageFileType getFileType() {
    return GroovyFileType.GROOVY_FILE_TYPE;
  }

  @NotNull
  @Override
  protected TokenSet getVariableDelimiters() {
    return VARIABLE_DELIMETERS;
  }

  @Override
  protected PsiCodeFragment createCodeFragment(Project project, String text, @Nullable PsiElement context) {
    GroovyCodeFragment result = new GroovyCodeFragment(project, text);
    result.setContext(context);
    return result;
  }

  @Override
  public Class<? extends TemplateContextType> getTemplateContextTypeClass() {
    return GroovyTemplateContextType.class;
  }

  @Override
  public String getContext(@NotNull String pattern, @Nullable Language language, String contextName) {
    return CLASS_CONTEXT.equals(contextName)
           ? "class AAAAA { " + PATTERN_PLACEHOLDER + " }"
           : PATTERN_PLACEHOLDER;
  }
}

@@ -259,59 +259,9 @@
 psi.
-PsiCodeFragment;%0Aimport com.intellij.psi.PsiElement
+*
 ;%0Aim
@@ -2084,11 +2084,299 @@
 DER;%0A  %7D
+%0A%0A  @Override%0A  public Class getElementContextByPsi(PsiElement element) %7B%0A    if (element instanceof PsiIdentifier) %7B%0A      element = element.getParent();%0A    %7D%0A%0A    if (element instanceof PsiMember) %7B%0A      return PsiMember.class;%0A    %7D else %7B%0A      return PsiExpression.class;%0A    %7D%0A  %7D
 %0A%7D%0A

fix GroovyStructuralSearchTest