IJPL-53: Add ability to turn off Write Intent Lock wrapping.

Default calls still wrap to Write Intent Lock.

GitOrigin-RevId: 0b8575a59dae133a6c3c48432fc8f5cdc9bbb4f0
This commit is contained in:
Lev Serebryakov
2023-08-01 16:00:02 +02:00
committed by intellij-monorepo-bot
parent 617302d81a
commit b10864ca70
2 changed files with 75 additions and 16 deletions

View File

@@ -21,38 +21,79 @@ public final class EdtTestUtil {
@kotlin.Deprecated(message = "Use Kotlin runInEdtAndGet { ... } instead") // this warning is only visible in Kotlin files
@TestOnly
public static <V, T extends Throwable> V runInEdtAndGet(@NotNull ThrowableComputable<V, T> computable) throws T {
return runInEdtAndGet(computable, true);
}
@kotlin.Deprecated(message = "Use Kotlin runInEdtAndGet { ... } instead") // this warning is only visible in Kotlin files
@TestOnly
public static <V, T extends Throwable> V runInEdtAndGet(@NotNull ThrowableComputable<V, T> computable, boolean writeIntent) throws T {
final Ref<V> res = new Ref<>();
runInEdtAndWait(() -> {
res.set(computable.compute());
});
}, writeIntent);
return res.get();
}
@kotlin.Deprecated(message = "Use Kotlin runInEdtAndWait { ... } instead") // this warning is only visible in Kotlin files
@TestOnly
public static <T extends Throwable> void runInEdtAndWait(@NotNull ThrowableRunnable<T> runnable) throws T {
runInEdtAndWait(runnable, true);
}
@kotlin.Deprecated(message = "Use Kotlin runInEdtAndWait { ... } instead") // this warning is only visible in Kotlin files
@TestOnly
public static <T extends Throwable> void runInEdtAndWait(@NotNull ThrowableRunnable<T> runnable, boolean writeIntent) throws T {
Application app = ApplicationManager.getApplication();
if (app != null && app.isDispatchThread()) {
app.runWriteIntentReadAction(() -> { runnable.run(); return null; });
if (writeIntent) {
app.runWriteIntentReadAction(() -> {
runnable.run();
return null;
});
}
else {
runnable.run();
}
return;
}
else if (EDT.isCurrentThreadEdt()) {
setupEventQueue();
IdeEventQueue.getInstance().getRwLockHolder().runWriteIntentReadAction(() -> { runnable.run(); return null; });
if (writeIntent) {
setupEventQueue();
IdeEventQueue.getInstance().getRwLockHolder().runWriteIntentReadAction(() -> {
runnable.run();
return null;
});
}
else {
runnable.run();
}
return;
}
Ref<T> exception = new Ref<>();
Runnable r = () -> {
try {
setupEventQueue();
IdeEventQueue.getInstance().getRwLockHolder().runWriteIntentReadAction(() -> { runnable.run(); return null; });
}
catch (Throwable e) {
//noinspection unchecked
exception.set((T)e);
}
};
Runnable r = writeIntent ?
() -> {
try {
setupEventQueue();
IdeEventQueue.getInstance().getRwLockHolder().runWriteIntentReadAction(() -> {
runnable.run();
return null;
});
}
catch (Throwable e) {
//noinspection unchecked
exception.set((T)e);
}
} :
() -> {
try {
runnable.run();
}
catch (Throwable e) {
//noinspection unchecked
exception.set((T)e);
}
};
if (app != null) {
app.invokeAndWait(r);

View File

@@ -11,7 +11,16 @@ import org.jetbrains.annotations.TestOnly
@TestOnly
inline fun <V> runInEdtAndGet(crossinline compute: () -> V): V {
@Suppress("DEPRECATION", "RemoveExplicitTypeArguments")
return EdtTestUtil.runInEdtAndGet(ThrowableComputable<V, Throwable> { compute() })
return EdtTestUtil.runInEdtAndGet(ThrowableComputable<V, Throwable> { compute() }, true)
}
/**
* Consider using Kotlin coroutines and [Dispatchers.EDT][com.intellij.openapi.application.EDT].
*/
@TestOnly
inline fun <V> runInEdtAndGet(writeIntent: Boolean, crossinline compute: () -> V): V {
@Suppress("DEPRECATION", "RemoveExplicitTypeArguments")
return EdtTestUtil.runInEdtAndGet(ThrowableComputable<V, Throwable> { compute() }, writeIntent)
}
/**
@@ -20,5 +29,14 @@ inline fun <V> runInEdtAndGet(crossinline compute: () -> V): V {
@TestOnly
inline fun runInEdtAndWait(crossinline runnable: () -> Unit) {
@Suppress("DEPRECATION", "RemoveExplicitTypeArguments")
EdtTestUtil.runInEdtAndWait(ThrowableRunnable<Throwable> { runnable() })
EdtTestUtil.runInEdtAndWait(ThrowableRunnable<Throwable> { runnable() }, true)
}
/**
* Consider using Kotlin coroutines and [Dispatchers.EDT][com.intellij.openapi.application.EDT].
*/
@TestOnly
inline fun runInEdtAndWait(writeIntent: Boolean, crossinline runnable: () -> Unit) {
@Suppress("DEPRECATION", "RemoveExplicitTypeArguments")
EdtTestUtil.runInEdtAndWait(ThrowableRunnable<Throwable> { runnable() }, writeIntent)
}