예제 #1
0
 public static GrStatement replaceStatement(
     GrStatementOwner declarationOwner, ExtractInfoHelper helper) {
   GrStatement realStatement;
   if (declarationOwner != null && !isSingleExpression(helper.getStatements())) {
     // Replace set of statements
     final GrStatement[] newStatement = createResultStatement(helper);
     // add call statement
     final GrStatement[] statements = helper.getStatements();
     LOG.assertTrue(statements.length > 0);
     realStatement = null;
     for (GrStatement statement : newStatement) {
       realStatement = declarationOwner.addStatementBefore(statement, statements[0]);
       GrReferenceAdjuster.shortenReferences(realStatement);
     }
     LOG.assertTrue(realStatement != null);
     // remove old statements
     removeOldStatements(declarationOwner, helper);
     PsiImplUtil.removeNewLineAfter(realStatement);
   } else {
     // Expression call replace
     GrExpression methodCall = createMethodCall(helper);
     GrExpression oldExpr = (GrExpression) helper.getStatements()[0];
     realStatement = oldExpr.replaceWithExpression(methodCall, true);
     GrReferenceAdjuster.shortenReferences(realStatement);
   }
   return realStatement;
 }
  private static boolean isTailAfterIf(
      @NotNull GrIfStatement ifStatement, @NotNull GrStatementOwner owner) {
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
    if (flowOwner == null) return false;

    final Instruction[] flow = flowOwner.getControlFlow();

    final GrStatement[] statements = owner.getStatements();
    final int index = ArrayUtilRt.find(statements, ifStatement);
    if (index == statements.length - 1) return false;

    final GrStatement then = ifStatement.getThenBranch();

    for (Instruction i : flow) {
      final PsiElement element = i.getElement();
      if (element == null || !PsiTreeUtil.isAncestor(then, element, true)) continue;

      for (Instruction succ : i.allSuccessors()) {
        if (succ instanceof IfEndInstruction) {
          return false;
        }
      }
    }

    return true;
  }
예제 #3
0
 public static GrStatement[] getStatements(final GrStatementOwner statementOwner) {
   List<GrStatement> result = new ArrayList<GrStatement>();
   for (PsiElement cur = statementOwner.getFirstChild(); cur != null; cur = cur.getNextSibling()) {
     if (cur instanceof GrStatement) {
       result.add((GrStatement) cur);
     }
   }
   return result.toArray(new GrStatement[result.size()]);
 }
 public void visitStatementOwner(GrStatementOwner owner, boolean shouldInsertReturnNull) {
   boolean hasLineFeed = false;
   for (PsiElement e = owner.getFirstChild(); e != null; e = e.getNextSibling()) {
     if (e instanceof GrStatement) {
       ((GrStatement) e).accept(this);
       hasLineFeed = false;
     } else if (TokenSets.COMMENT_SET.contains(e.getNode().getElementType())) {
       builder.append(e.getText());
     } else if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isLineFeed(e)) {
       hasLineFeed = true;
       if (IN_TEST) {
         builder.append(genSameLineFeed(e.getText()));
       } else {
         builder.append(e.getText());
       }
     }
   }
   if (shouldInsertReturnNull) {
     if (!hasLineFeed) {
       builder.append('\n');
     }
     builder.append("return null;\n");
   }
 }
예제 #5
0
 private static void removeOldStatements(GrStatementOwner owner, ExtractInfoHelper helper)
     throws IncorrectOperationException {
   owner.removeElements(helper.getInnerElements());
 }