diff --git a/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java b/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java index 4dd4eadfc919..35c685422204 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/AutoPopupController.java @@ -90,17 +90,12 @@ public class AutoPopupController implements Disposable { final CodeInsightSettings settings = CodeInsightSettings.getInstance(); if (settings.AUTO_POPUP_COMPLETION_LOOKUP) { - final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, myProject); - if (file == null) return; + if (PsiUtilBase.getPsiFileInEditor(editor, myProject) == null) return; final Runnable request = new Runnable(){ public void run(){ - if (myProject.isDisposed()) return; - if (editor.isDisposed()) return; - - //PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - if (!file.isValid()) return; - - CompletionAutoPopupHandler.invokeAutoPopupCompletion(myProject, editor, condition); + if (!myProject.isDisposed() && !editor.isDisposed()) { + CompletionAutoPopupHandler.scheduleAutoPopup(editor, condition); + } } }; 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 d8d858c11a23..dda1b66d3b7e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java @@ -591,7 +591,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement public void scheduleRestart() { if (isAutopopupCompletion() && hideAutopopupIfMeaningless()) { - CompletionAutoPopupHandler.scheduleAutoPopup(getProject(), myEditor); + CompletionAutoPopupHandler.scheduleAutoPopup(myEditor, null); return; } 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 8aae01ccdec3..ecf9e4ba8799 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java @@ -35,6 +35,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.impl.PsiDocumentManagerImpl; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author peter @@ -82,59 +83,32 @@ public class CompletionAutoPopupHandler extends TypedHandlerDelegate { return Result.CONTINUE; } - scheduleAutoPopup(project, editor); + scheduleAutoPopup(editor, null); return Result.STOP; } - public static void scheduleAutoPopup(final Project project, final Editor editor) { + public static void scheduleAutoPopup(final Editor editor, @Nullable final Condition condition) { + final Project project = editor.getProject(); + assert project != null; final CompletionPhase.AutoPopupAlarm phase = new CompletionPhase.AutoPopupAlarm(false, editor); CompletionServiceImpl.setCompletionPhase(phase); - final Runnable request = new Runnable() { - @Override - public void run() { - if (phase.isExpired()) return; - invokeCompletion(CompletionType.BASIC, true, project, editor, 0); - } - }; AutoPopupController.getInstance(project).invokeAutoPopupRunnable(new Runnable() { @Override public void run() { - runLaterWithCommitted(project, editor.getDocument(), request); - } - }, CodeInsightSettings.getInstance().AUTO_LOOKUP_DELAY); - } - - public static void invokeAutoPopupCompletion(final Project project, final Editor editor, final Condition condition) { - ApplicationManager.getApplication().assertIsDispatchThread(); - - final Document document = editor.getDocument(); - final long beforeStamp = document.getModificationStamp(); - final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); - documentManager.cancelAndRunWhenAllCommitted("start completion when all docs committed", new Runnable() { - @Override - public void run() { - long afterStamp = document.getModificationStamp(); - if (beforeStamp != afterStamp) { - // no luck, will try later - return; - } - // later because we may end up in write action here if there was a synchronous commit - ApplicationManager.getApplication().invokeLater(new Runnable() { + runLaterWithCommitted(project, editor.getDocument(), new Runnable() { @Override public void run() { - long afterStamp = document.getModificationStamp(); - if (beforeStamp != afterStamp) { - // no luck, will try later - return; - } - PsiFile file = documentManager.getPsiFile(document); + if (phase.isExpired()) return; + + PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file != null && condition != null && !condition.value(file)) return; + invokeCompletion(CompletionType.BASIC, true, project, editor, 0); } - }, project.getDisposed()); + }); } - }); + }, CodeInsightSettings.getInstance().AUTO_LOOKUP_DELAY); } public static void invokeCompletion(CompletionType completionType, diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java index c76ca0c14c8e..9cb628f1c290 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/TypedHandler.java @@ -77,7 +77,7 @@ public class TypedHandler extends TypedActionHandlerBase { }); lookup.appendPrefix(charTyped); if (lookup.isStartCompletionWhenNothingMatches() && lookup.getItems().isEmpty()) { - CompletionAutoPopupHandler.scheduleAutoPopup(editor.getProject(), editor); + CompletionAutoPopupHandler.scheduleAutoPopup(editor, null); } AutoHardWrapHandler.getInstance().wrapLineIfNecessary(editor, dataContext, modificationStamp);