private void finishCompletionProcess(boolean disposeOffsetMap) {
    cancel();

    ApplicationManager.getApplication().assertIsDispatchThread();
    Disposer.dispose(myQueue);
    LookupManager.getInstance(getProject()).removePropertyChangeListener(myLookupManagerListener);

    CompletionProgressIndicator currentCompletion =
        CompletionServiceImpl.getCompletionService().getCurrentCompletion();
    LOG.assertTrue(currentCompletion == this, currentCompletion + "!=" + this);

    CompletionServiceImpl.assertPhase(
        CompletionPhase.BgCalculation.class,
        CompletionPhase.ItemsCalculated.class,
        CompletionPhase.Synchronous.class,
        CompletionPhase.CommittingDocuments.class);
    if (CompletionServiceImpl.getCompletionPhase() instanceof CompletionPhase.CommittingDocuments) {
      LOG.assertTrue(
          CompletionServiceImpl.getCompletionPhase().indicator != null,
          CompletionServiceImpl.getCompletionPhase());
      ((CompletionPhase.CommittingDocuments) CompletionServiceImpl.getCompletionPhase()).replaced =
          true;
    }
    CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion);
    if (disposeOffsetMap) {
      disposeOffsetMaps();
    }
  }
  public CompletionProgressIndicator(
      final Editor editor,
      CompletionParameters parameters,
      CodeCompletionHandlerBase handler,
      Semaphore freezeSemaphore,
      final OffsetMap offsetMap,
      boolean hasModifiers) {
    myEditor = editor;
    myParameters = parameters;
    myHandler = handler;
    myFreezeSemaphore = freezeSemaphore;
    myOffsetMap = offsetMap;
    myLookup = (LookupImpl) parameters.getLookup();

    myLookup.setArranger(new CompletionLookupArranger(parameters, this));

    myLookup.addLookupListener(myLookupListener);
    myLookup.setCalculating(true);

    myLookupManagerListener =
        new PropertyChangeListener() {
          @Override
          public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getNewValue() != null) {
              LOG.error(
                  "An attempt to change the lookup during completion, phase = "
                      + CompletionServiceImpl.getCompletionPhase());
            }
          }
        };
    LookupManager.getInstance(getProject()).addPropertyChangeListener(myLookupManagerListener);

    myQueue =
        new MergingUpdateQueue(
            "completion lookup progress", 200, true, myEditor.getContentComponent());
    myQueue.setPassThrough(false);

    ApplicationManager.getApplication().assertIsDispatchThread();
    addMapToDispose(offsetMap);

    if (ApplicationManager.getApplication().isUnitTestMode()) {
      return;
    }

    if (!myLookup.isShown()) {
      scheduleAdvertising();
    }

    if (hasModifiers) {
      trackModifiers();
    }
  }
  public void closeAndFinish(boolean hideLookup) {
    if (!myLookup.isLookupDisposed()) {
      Lookup lookup = LookupManager.getActiveLookup(myEditor);
      LOG.assertTrue(lookup == myLookup, "lookup changed: " + lookup + "; " + this);
    }
    myLookup.removeLookupListener(myLookupListener);
    finishCompletionProcess(true);
    CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass());

    if (hideLookup) {
      LookupManager.getInstance(getProject()).hideActiveLookup();
    }
  }