diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/util/CoroutineUtils.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/util/CoroutineUtils.kt index 0051a2ff8bc..44d485f86fc 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/util/CoroutineUtils.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/util/CoroutineUtils.kt @@ -16,9 +16,14 @@ package org.jetbrains.kotlin.idea.core.util +import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.components.service +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Disposer import kotlinx.coroutines.experimental.CoroutineDispatcher +import kotlinx.coroutines.experimental.Job import kotlin.coroutines.experimental.AbstractCoroutineContextElement import kotlin.coroutines.experimental.CoroutineContext @@ -36,4 +41,20 @@ public object EDT : CoroutineDispatcher() { class ModalityStateElement(val modalityState: ModalityState) : AbstractCoroutineContextElement(Key) { companion object Key : CoroutineContext.Key } + + operator fun invoke(project: Project) = this + project.cancelOnDisposal } + +// job that is cancelled when the project is disposed +val Project.cancelOnDisposal: Job + get() = service().sharedJob + +internal class ProjectJob(project: Project) { + internal val sharedJob: Job = Job() + + init { + Disposer.register(project, Disposable { + sharedJob.cancel() + }) + } +} \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index aa743022a9e..680891dbefb 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -367,6 +367,9 @@ + +