Search in sources :

Example 11 with TemplateBuilder

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);
}
Also used : Project(com.intellij.openapi.project.Project) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)11 Editor (com.intellij.openapi.editor.Editor)5 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 PsiElement (com.intellij.psi.PsiElement)5 ParamHelper (com.jetbrains.python.psi.impl.ParamHelper)3 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 PyClass (com.jetbrains.python.psi.PyClass)2 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyFile (com.jetbrains.python.psi.PyFile)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)1