コード例 #1
0
  public static void foldWhenExpressionWithReturns(JetWhenExpression whenExpression) {
    Project project = whenExpression.getProject();

    assert !whenExpression.getEntries().isEmpty() : FOLD_WITHOUT_CHECK;

    JetReturnExpression newReturnExpression = JetPsiFactory.createReturn(project, whenExpression);
    JetWhenExpression newWhenExpression =
        (JetWhenExpression) newReturnExpression.getReturnedExpression();

    assertNotNull(newWhenExpression);

    //noinspection ConstantConditions
    for (JetWhenEntry entry : newWhenExpression.getEntries()) {
      JetReturnExpression currReturn = getFoldableBranchedReturn(entry.getExpression());

      assertNotNull(currReturn);

      JetExpression currExpr = currReturn.getReturnedExpression();

      assertNotNull(currExpr);

      //noinspection ConstantConditions
      currReturn.replace(currExpr);
    }

    whenExpression.replace(newReturnExpression);
  }
コード例 #2
0
  public static void foldIfExpressionWithReturns(JetIfExpression ifExpression) {
    Project project = ifExpression.getProject();

    JetReturnExpression newReturnExpression = JetPsiFactory.createReturn(project, ifExpression);
    JetIfExpression 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);

    ifExpression.replace(newReturnExpression);
  }
コード例 #3
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);
  }