void () { final Document document = myEditor.getDocument(); final VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null) { ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(Collections.singletonList(file)); } String selection = myEditor.getSelectionModel().getSelectedText(true); if (selection != null) { selection = selection.trim(); PsiDocumentManager.getInstance(myFile.getProject()).commitAllDocuments(); myTemplate.wrap(selection, new CustomTemplateCallback(myEditor, myFile) { @Override public void startTemplate(@NotNull Template template, Map<String, String> predefinedValues, TemplateEditingListener listener) { super.startTemplate(template, predefinedValues, listener); if (myAfterExecutionCallback != null) { myAfterExecutionCallback.run(); } } }); } }