private boolean hideAutopopupIfMeaningless() {
    if (!myLookup.isLookupDisposed()
        && isAutopopupCompletion()
        && !myLookup.isSelectionTouched()
        && !myLookup.isCalculating()) {
      myLookup.refreshUi(true);
      final List<LookupElement> items = myLookup.getItems();

      for (LookupElement item : items) {
        if (!myLookup.itemPattern(item).equals(item.getLookupString())) {
          return false;
        }

        if (item.isValid()) {
          final LookupElementPresentation presentation = new LookupElementPresentation();
          item.renderElement(presentation);
          if (StringUtil.isNotEmpty(presentation.getTailText())) {
            return false;
          }
        }
      }

      myLookup.hideLookup(false);
      LOG.assertTrue(CompletionServiceImpl.getCompletionService().getCurrentCompletion() == null);
      CompletionServiceImpl.setCompletionPhase(new CompletionPhase.EmptyAutoPopup(this));
      return true;
    }
    return false;
  }
  private boolean updateLookup() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (isOutdated() || !shouldShowLookup()) return false;

    boolean justShown = false;
    if (!myLookup.isShown()) {
      if (hideAutopopupIfMeaningless()) {
        return false;
      }

      if (Registry.is("dump.threads.on.empty.lookup")
          && myLookup.isCalculating()
          && myLookup.getItems().isEmpty()) {
        PerformanceWatcher.getInstance().dumpThreads(true);
      }

      if (StringUtil.isEmpty(myLookup.getAdvertisementText()) && !isAutopopupCompletion()) {
        final String text = DefaultCompletionContributor.getDefaultAdvertisementText(myParameters);
        if (text != null) {
          myLookup.setAdvertisementText(text);
        }
      }

      if (!myLookup.showLookup()) {
        return false;
      }
      justShown = true;
    }
    myLookup.refreshUi(true, justShown);
    hideAutopopupIfMeaningless();
    if (justShown) {
      myLookup.ensureSelectionVisible();
    }
    return true;
  }
        public void itemSelected(LookupEvent event) {
          finishCompletionProcess(false);

          LookupElement item = event.getItem();
          if (item == null) return;

          setMergeCommand();

          CodeCompletionHandlerBase.lookupItemSelected(
              CompletionProgressIndicator.this,
              item,
              event.getCompletionChar(),
              myLookup.getItems());
        }
    @Override
    public boolean isEnabled(Editor editor, DataContext dataContext) {
      LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
      if (lookup == null) return false;
      if (!lookup.isAvailableToUser()) return false;
      if (focusedOnly && !CompletionPreview.hasPreview(lookup) && !lookup.isFocused()) return false;
      if (finishingChar == Lookup.NORMAL_SELECT_CHAR
              && hasTemplatePrefix(lookup, TemplateSettings.ENTER_CHAR)
          || finishingChar == Lookup.REPLACE_SELECT_CHAR
              && hasTemplatePrefix(lookup, TemplateSettings.TAB_CHAR)) {
        return false;
      }
      if (finishingChar == Lookup.REPLACE_SELECT_CHAR) {
        if (lookup.isFocused()) {
          return true;
        }
        return !lookup.getItems().isEmpty();
      }

      return true;
    }
  public static boolean hasTemplatePrefix(LookupImpl lookup, char shortcutChar) {
    lookup.refreshUi(false, false); // to bring the list model up to date

    CompletionProcess completion = CompletionService.getCompletionService().getCurrentCompletion();
    if (completion == null || !completion.isAutopopupCompletion()) {
      return false;
    }

    if (lookup.isSelectionTouched()) {
      return false;
    }

    final PsiFile file = lookup.getPsiFile();
    if (file == null) return false;

    final Editor editor = lookup.getEditor();
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());

    final int end = editor.getCaretModel().getOffset();
    final int start = lookup.getLookupStart();
    final String prefix =
        !lookup.getItems().isEmpty()
            ? editor.getDocument().getText(TextRange.create(start, end))
            : ListTemplatesHandler.getPrefix(editor.getDocument(), end);

    if (TemplateSettings.getInstance().getTemplates(prefix).isEmpty()) {
      return false;
    }

    for (TemplateImpl template :
        SurroundWithTemplateHandler.getApplicableTemplates(editor, file, false)) {
      if (prefix.equals(template.getKey())
          && shortcutChar == TemplateSettings.getInstance().getShortcutChar(template)) {
        return true;
      }
    }
    return false;
  }