コード例 #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
  @SuppressWarnings("ConstantConditions")
  public static void foldWhenExpressionWithAssignments(JetWhenExpression whenExpression) {
    Project project = whenExpression.getProject();

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

    JetBinaryExpression firstAssignment =
        getFoldableBranchedAssignment(whenExpression.getEntries().get(0).getExpression());

    assertNotNull(firstAssignment);

    String op = firstAssignment.getOperationReference().getText();
    JetSimpleNameExpression lhs = (JetSimpleNameExpression) firstAssignment.getLeft();

    JetBinaryExpression assignment =
        JetPsiFactory.createBinaryExpression(project, lhs, op, whenExpression);
    JetWhenExpression newWhenExpression = (JetWhenExpression) assignment.getRight();

    assertNotNull(newWhenExpression);

    for (JetWhenEntry entry : newWhenExpression.getEntries()) {
      JetBinaryExpression currAssignment = getFoldableBranchedAssignment(entry.getExpression());

      assertNotNull(currAssignment);

      JetExpression currRhs = currAssignment.getRight();

      assertNotNull(currRhs);

      currAssignment.replace(currRhs);
    }

    whenExpression.replace(assignment);
  }