コード例 #1
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);
  }
コード例 #2
0
  // Returns null if standalone closing brace is not found
  private static BraceStatus checkForMovableClosingBrace(
      @NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    PsiElement closingBrace = getStandaloneClosingBrace(file, editor);
    if (closingBrace == null) return BraceStatus.NOT_FOUND;

    PsiElement blockLikeElement = closingBrace.getParent();
    if (!(blockLikeElement instanceof JetBlockExpression)) return BraceStatus.NOT_MOVABLE;

    PsiElement blockParent = blockLikeElement.getParent();
    if (blockParent instanceof JetWhenEntry) return BraceStatus.NOT_FOUND;
    if (PsiTreeUtil.instanceOf(blockParent, FUNCTIONLIKE_ELEMENT_CLASSES))
      return BraceStatus.NOT_FOUND;

    PsiElement enclosingExpression =
        PsiTreeUtil.getParentOfType(blockLikeElement, JetExpression.class);

    if (enclosingExpression instanceof JetDoWhileExpression) return BraceStatus.NOT_MOVABLE;

    if (enclosingExpression instanceof JetIfExpression) {
      JetIfExpression ifExpression = (JetIfExpression) enclosingExpression;

      if (blockLikeElement == ifExpression.getThen() && ifExpression.getElse() != null)
        return BraceStatus.NOT_MOVABLE;
    }

    return down
        ? checkForMovableDownClosingBrace(closingBrace, blockLikeElement, editor, info)
        : checkForMovableUpClosingBrace(closingBrace, blockLikeElement, editor, info);
  }
コード例 #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);
  }
コード例 #4
0
  private static boolean checkFoldableIfExpressionWithAssignments(JetIfExpression ifExpression) {
    JetExpression thenBranch = ifExpression.getThen();
    JetExpression elseBranch = ifExpression.getElse();

    JetBinaryExpression thenAssignment = getFoldableBranchedAssignment(thenBranch);
    JetBinaryExpression elseAssignment = getFoldableBranchedAssignment(elseBranch);

    if (thenAssignment == null || elseAssignment == null) return false;

    return checkAssignmentsMatch(thenAssignment, elseAssignment);
  }
コード例 #5
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;
  }
コード例 #6
0
  public static void foldIfExpressionWithAssignments(JetIfExpression ifExpression) {
    Project project = ifExpression.getProject();

    JetBinaryExpression thenAssignment = getFoldableBranchedAssignment(ifExpression.getThen());

    assertNotNull(thenAssignment);

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

    JetBinaryExpression assignment =
        JetPsiFactory.createBinaryExpression(project, lhs, op, ifExpression);
    JetIfExpression newIfExpression = (JetIfExpression) assignment.getRight();

    assertNotNull(newIfExpression);

    //noinspection ConstantConditions
    thenAssignment = getFoldableBranchedAssignment(newIfExpression.getThen());
    JetBinaryExpression elseAssignment = getFoldableBranchedAssignment(newIfExpression.getElse());

    assertNotNull(thenAssignment);
    assertNotNull(elseAssignment);

    JetExpression thenRhs = thenAssignment.getRight();
    JetExpression elseRhs = elseAssignment.getRight();

    assertNotNull(thenRhs);
    assertNotNull(elseRhs);

    //noinspection ConstantConditions
    thenAssignment.replace(thenRhs);
    //noinspection ConstantConditions
    elseAssignment.replace(elseRhs);

    ifExpression.replace(assignment);
  }
コード例 #7
0
 private static boolean checkFoldableIfExpressionWithReturns(JetIfExpression ifExpression) {
   return getFoldableBranchedReturn(ifExpression.getThen()) != null
       && getFoldableBranchedReturn(ifExpression.getElse()) != null;
 }