addedElement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(addedElement);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedElement);
editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
assert editor != null;
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(addedElement);
final PsiAnnotation annotation = addedElement.getModifierList().getAnnotations()[0];
final PsiNameValuePair regexpElement = annotation.getParameterList().getAttributes()[0];
final TextRange range = new TextRange(1, regexpElement.getTextLength() - 1);
builder.replaceElement(regexpElement, range, regexpElement.getText().substring(range.getStartOffset(), range.getEndOffset()));
final PsiParameterList blockVars = addedElement.getParameterList();
for (PsiParameter var : blockVars.getParameters()) {
final PsiElement nameIdentifier = var.getNameIdentifier();
if (nameIdentifier != null) {
builder.replaceElement(nameIdentifier, nameIdentifier.getText());
}
}
final PsiCodeBlock body = addedElement.getBody();
if (body != null && body.getStatements().length > 0) {
final PsiElement firstStatement = body.getStatements()[0];
final TextRange pendingRange = new TextRange(0, firstStatement.getTextLength() - 1);
builder.replaceElement(firstStatement, pendingRange,
firstStatement.getText().substring(pendingRange.getStartOffset(), pendingRange.getEndOffset()));
}
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.doPostponedOperationsAndUnblockDocument(editor.getDocument());
builder.run(editor, false);
}
return true;
}