private void initProject(@NotNull ProjectImpl project, @Nullable ProjectImpl template)
      throws IOException {
    final ProgressIndicator indicator = myProgressManager.getProgressIndicator();
    if (indicator != null && !project.isDefault()) {
      indicator.setText(ProjectBundle.message("loading.components.for", project.getName()));
      indicator.setIndeterminate(true);
    }

    ApplicationManager.getApplication()
        .getMessageBus()
        .syncPublisher(ProjectLifecycleListener.TOPIC)
        .beforeProjectLoaded(project);

    try {
      if (template != null) {
        project.getStateStore().loadProjectFromTemplate(template);
      } else {
        project.getStateStore().load();
      }
      project.loadProjectComponents();
      project.init();
    } catch (IOException e) {
      scheduleDispose(project);
      throw e;
    } catch (ProcessCanceledException e) {
      scheduleDispose(project);
      throw e;
    }
  }
 protected static int initOccurrencesNumber(PsiNameIdentifierOwner nameIdentifierOwner) {
   final ProgressManager progressManager = ProgressManager.getInstance();
   final PsiSearchHelper searchHelper =
       PsiSearchHelper.SERVICE.getInstance(nameIdentifierOwner.getProject());
   final GlobalSearchScope scope =
       GlobalSearchScope.projectScope(nameIdentifierOwner.getProject());
   final String name = nameIdentifierOwner.getName();
   final boolean isCheapToSearch =
       name != null
           && searchHelper.isCheapEnoughToSearch(
                   name, scope, null, progressManager.getProgressIndicator())
               != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
   return isCheapToSearch ? ReferencesSearch.search(nameIdentifierOwner).findAll().size() : -1;
 }
 public static boolean isTooExpensiveToSearch(PsiNamedElement element, boolean zeroResult) {
   final String name = element.getName();
   if (name == null) {
     return true;
   }
   final ProgressManager progressManager = ProgressManager.getInstance();
   final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(element.getProject());
   final SearchScope useScope = element.getUseScope();
   if (!(useScope instanceof GlobalSearchScope)) {
     return zeroResult;
   }
   final PsiSearchHelper.SearchCostResult cost =
       searchHelper.isCheapEnoughToSearch(
           name, (GlobalSearchScope) useScope, null, progressManager.getProgressIndicator());
   if (cost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) {
     return zeroResult;
   }
   return cost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
 }