use async document commit in file opening startup activity
authorpeter <peter@jetbrains.com>
Thu, 21 Apr 2016 16:58:11 +0000 (18:58 +0200)
committerpeter <peter@jetbrains.com>
Thu, 21 Apr 2016 17:31:26 +0000 (19:31 +0200)
platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java

index 119f81b5f0e25f0334f01e6e5f631c36ad145f25..54b0ef5dd6c429e6caa1422a3c3d01aba036f273 100644 (file)
@@ -63,6 +63,7 @@ import com.intellij.openapi.wm.ToolWindowManager;
 import com.intellij.openapi.wm.WindowManager;
 import com.intellij.openapi.wm.ex.StatusBarEx;
 import com.intellij.openapi.wm.impl.IdeFrameImpl;
+import com.intellij.psi.PsiDocumentManager;
 import com.intellij.reference.SoftReference;
 import com.intellij.ui.FocusTrackback;
 import com.intellij.ui.docking.DockContainer;
@@ -71,6 +72,7 @@ import com.intellij.ui.docking.impl.DockManagerImpl;
 import com.intellij.ui.tabs.impl.JBTabsImpl;
 import com.intellij.util.Function;
 import com.intellij.util.SmartList;
+import com.intellij.util.concurrency.Semaphore;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.messages.MessageBusConnection;
 import com.intellij.util.messages.impl.MessageListenerList;
@@ -873,7 +875,7 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Persis
       newProviders = null;
       builders = null;
     }
-    UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+    Runnable runnable = new Runnable() {
       @Override
       public void run() {
         if (myProject.isDisposed() || !file.isValid()) {
@@ -991,7 +993,25 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Persis
           window.setFilePinned(file, pin);
         }
       }
-    });
+    };
+
+    if (ApplicationManager.getApplication().isDispatchThread()) {
+      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
+      runnable.run();
+    } else {
+      Semaphore semaphore = new Semaphore();
+      semaphore.down();
+      PsiDocumentManager.getInstance(myProject).performLaterWhenAllCommitted(() -> {
+        try {
+          runnable.run();
+        } finally {
+          semaphore.up();
+        }
+      });
+      //noinspection StatementWithEmptyBody
+      while (!semaphore.waitFor(10) && !myProject.isDisposed());
+    }
+
     EditorWithProviderComposite composite = compositeRef.get();
     return Pair.create(composite == null ? EMPTY_EDITOR_ARRAY : composite.getEditors(),
                        composite == null ? EMPTY_PROVIDER_ARRAY : composite.getProviders());