Ejemplo n.º 1
0
  private static boolean checkFoldableIfExpressionWithAsymmetricReturns(
      JetIfExpression ifExpression) {
    if (getFoldableBranchedReturn(ifExpression.getThen()) == null
        || ifExpression.getElse() != null) {
      return false;
    }

    PsiElement nextElement = JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression);
    return (nextElement instanceof JetExpression)
        && getFoldableBranchedReturn((JetExpression) nextElement) != null;
  }
Ejemplo n.º 2
0
  public static void foldIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
    Project project = ifExpression.getProject();

    JetExpression condition = ifExpression.getCondition();
    JetExpression thenRoot = ifExpression.getThen();
    JetExpression elseRoot =
        (JetExpression) JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression);

    assertNotNull(condition);
    assertNotNull(thenRoot);
    assertNotNull(elseRoot);

    //noinspection ConstantConditions
    JetIfExpression newIfExpression =
        JetPsiFactory.createIf(project, condition, thenRoot, elseRoot);
    JetReturnExpression newReturnExpression = JetPsiFactory.createReturn(project, newIfExpression);

    newIfExpression = (JetIfExpression) newReturnExpression.getReturnedExpression();

    assertNotNull(newIfExpression);

    //noinspection ConstantConditions
    JetReturnExpression thenReturn = getFoldableBranchedReturn(newIfExpression.getThen());
    JetReturnExpression elseReturn = getFoldableBranchedReturn(newIfExpression.getElse());

    assertNotNull(thenReturn);
    assertNotNull(elseReturn);

    JetExpression thenExpr = thenReturn.getReturnedExpression();
    JetExpression elseExpr = elseReturn.getReturnedExpression();

    assertNotNull(thenExpr);
    assertNotNull(elseExpr);

    //noinspection ConstantConditions
    thenReturn.replace(thenExpr);
    //noinspection ConstantConditions
    elseReturn.replace(elseExpr);

    elseRoot.delete();
    ifExpression.replace(newReturnExpression);
  }