From 6a59fbc5ecc6a53a5b4c256da0a712e4b0bd5de0 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 28 Mar 2014 11:06:10 +0100 Subject: [PATCH] continue: local variables defined in the console should be available for resolve/completion/variables view --- .../openapi/util/AsyncValueLoader.java | 41 +++++++++++++++---- .../openapi/util/AsyncValueLoaderManager.java | 4 +- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoader.java b/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoader.java index ea25330dd592..f3b13dc6963c 100644 --- a/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoader.java +++ b/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoader.java @@ -9,6 +9,16 @@ import java.util.concurrent.atomic.AtomicReference; public abstract class AsyncValueLoader { private final AtomicReference> ref = new AtomicReference>(); + private volatile long modificationCount; + private volatile long loadedModificationCount; + + private final Runnable doneHandler = new Runnable() { + @Override + public void run() { + loadedModificationCount = modificationCount; + } + }; + @NotNull public final AsyncResult get() { return get(true); @@ -59,7 +69,7 @@ public abstract class AsyncValueLoader { return asyncResult; } else if (asyncResult.isDone()) { - if (!checkFreshness || checkFreshness(asyncResult.getResult())) { + if (!checkFreshness || isUpToDate(asyncResult.getResult())) { return asyncResult; } @@ -90,22 +100,24 @@ public abstract class AsyncValueLoader { return false; } - private void callLoad(final AsyncResult asyncResult) { + private void callLoad(final @NotNull AsyncResult result) { if (isCancelOnReject()) { - asyncResult.doWhenRejected(new Runnable() { + result.doWhenRejected(new Runnable() { @Override public void run() { - ref.compareAndSet(asyncResult, null); + ref.compareAndSet(result, null); } }); } + result.doWhenDone(doneHandler); + try { - load(asyncResult); + load(result); } catch (Throwable e) { - ref.compareAndSet(asyncResult, null); - rejectAndDispose(asyncResult); + ref.compareAndSet(result, null); + rejectAndDispose(result); //noinspection InstanceofCatchParameter throw e instanceof RuntimeException ? ((RuntimeException)e) : new RuntimeException(e); } @@ -113,7 +125,18 @@ public abstract class AsyncValueLoader { protected abstract void load(@NotNull AsyncResult result) throws IOException; - protected boolean checkFreshness(@NotNull T result) { - return true; + protected boolean isUpToDate(@NotNull T result) { + return loadedModificationCount == modificationCount; + } + + public final void set(@NotNull T result) { + AsyncResult oldValue = ref.getAndSet(AsyncResult.done(result)); + if (oldValue != null) { + rejectAndDispose(oldValue); + } + } + + public final void markDirty() { + modificationCount++; } } \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoaderManager.java b/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoaderManager.java index c760e0b2e3b2..e8ef64bb1108 100644 --- a/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoaderManager.java +++ b/platform/core-api/src/com/intellij/openapi/util/AsyncValueLoaderManager.java @@ -12,7 +12,7 @@ public abstract class AsyncValueLoaderManager { this.fieldUpdater = fieldUpdater; } - public boolean checkFreshness(@NotNull HOST host, @NotNull VALUE value) { + public boolean isUpToDate(@NotNull HOST host, @NotNull VALUE value) { return true; } @@ -57,7 +57,7 @@ public abstract class AsyncValueLoaderManager { return asyncResult; } else if (asyncResult.isDone()) { - if (!checkFreshness || checkFreshness(host, asyncResult.getResult())) { + if (!checkFreshness || isUpToDate(host, asyncResult.getResult())) { return asyncResult; }