Condition<PostfixTemplate> (@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); Document copyDocument = copyFile.getViewProvider().getDocument(); if (copyDocument == null) { return Conditions.alwaysFalse(); } copyFile = provider.preCheck(copyFile, editor, newOffset); copyDocument = copyFile.getViewProvider().getDocument(); if (copyDocument == null) { return Conditions.alwaysFalse(); } // The copy document doesn't contain live template key. // Register offset translator to make getOriginalElement() work in the copy. Document fileDocument = file.getViewProvider().getDocument(); if (fileDocument != null && 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); }