private void invokeCompletion(final ExpressionContext context) { final Project project = context.getProject(); final Editor editor = context.getEditor(); final PsiFile psiFile = editor != null ? PsiUtilBase.getPsiFileInEditor(editor, project) : null; Runnable runnable = () -> { if (project.isDisposed() || editor == null || editor.isDisposed() || psiFile == null || !psiFile.isValid()) return; // it's invokeLater, so another completion could have started if (CompletionServiceImpl.getCompletionService().getCurrentCompletion() != null) return; CommandProcessor.getInstance() .executeCommand( project, () -> { // if we're in some completion's insert handler, make sure our new completion // isn't treated as the second invocation CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion); invokeCompletionHandler(project, editor); Lookup lookup = LookupManager.getInstance(project).getActiveLookup(); if (lookup != null) { lookup.addLookupListener( new MyLookupListener(context, myCheckCompletionChar)); } }, "", null); }; ApplicationManager.getApplication().invokeLater(runnable); }