@Override
  public void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null && !templateState.isFinished()) {
      SelectionModel selectionModel = editor.getSelectionModel();
      LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);

      // the idea behind lookup checking is that if there is a preselected value in lookup
      // then user might want just to close lookup but not finish a template.
      // E.g. user wants to move to the next template segment by Tab without completion invocation.
      // If there is no selected value in completion that user definitely wants to finish template
      boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
      if (!selectionModel.hasSelection() && lookupIsEmpty) {
        CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
        templateState.gotoEnd(true);
        return;
      }
      else if (lookup != null) { //to hide lookup and remove selection at once
        lookup.hide();
      }
    }

    if (myOriginalHandler.isEnabled(editor, caret, dataContext)) {
      myOriginalHandler.execute(editor, caret, dataContext);
    }
  }
@Override
  public void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext)