diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/AsyncEditorLoader.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/AsyncEditorLoader.java index e73654371d84..bd37149f9b69 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/AsyncEditorLoader.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/AsyncEditorLoader.java @@ -17,7 +17,6 @@ import com.intellij.ui.EditorNotifications; import com.intellij.util.concurrency.AppExecutorUtil; import com.intellij.util.concurrency.Semaphore; import org.jetbrains.annotations.NotNull; -import org.jetbrains.concurrency.CancellablePromise; import java.util.ArrayList; import java.util.List; @@ -53,7 +52,7 @@ public class AsyncEditorLoader { void start() { ApplicationManager.getApplication().assertIsDispatchThread(); - CancellablePromise asyncLoading = scheduleLoading(); + Future asyncLoading = scheduleLoading(); boolean showProgress = true; if (worthWaiting()) { @@ -76,18 +75,25 @@ public class AsyncEditorLoader { } } - private CancellablePromise scheduleLoading() { + private Future scheduleLoading() { long commitDeadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(DOCUMENT_COMMIT_WAITING_TIME_MS); - return ReadAction + // we can't return the result of "nonBlocking" call below because it's only finished on EDT later, + // but we need to get the result of bg calculation in the same EDT event, if it's quick + CompletableFuture future = new CompletableFuture<>(); + + ReadAction .nonBlocking(() -> { waitForCommit(commitDeadline); - return myTextEditor.loadEditorInBackground(); + Runnable runnable = myTextEditor.loadEditorInBackground(); + future.complete(runnable); + return runnable; }) .expireWith(myEditorComponent) .expireWith(myProject) .finishOnUiThread(ModalityState.any(), result -> loadingFinished(result)) .submit(ourExecutor); + return future; } private void waitForCommit(long commitDeadlineNs) {