From ceb2b9bf5343f5d5c190b45a4a74116a196841ac Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 31 May 2016 16:24:11 +0200 Subject: [PATCH] performLaterWhenAllCommitted should work from within commit handler --- .../psi/impl/PsiDocumentManagerBase.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java b/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java index 7d6673ece18f..2b64e76ba2ba 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java +++ b/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java @@ -533,30 +533,34 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen @Override public void performLaterWhenAllCommitted(@NotNull final Runnable runnable) { - final ModalityState modalityState = ModalityState.current(); - UIUtil.invokeLaterIfNeeded(new Runnable() { + final ModalityState modalityState = ModalityState.defaultModalityState(); + final Runnable whenAllCommitted = new Runnable() { @Override public void run() { - performWhenAllCommitted(new Runnable() { + ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - // later because we may end up in write action here if there was a synchronous commit - ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - if (hasUncommitedDocuments()) { - // no luck, will try later - performLaterWhenAllCommitted(runnable); - } - else { - runnable.run(); - } - } - }, modalityState, myProject.getDisposed()); + if (hasUncommitedDocuments()) { + // no luck, will try later + performLaterWhenAllCommitted(runnable); + } + else { + runnable.run(); + } } - }); + }, modalityState, myProject.getDisposed()); } - }); + }; + if (ApplicationManager.getApplication().isDispatchThread() && isInsideCommitHandler()) { + whenAllCommitted.run(); + } else { + UIUtil.invokeLaterIfNeeded(new Runnable() { + @Override + public void run() { + performWhenAllCommitted(whenAllCommitted); + } + }); + } } private static class CompositeRunnable extends ArrayList implements Runnable { @@ -586,7 +590,7 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen if (!hasUncommitedDocuments() && !actionsWhenAllDocumentsAreCommitted.isEmpty()) { List> entries = new ArrayList>(new LinkedHashMap(actionsWhenAllDocumentsAreCommitted).entrySet()); - weAreInsideAfterCommitHandler(); + beforeCommitHandler(); try { for (Map.Entry entry : entries) { @@ -605,15 +609,19 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen } } - private void weAreInsideAfterCommitHandler() { + private void beforeCommitHandler() { actionsWhenAllDocumentsAreCommitted.put(PERFORM_ALWAYS_KEY, EmptyRunnable.getInstance()); // to prevent listeners from registering new actions during firing } private void checkWeAreOutsideAfterCommitHandler() { - if (actionsWhenAllDocumentsAreCommitted.get(PERFORM_ALWAYS_KEY) == EmptyRunnable.getInstance()) { + if (isInsideCommitHandler()) { throw new IncorrectOperationException("You must not call performWhenAllCommitted()/cancelAndRunWhenCommitted() from within after-commit handler"); } } + private boolean isInsideCommitHandler() { + return actionsWhenAllDocumentsAreCommitted.get(PERFORM_ALWAYS_KEY) == EmptyRunnable.getInstance(); + } + @Override public void addListener(@NotNull Listener listener) { myListeners.add(listener);