package com.intellij.codeInsight.actions;

import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import org.jetbrains.annotations.Nullable;

public abstract class BaseCodeInsightAction extends CodeInsightAction {
  private final boolean myLookForInjectedEditor;

  protected BaseCodeInsightAction() {
    this(true);
  }

  protected BaseCodeInsightAction(boolean lookForInjectedEditor) {
    myLookForInjectedEditor = lookForInjectedEditor;
  }

  @Nullable
  protected Editor getEditor(final DataContext dataContext, final Project project) {
    Editor editor = getBaseEditor(dataContext, project);
    if (!myLookForInjectedEditor) return editor;
    Editor injectedEditor = editor;
    if (editor != null) {
      PsiFile psiFile = PsiDocumentManager.getInstance(project).getCachedPsiFile(editor.getDocument());
      injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguage(editor, psiFile);
    }
    return injectedEditor;
  }

  @Nullable
  protected Editor getBaseEditor(final DataContext dataContext, final Project project) {
    return super.getEditor(dataContext, project);
  }

  public boolean isValidForLookup(Lookup lookup, DataContext context) {
    return isValidForLookup();
  }

  public void update(AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    DataContext dataContext = event.getDataContext();
    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    if (project == null){
      presentation.setEnabled(false);
      return;
    }

    final Lookup activeLookup = LookupManager.getInstance(project).getActiveLookup();
    if (activeLookup != null){
      if (!isValidForLookup(activeLookup, dataContext)){
        presentation.setEnabled(false);
      }
      else{
        presentation.setEnabled(true);
      }
    } else {
      super.update(event);
    }
  }

  protected boolean isValidForLookup() {
    return false;
  }
}

@@ -1226,20 +1226,39 @@
  Psi
-File psiFile
+DocumentManager documentManager
  = P
@@ -1291,24 +1291,65 @@
 nce(project)
+;%0A      PsiFile psiFile = documentManager
 .getCachedPs
@@ -1377,16 +1377,93 @@
 ent());%0A
+      if (psiFile != null) %7B%0A        documentManager.commitAllDocuments();%0A  
       in
@@ -1541,24 +1541,32 @@
 , psiFile);%0A
+      %7D%0A
     %7D%0A    re
@@ -1751,116 +1751,8 @@
  %7D%0A%0A
-  public boolean isValidForLookup(Lookup lookup, DataContext context) %7B%0A    return isValidForLookup();%0A  %7D%0A%0A
   pu
@@ -2183,160 +2183,53 @@
     
-if (!isValidForLookup(activeLookup, dataContext))%7B%0A        presentation.setEnabled(false);%0A      %7D%0A      else%7B%0A        presentation.setEnabled(true
+presentation.setEnabled(isValidForLookup()
 );%0A
-  
     
@@ -2233,18 +2233,16 @@
    %7D%0A   
- %7D
  else %7B%0A

fix test hopefully