package org.asciidoc.intellij.editor;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorProvider;
import com.intellij.openapi.util.Disposer;
import org.asciidoc.intellij.ui.SplitTextEditorProvider;
import org.jetbrains.annotations.NotNull;

public class AsciiDocSplitEditorProvider extends SplitTextEditorProvider {
  public AsciiDocSplitEditorProvider() {
    super(new PsiAwareTextEditorProvider(), new AsciiDocPreviewEditorProvider());
  }

  @Override
  protected FileEditor createSplitEditor(@NotNull final FileEditor firstEditor, @NotNull FileEditor secondEditor) {
    if (!(firstEditor instanceof TextEditor) || !(secondEditor instanceof AsciiDocPreviewEditor)) {
      throw new IllegalArgumentException("Main editor should be TextEditor");
    }
    AsciiDocPreviewEditor asciiDocPreviewEditor = (AsciiDocPreviewEditor) secondEditor;
    asciiDocPreviewEditor.setEditor(((TextEditor) firstEditor).getEditor());
    return new AsciiDocSplitEditor(((TextEditor) firstEditor), ((AsciiDocPreviewEditor) secondEditor));
  }

  @Override
  public void disposeEditor(@NotNull FileEditor fileEditor) {
    // default -- needed for IntelliJ IDEA 15 compatibility
    Disposer.dispose(fileEditor);
  }

}

@@ -32,16 +32,76 @@
 ditor;%0A%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -135,32 +135,90 @@
 tor.FileEditor;%0A
+import com.intellij.openapi.fileEditor.FileEditorManager;%0A
 import com.intel
@@ -361,21 +361,279 @@
 api.
-util.Disposer
+project.Project;%0Aimport com.intellij.openapi.project.ProjectManager;%0Aimport com.intellij.openapi.util.Disposer;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiManager;%0Aimport org.asciidoc.intellij.AsciiDocLanguage
 ;%0Aim
@@ -924,24 +924,1437 @@
 rovider());%0A
+    // when this plugin is installed at runtime, check if the existing editors as split editors.%0A    // If not, close the editor and re-open the files%0A    // on startup, the list of files is always empty (it is still being restored)%0A    ApplicationManager.getApplication().invokeLater(() -%3E %7B%0A      // run later, to avoid a cyclic dependency plugin is dynamically loaded%0A      for (Project project : ProjectManager.getInstance().getOpenProjects()) %7B%0A        FileEditorManager fem = FileEditorManager.getInstance(project);%0A        PsiManager pm = PsiManager.getInstance(project);%0A        for (FileEditor editor : fem.getAllEditors()) %7B%0A          if (!(editor instanceof AsciiDocSplitEditor)) %7B%0A            VirtualFile vFile = editor.getFile();%0A            if (vFile != null) %7B%0A              PsiFile pFile = pm.findFile(vFile);%0A              if (pFile != null) %7B%0A                if (pFile.getLanguage() == AsciiDocLanguage.INSTANCE) %7B%0A                  // an AsciiDoc file in a non-split editor, close and re-open the file to enforce split editor%0A                  ApplicationManager.getApplication().runWriteAction(() -%3E %7B%0A                    // closing the file might trigger a save, therefore wrap in write action%0A                    fem.closeFile(vFile);%0A                    fem.openFile(vFile, false);%0A                  %7D);%0A                %7D%0A              %7D%0A            %7D%0A          %7D%0A        %7D%0A      %7D%0A    %7D);%0A
   %7D%0A%0A  @Over

enforce re-opening files to show user the split editor after plugin's installation (#391)