use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-plugins by JetBrains.
the class JavaStepDefinitionCreator method runTemplateBuilderOnAddedStep.
protected void runTemplateBuilderOnAddedStep(Editor editor, PsiElement addedElement, PsiElement regexpElement, PsiParameterList blockVars, PsiCodeBlock body) {
Project project = regexpElement.getProject();
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(addedElement);
final TextRange range = new TextRange(1, regexpElement.getTextLength() - 1);
builder.replaceElement(regexpElement, range, regexpElement.getText().substring(range.getStartOffset(), range.getEndOffset()));
for (PsiParameter var : blockVars.getParameters()) {
final PsiElement nameIdentifier = var.getNameIdentifier();
if (nameIdentifier != null) {
builder.replaceElement(nameIdentifier, nameIdentifier.getText());
}
}
if (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);
}
Aggregations