package org.jetbrains.plugins.groovy.lang.completion;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.completion.actions.CodeCompletionAction;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import com.intellij.testFramework.fixtures.JavaTestFixtureFactory;
import junit.framework.Assert;
import org.jetbrains.plugins.groovy.CompositeCompletionData;
import org.jetbrains.plugins.groovy.testcases.action.ActionTestCase;
import org.jetbrains.plugins.groovy.util.TestUtils;

import java.io.IOException;
import java.util.Arrays;

/**
 * author ven
 */
public abstract class CompletionTestBase extends ActionTestCase {
  protected Editor myEditor;
  protected FileEditorManager myFileEditorManager;
  protected PsiFile myFile;

  public CompletionTestBase(String path) {
    super(path);
    JavaTestFixtureFactory.getFixtureFactory();
  }

  protected CodeInsightActionHandler getCompetionHandler() {
    return new CodeCompletionAction().getHandler();
  }

  protected String processFile(final PsiFile file) throws IncorrectOperationException, InvalidDataException, IOException {
    //  todo [DIANA] uncomment me!
    String fileText = file.getText();
    int offset = fileText.indexOf(CARET_MARKER);
    fileText = removeCaretMarker(fileText);
    myFile = createFile(fileText);
    assert myFile != null;
    myFileEditorManager = FileEditorManager.getInstance(myProject);
    VirtualFile virtualFile = myFile.getVirtualFile();
    assert virtualFile != null;
    myEditor = myFileEditorManager.openTextEditor(new OpenFileDescriptor(myProject, virtualFile, 0), false);
    Assert.assertNotNull(myEditor);
    myEditor.getCaretModel().moveToOffset(offset);
    CompositeCompletionData.restrictCompletion(addReferenceVariants(), addKeywords());

    boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = false;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;

    String result = "";
    try {
      performAction(myProject, new Runnable() {
        public void run() {
          new CodeCompletionAction().getHandler().invoke(myProject, myEditor, myFile);
        }
      });

      final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myEditor);
      if (lookup != null) {
        LookupElement[] items = lookup.getItems();
        Arrays.sort(items);
        result = "";
        for (LookupElement item : items) {
          result = result + "\n" + item.getLookupString();
        }
        result = result.trim();
        LookupManager.getInstance(myProject).hideActiveLookup();
      }

    }
    finally {
      CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = true;
      CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = old;
      myFileEditorManager.closeFile(virtualFile);
      myEditor = null;
    }
    return result;
  }

  protected PsiFile createFile(String fileText) throws IncorrectOperationException {
    return TestUtils.createPseudoPhysicalGroovyFile(myProject, fileText);
  }


  public String transform(String testName, String[] data) throws Exception {
    String fileText = data[0];
    final PsiFile psiFile = createFile(fileText);
    String result = processFile(psiFile);
    //System.out.println("------------------------ " + testName + " ------------------------");
    //System.out.println(result);
    //System.out.println("");
    return result;
  }

  protected boolean addKeywords() {
    return true;
  }

  protected boolean addReferenceVariants() {
    return true;
  }
}

@@ -713,40 +713,28 @@
 lij.
+psi.
 util.
-IncorrectOperationException
+PsiUtilBase
 ;%0Aim
@@ -798,16 +798,70 @@
 actory;%0A
+import com.intellij.util.IncorrectOperationException;%0A
 import j
@@ -2760,22 +2760,67 @@
 Editor, 
-myFile
+PsiUtilBase.getPsiFileInEditor(myEditor, myProject)
 );%0A     

Support JSP Tag in GSP; XML part