package org.asciidoc.intellij.actions.editorAction;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorTextInsertHandler;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.Producer;
import org.asciidoc.intellij.AsciiDocLanguage;
import org.asciidoc.intellij.actions.asciidoc.PasteImageAction;
import org.asciidoc.intellij.file.AsciiDocFileType;
import org.asciidoc.intellij.psi.AsciiDocUtil;
import org.jetbrains.annotations.NotNull;

import java.awt.datatransfer.Transferable;

/**
 * Page an image from the clipboard when in editor.
 * Inspired by {@link com.intellij.codeInsight.editorActions.PasteHandler} and
 * https://plugins.jetbrains.com/plugin/8446-paste-images-into-markdown who showed me how to used this API.
 */
public class AsciiDocPasteImageHandler extends EditorActionHandler implements EditorTextInsertHandler {

  private final EditorActionHandler myOriginalHandler;

  public AsciiDocPasteImageHandler(EditorActionHandler originalAction) {
    myOriginalHandler = originalAction;
  }

  @Override
  public void doExecute(@NotNull final Editor editor, Caret caret, final DataContext dataContext) {
    assert caret == null : "Invocation of 'paste' operation for specific caret is not supported";
    execute(editor, dataContext, null);
  }

  @Override
  public void execute(Editor editor, DataContext dataContext, Producer<Transferable> producer) {
    if (editor instanceof EditorEx && editor.getProject() != null) {
      VirtualFile virtualFile = ((EditorEx) editor).getVirtualFile();
      PsiFile file = PsiManager.getInstance(editor.getProject()).findFile(virtualFile);
      if (file != null) {
        PsiElement element = AsciiDocUtil.getStatementAtCaret(editor, file);
        // handle both language injection and start of empty file
        if ((element != null && AsciiDocLanguage.INSTANCE.equals(element.getLanguage())) ||
          AsciiDocFileType.INSTANCE.equals(virtualFile.getFileType())) {
          if (PasteImageAction.imageAvailable()) {
            PasteImageAction action = new PasteImageAction();
            AnActionEvent event = createAnEvent(action, dataContext);
            action.actionPerformed(event);
            return;
          }
        }
      }
    }

    if (myOriginalHandler != null) {
      myOriginalHandler.execute(editor, null, dataContext);
    }
  }

  private AnActionEvent createAnEvent(AnAction action, @NotNull DataContext context) {
    Presentation presentation = action.getTemplatePresentation().clone();
    return new AnActionEvent(null, context, ActionPlaces.UNKNOWN, presentation, ActionManager.getInstance(), 0);
  }
}

@@ -2155,24 +2155,59 @@
 tualFile();%0A
+      if (virtualFile != null) %7B%0A  
       PsiFil
@@ -2284,24 +2284,26 @@
 ile);%0A      
+  
 if (file != 
@@ -2302,32 +2302,34 @@
 file != null) %7B%0A
+  
         PsiEleme
@@ -2397,16 +2397,18 @@
         
+ 
+ 
 // handl
@@ -2457,16 +2457,18 @@
 ty file%0A
+  
         
@@ -2561,16 +2561,18 @@
         
+ 
+ 
 AsciiDoc
@@ -2632,24 +2632,26 @@
 %7B%0A          
+  
 if (PasteIma
@@ -2691,16 +2691,18 @@
         
+ 
+ 
 PasteIma
@@ -2739,24 +2739,26 @@
 geAction();%0A
+  
             
@@ -2827,16 +2827,18 @@
         
+ 
+ 
 action.a
@@ -2876,16 +2876,18 @@
     
+  
 return;%0A
     
@@ -2882,16 +2882,30 @@
 return;%0A
+            %7D%0A
         

fix NPE when pasting text for example into commit messages (#402)