Ejemplo n.º 1
0
 public static void removeNewLineAfter(@NotNull GrStatement statement) {
   ASTNode parentNode = statement.getParent().getNode();
   ASTNode next = statement.getNode().getTreeNext();
   if (parentNode != null && next != null && mNLS == next.getElementType()) {
     parentNode.removeChild(next);
   }
 }
  protected TextRange surroundExpression(GrExpression expression, PsiElement context) {
    GrIfStatement ifStatement =
        (GrIfStatement)
            GroovyPsiElementFactory.getInstance(expression.getProject())
                .createStatementFromText("if(a){4\n} else{\n}", context);
    replaceToOldExpression(ifStatement.getCondition(), expression);
    ifStatement = expression.replaceWithStatement(ifStatement);
    GrStatement psiElement = ifStatement.getThenBranch();

    assert psiElement instanceof GrBlockStatement;
    GrStatement[] statements = ((GrBlockStatement) psiElement).getBlock().getStatements();
    assert statements.length > 0;

    GrStatement statement = statements[0];
    int endOffset = statement.getTextRange().getStartOffset();
    statement.getNode().getTreeParent().removeChild(statement.getNode());

    return new TextRange(endOffset, endOffset);
  }
Ejemplo n.º 3
0
 public static <T extends GrCondition> T replaceBody(
     T newBody, GrStatement body, ASTNode node, Project project) {
   if (body == null || newBody == null) {
     throw new IncorrectOperationException();
   }
   ASTNode oldBodyNode = body.getNode();
   if (oldBodyNode.getTreePrev() != null
       && mNLS.equals(oldBodyNode.getTreePrev().getElementType())) {
     ASTNode whiteNode = GroovyPsiElementFactory.getInstance(project).createWhiteSpace().getNode();
     node.replaceChild(oldBodyNode.getTreePrev(), whiteNode);
   }
   node.replaceChild(oldBodyNode, newBody.getNode());
   return newBody;
 }