private static void openEditorAndAddTemplateForAnnotation(@NotNull PyAnnotationOwner annotated) {
    assert annotated.isValid();
    assert annotated.getAnnotationValue() != null;

    final Project project = annotated.getProject();
    final int initialCaretOffset = annotated.getTextRange().getStartOffset();
    final VirtualFile updatedVirtualFile = annotated.getContainingFile().getVirtualFile();
    final TemplateBuilder templateBuilder = TemplateBuilderFactory.getInstance().createTemplateBuilder(annotated);
    final String annotation = annotated.getAnnotationValue();
    final String replacementText = ApplicationManager.getApplication().isUnitTestMode() ? "[" + annotation + "]" : annotation;
    //noinspection ConstantConditions
    templateBuilder.replaceElement(annotated.getAnnotation().getValue(), replacementText);

    final Editor editor = PythonUiService.getInstance().openTextEditor(project, updatedVirtualFile, initialCaretOffset);
    if (editor != null) {
      editor.getCaretModel().moveToOffset(initialCaretOffset);
      templateBuilder.run(editor, true);
    }
    else {
      templateBuilder.runNonInteractively(true);
    }
  }
private static void openEditorAndAddTemplateForAnnotation(@NotNull PyAnnotationOwner annotated)