@Override
 public boolean startTemplate(@NotNull Editor editor, char shortcutChar) {
   Runnable runnable = prepareTemplate(editor, shortcutChar, null);
   if (runnable != null) {
     PsiDocumentManager.getInstance(myProject).commitDocument(editor.getDocument());
     runnable.run();
   }
   return runnable != null;
 }
  private void startTemplate(
      final Editor editor,
      final String selectionString,
      final Template template,
      boolean inSeparateCommand,
      TemplateEditingListener listener,
      final PairProcessor<String, String> processor,
      final Map<String, String> predefinedVarValues) {
    final TemplateState templateState = initTemplateState(editor);

    //noinspection unchecked
    templateState.getProperties().put(ExpressionContext.SELECTION, selectionString);

    if (listener != null) {
      templateState.addTemplateStateListener(listener);
    }
    Runnable r =
        () -> {
          if (selectionString != null) {
            ApplicationManager.getApplication()
                .runWriteAction(() -> EditorModificationUtil.deleteSelectedText(editor));
          } else {
            editor.getSelectionModel().removeSelection();
          }
          templateState.start((TemplateImpl) template, processor, predefinedVarValues);
        };
    if (inSeparateCommand) {
      CommandProcessor.getInstance()
          .executeCommand(
              myProject, r, CodeInsightBundle.message("insert.code.template.command"), null);
    } else {
      r.run();
    }

    if (shouldSkipInTests()) {
      if (!templateState.isFinished()) templateState.gotoEnd(false);
    }
  }
    @Override
    public void itemSelected(LookupEvent event) {
      LookupElement item = event.getItem();
      if (item == null) return;

      char c = event.getCompletionChar();
      if (myCheckCompletionChar && !LookupEvent.isSpecialCompletionChar(c)) {
        return;
      }

      final Project project = myContext.getProject();
      if (project == null) {
        return;
      }

      Runnable runnable =
          () ->
              new WriteCommandAction(project) {
                @Override
                protected void run(@NotNull com.intellij.openapi.application.Result result)
                    throws Throwable {
                  Editor editor = myContext.getEditor();
                  if (editor != null) {
                    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
                    if (templateState != null) {
                      templateState.considerNextTabOnLookupItemSelected(item);
                    }
                  }
                }
              }.execute();
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
      } else {
        ApplicationManager.getApplication()
            .invokeLater(runnable, ModalityState.current(), project.getDisposed());
      }
    }