private static Condition<PostfixTemplate> createIsApplicationTemplateFunction(@NotNull final PostfixTemplateProvider provider,
                                                                                @NotNull String key,
                                                                                @NotNull PsiFile file,
                                                                                @NotNull Editor editor,
                                                                                @NotNull Disposable parentDisposable) {
    if (file.getFileType().isBinary()) {
      return Conditions.alwaysFalse();
    }

    int currentOffset = editor.getCaretModel().getOffset();
    final int newOffset = currentOffset - key.length();
    CharSequence fileContent = editor.getDocument().getCharsSequence();
    StringBuilder fileContentWithoutKey = new StringBuilder();
    fileContentWithoutKey.append(fileContent.subSequence(0, newOffset));
    fileContentWithoutKey.append(fileContent.subSequence(currentOffset, fileContent.length()));
    PsiFile copyFile = copyFile(file, fileContentWithoutKey);

    copyFile = provider.preCheck(copyFile, editor, newOffset);
    Document copyDocument = copyFile.getFileDocument();

    // The copy document doesn't contain live template key.
    // Register offset translator to make getOriginalElement() work in the copy.
    Document fileDocument = file.getFileDocument();
    if (fileDocument.getTextLength() < currentOffset) {
      LOG.error("File document length (" + fileDocument.getTextLength() + ") is less than offset (" + currentOffset + ")",
                CoreAttachmentFactory.createAttachment(fileDocument), CoreAttachmentFactory.createAttachment(editor.getDocument()));
    }
    Document originalDocument = editor.getDocument();
    OffsetTranslator translator = new OffsetTranslator(originalDocument, file, copyDocument, newOffset, currentOffset, "");
    Disposer.register(parentDisposable, translator);

    final PsiElement context = CustomTemplateCallback.getContext(copyFile, positiveOffset(newOffset));
    final Document finalCopyDocument = copyDocument;
    return template -> template != null && isDumbEnough(template, context) &&
                       template.isEnabled(provider) && template.isApplicable(context, finalCopyDocument, newOffset);
  }
private static Condition<PostfixTemplate> createIsApplicationTemplateFunction(@NotNull final PostfixTemplateProvider provider,
                                                                                @NotNull String key,
                                                                                @NotNull PsiFile file,
                                                                                @NotNull Editor editor,
                                                                                @NotNull Disposable parentDisposable)