diff --git a/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java b/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java index 143ffcb063c5..a4289b1a1c0d 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java @@ -31,6 +31,7 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.ex.ActionManagerEx; import com.intellij.openapi.actionSystem.ex.AnActionListener; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.DumbService; @@ -123,19 +124,16 @@ public class AutoPopupController implements Disposable { CompletionServiceImpl.setCompletionPhase(phase); phase.ignoreCurrentDocumentChange(); - CompletionAutoPopupHandler.runLaterWithCommitted(myProject, editor.getDocument(), new Runnable() { - @Override - public void run() { - if (phase.checkExpired()) return; + runLaterWithEverythingCommitted(myProject, () -> { + if (phase.checkExpired()) return; - PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument()); - if (file != null && condition != null && !condition.value(file)) { - CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion); - return; - } - - CompletionAutoPopupHandler.invokeCompletion(CompletionType.BASIC, true, myProject, editor, 0, false); + PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument()); + if (file != null && condition != null && !condition.value(file)) { + CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion); + return; } + + CompletionAutoPopupHandler.invokeCompletion(CompletionType.BASIC, true, myProject, editor, 0, false); }); } @@ -201,4 +199,23 @@ public class AutoPopupController implements Disposable { @Override public void dispose() { } + + public static void runLaterWithEverythingCommitted(@NotNull final Project project, + @NotNull final Runnable runnable) { + ModalityState modalityState = ModalityState.current(); + final PsiDocumentManager pdm = PsiDocumentManager.getInstance(project); + pdm.performWhenAllCommitted(() -> { + // later because we may end up in write action here if there was a synchronous commit + ApplicationManager.getApplication().invokeLater(() -> { + if (pdm.hasUncommitedDocuments()) { + // no luck, will try later + runLaterWithEverythingCommitted(project, runnable); + } + else { + runnable.run(); + } + }, modalityState, project.getDisposed()); + + }); + } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java index fe2f2f248875..4967228a9617 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java @@ -16,10 +16,10 @@ package com.intellij.codeInsight.completion; +import com.intellij.codeInsight.AutoPopupController; import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.CodeInsightUtilBase; import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; -import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler; import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor; import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessors; import com.intellij.codeInsight.lookup.*; @@ -485,15 +485,12 @@ public class CodeCompletionHandlerBase { final CompletionPhase.CommittingDocuments phase = (CompletionPhase.CommittingDocuments)CompletionServiceImpl.getCompletionPhase(); - CompletionAutoPopupHandler.runLaterWithCommitted(project, copyDocument, new Runnable() { - @Override - public void run() { - if (phase.checkExpired()) { - Disposer.dispose(translator); - return; - } - doComplete(initContext, hasModifiers, invocationCount, hostCopy, hostMap, translator); + AutoPopupController.runLaterWithEverythingCommitted(project, () -> { + if (phase.checkExpired()) { + Disposer.dispose(translator); + return; } + doComplete(initContext, hasModifiers, invocationCount, hostCopy, hostMap, translator); }); } else { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java index 613f15b5df16..b0b6e7c007ef 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.completion; +import com.intellij.codeInsight.AutoPopupController; import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.TargetElementUtil; import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; @@ -694,21 +695,13 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement phase.ignoreCurrentDocumentChange(); final Project project = getProject(); - ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - CompletionAutoPopupHandler.runLaterWithCommitted(project, myEditor.getDocument(), new Runnable() { - @Override - public void run() { - if (phase.checkExpired()) return; + AutoPopupController.runLaterWithEverythingCommitted(project, () -> { + if (phase.checkExpired()) return; - CompletionAutoPopupHandler.invokeCompletion(myParameters.getCompletionType(), - isAutopopupCompletion(), project, myEditor, myParameters.getInvocationCount(), - true); - } - }); - } - }, project.getDisposed()); + CompletionAutoPopupHandler.invokeCompletion(myParameters.getCompletionType(), + isAutopopupCompletion(), project, myEditor, myParameters.getInvocationCount(), + true); + }); } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java index 8010e76335bb..ec4c378ea1f6 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java @@ -23,7 +23,6 @@ import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInsight.lookup.impl.LookupImpl; import com.intellij.lang.injection.InjectedLanguageManager; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; @@ -102,27 +101,13 @@ public class CompletionAutoPopupHandler extends TypedHandlerDelegate { } } - public static void runLaterWithCommitted(@NotNull final Project project, - @NotNull final Document document, - @NotNull final Runnable runnable) { - final long beforeStamp = document.getModificationStamp(); - PsiDocumentManager.getInstance(project).performWhenAllCommitted(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 (beforeStamp != document.getModificationStamp()) { - // no luck, will try later - runLaterWithCommitted(project, document, runnable); - } - else { - runnable.run(); - } - } - }, project.getDisposed()); - } - }); + /** + * @deprecated + * @see AutoPopupController#runLaterWithEverythingCommitted(Project, Runnable) + */ + @SuppressWarnings("unused") + @Deprecated + public static void runLaterWithCommitted(@NotNull final Project project, final Document document, @NotNull final Runnable runnable) { + AutoPopupController.runLaterWithEverythingCommitted(project, runnable); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java index c76b1b594f7d..9b296285f51e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.hint; +import com.intellij.codeInsight.AutoPopupController; import com.intellij.codeInsight.lookup.Lookup; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.ide.IdeTooltip; @@ -44,7 +45,7 @@ import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import javax.swing.JRootPane; +import javax.swing.*; import java.awt.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -253,17 +254,11 @@ public class ParameterInfoController implements Disposable { } private void addAlarmRequest(){ - Runnable request = new Runnable(){ - @Override - public void run(){ - if (!myDisposed && !myProject.isDisposed()) { - DumbService.getInstance(myProject).withAlternativeResolveEnabled(new Runnable() { - @Override - public void run() { - updateComponent(); - } - }); - } + Runnable request = () -> { + if (!myDisposed && !myProject.isDisposed()) { + AutoPopupController.runLaterWithEverythingCommitted(myProject, () -> + DumbService.getInstance(myProject).withAlternativeResolveEnabled(this::updateComponent) + ); } }; myAlarm.addRequest(request, DELAY, ModalityState.stateForComponent(myEditor.getComponent())); @@ -275,8 +270,6 @@ public class ParameterInfoController implements Disposable { return; } - PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - final PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument()); CharSequence chars = myEditor.getDocument().getCharsSequence(); final int offset = CharArrayUtil.shiftBackward(chars, myEditor.getCaretModel().getOffset() - 1, " \t") + 1; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hint/ShowParameterInfoContext.java b/platform/lang-impl/src/com/intellij/codeInsight/hint/ShowParameterInfoContext.java index e8ab98dabda9..265b6e436e83 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hint/ShowParameterInfoContext.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hint/ShowParameterInfoContext.java @@ -15,11 +15,11 @@ */ package com.intellij.codeInsight.hint; +import com.intellij.codeInsight.AutoPopupController; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.injected.editor.EditorWindow; import com.intellij.lang.parameterInfo.CreateParameterInfoContext; import com.intellij.lang.parameterInfo.ParameterInfoHandler; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.LogicalPosition; @@ -135,23 +135,20 @@ public class ShowParameterInfoContext implements CreateParameterInfoContext { final ShowParameterInfoHandler.BestLocationPointProvider provider = new MyBestLocationPointProvider(editor); final Pair pos = provider.getBestPointPosition(hint, element, elementStart, true, HintManager.UNDER); - ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - if (editor.isDisposed() || DumbService.isDumb(project)) return; + AutoPopupController.runLaterWithEverythingCommitted(project, () -> { + if (editor.isDisposed() || DumbService.isDumb(project)) return; - final Document document = editor.getDocument(); - if (document.getTextLength() < elementStart) return; + final Document document = editor.getDocument(); + if (document.getTextLength() < elementStart) return; - HintHint hintHint = HintManagerImpl.createHintHint(editor, pos.getFirst(), hint, pos.getSecond()); - hintHint.setExplicitClose(true); + HintHint hintHint = HintManagerImpl.createHintHint(editor, pos.getFirst(), hint, pos.getSecond()); + hintHint.setExplicitClose(true); - Editor editorToShow = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor; - // is case of injection we need to calculate position for EditorWindow - // also we need to show the hint in the main editor because of intention bulb - hintManager.showEditorHint(hint, editorToShow, pos.getFirst(), HintManager.HIDE_BY_ESCAPE | HintManager.UPDATE_BY_SCROLLING, 0, false, hintHint); - new ParameterInfoController(project, editor, elementStart, hint, handler, provider); - } + Editor editorToShow = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor; + // is case of injection we need to calculate position for EditorWindow + // also we need to show the hint in the main editor because of intention bulb + hintManager.showEditorHint(hint, editorToShow, pos.getFirst(), HintManager.HIDE_BY_ESCAPE | HintManager.UPDATE_BY_SCROLLING, 0, false, hintHint); + new ParameterInfoController(project, editor, elementStart, hint, handler, provider); }); }