/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInsight.editorActions;

import com.intellij.codeInsight.AutoPopupController;
import com.intellij.codeInsight.completion.CodeCompletionHandlerBase;
import com.intellij.codeInsight.completion.CompletionPhase;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiPlainText;
import com.intellij.psi.PsiPlainTextFile;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author peter
 */
public class CompletionAutoPopupHandler extends TypedHandlerDelegate {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler");
  public static volatile boolean ourTestingAutopopup = false;

  @Override
  public Result checkAutoPopup(char charTyped, final Project project, final Editor editor, final PsiFile file) {
    LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);

    if (LOG.isDebugEnabled()) {
      LOG.debug("checkAutoPopup: character=" + charTyped + ";");
      LOG.debug("phase=" + CompletionServiceImpl.getCompletionPhase());
      LOG.debug("lookup=" + lookup);
      LOG.debug("currentCompletion=" + CompletionServiceImpl.getCompletionService().getCurrentCompletion());
    }

    if (lookup != null) {
      if (editor.getSelectionModel().hasSelection()) {
        lookup.performGuardedChange(new Runnable() {
          @Override
          public void run() {
            EditorModificationUtil.deleteSelectedText(editor);
          }
        });
      }
      return Result.STOP;
    }

    if ((Character.isLetter(charTyped) || charTyped == '_') &&
        !(file instanceof PsiPlainTextFile) // todo [maxim, peter] why we start autopopup when editing text files ? it cancels find preview
      ) {
      AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
      return Result.STOP;
    }

    return Result.CONTINUE;
  }

  public static void invokeCompletion(@NotNull CompletionType completionType,
                                      boolean autopopup,
                                      Project project, Editor editor, int time, boolean restart) {
    if (editor.isDisposed()) {
      CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion);
      return;
    }

    // retrieve the injected file from scratch since our typing might have destroyed the old one completely
    Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(editor);
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(topLevelEditor.getDocument());
    if (file == null) {
      CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion);
      return;
    }

    PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    if (!PsiDocumentManager.getInstance(project).isCommitted(editor.getDocument())) {
      LOG.error("Non-committed document");
      PsiDocumentManager.getInstance(project).commitAllDocuments();
    }
    Editor newEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(topLevelEditor, topLevelFile);
    try {
      new CodeCompletionHandlerBase(completionType, false, autopopup, false).invokeCompletion(project, newEditor, time, false, restart);
    }
    catch (IndexNotReadyException ignored) {
    }
  }

  public static void runLaterWithCommitted(@NotNull final Project project,
                                           @NotNull final Document document,
                                           @NotNull final Runnable runnable) {
    final long beforeStamp = document.getModificationStamp();
    PsiDocumentManager.getInstance(project).performWhenAllCommitted(new Runnable() {
      @Override
      public void run() {
        // later because we may end up in write action here if there was a synchronous commit
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          @Override
          public void run() {
            if (beforeStamp != document.getModificationStamp()) {
              // no luck, will try later
              runLaterWithCommitted(project, document, runnable);
            }
            else {
              runnable.run();
            }
          }
        }, project.getDisposed());
      }
    });
  }
}

@@ -1566,88 +1566,8 @@
 le;%0A
-import com.intellij.psi.PsiPlainText;%0Aimport com.intellij.psi.PsiPlainTextFile;%0A
 impo
@@ -2804,17 +2804,16 @@
 %0A    if 
-(
 (Charact
@@ -2859,159 +2859,8 @@
 '_')
- &&%0A        !(file instanceof PsiPlainTextFile) // todo %5Bmaxim, peter%5D why we start autopopup when editing text files ? it cancels find preview%0A      )
  %7B%0A 

revert my previous temp fix 0c90cc01992c2c3a19391c5199a9f85eeae7dac2