Fixed ProgressIndicatorUtils.kt for 201

#KT-36008 Fixed
Relates to #KT-33939
This commit is contained in:
Vladimir Dolzhenko
2020-01-20 15:01:40 +01:00
parent 5cb7cf040a
commit 1911cbb077
@@ -5,10 +5,46 @@
package org.jetbrains.kotlin.idea.util
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.util.ExceptionUtil
import java.util.concurrent.CancellationException
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
/**
* Copied from [com.intellij.openapi.progress.util.ProgressIndicatorUtils]
*/
object ProgressIndicatorUtils {
@JvmStatic
fun <T> awaitWithCheckCanceled(future: Future<T>) =
com.intellij.openapi.progress.util.ProgressIndicatorUtils.awaitWithCheckCanceled(future)
fun <T> awaitWithCheckCanceled(future: Future<T>) {
val indicator =
ProgressManager.getInstance().progressIndicator
while (true) {
checkCancelledEvenWithPCEDisabled(indicator)
try {
future[10, TimeUnit.MILLISECONDS]
return
} catch (ignore: TimeoutException) {
} catch (e: Throwable) {
val cause = e.cause
if (cause is CancellationException) {
throw ProcessCanceledException(cause)
} else {
ExceptionUtil.rethrowUnchecked(e)
throw RuntimeException(e)
}
}
}
}
private fun checkCancelledEvenWithPCEDisabled(indicator: ProgressIndicator?) =
indicator?.let {
if (it.isCanceled) {
it.checkCanceled() // maybe it'll throw with some useful additional information
throw ProcessCanceledException()
}
}
}