예제 #1
0
 public static boolean checkWhenExpressionHasSingleElse(@NotNull KtWhenExpression whenExpression) {
   int elseCount = 0;
   for (KtWhenEntry entry : whenExpression.getEntries()) {
     if (entry.isElse()) {
       elseCount++;
     }
   }
   return (elseCount == 1);
 }
예제 #2
0
 @Nullable
 public static <T extends PsiElement> T getDirectParentOfTypeForBlock(
     @NotNull KtBlockExpression block, @NotNull Class<T> aClass) {
   T parent = PsiTreeUtil.getParentOfType(block, aClass);
   if (parent instanceof KtIfExpression) {
     KtIfExpression ifExpression = (KtIfExpression) parent;
     if (ifExpression.getElse() == block || ifExpression.getThen() == block) {
       return parent;
     }
   }
   if (parent instanceof KtWhenExpression) {
     KtWhenExpression whenExpression = (KtWhenExpression) parent;
     for (KtWhenEntry whenEntry : whenExpression.getEntries()) {
       if (whenEntry.getExpression() == block) {
         return parent;
       }
     }
   }
   if (parent instanceof KtFunctionLiteral) {
     KtFunctionLiteral functionLiteral = (KtFunctionLiteral) parent;
     if (functionLiteral.getBodyExpression() == block) {
       return parent;
     }
   }
   if (parent instanceof KtTryExpression) {
     KtTryExpression tryExpression = (KtTryExpression) parent;
     if (tryExpression.getTryBlock() == block) {
       return parent;
     }
     for (KtCatchClause clause : tryExpression.getCatchClauses()) {
       if (clause.getCatchBody() == block) {
         return parent;
       }
     }
   }
   return null;
 }
예제 #3
0
  private WhenTranslator(
      @NotNull KtWhenExpression expression, @NotNull TranslationContext context) {
    super(context);

    whenExpression = expression;

    KtExpression subject = expression.getSubjectExpression();
    if (subject != null) {
      JsExpression subjectExpression = Translation.translateAsExpression(subject, context);
      if (TranslationUtils.isCacheNeeded(subjectExpression)) {
        TemporaryVariable subjectVar = context.declareTemporary(null);
        context.addStatementToCurrentBlock(
            JsAstUtils.assignment(subjectVar.reference(), subjectExpression).makeStmt());
        subjectExpression = subjectVar.reference();
      }
      expressionToMatch = subjectExpression;
    } else {
      expressionToMatch = null;
    }
  }
예제 #4
0
  private JsStatement translate() {
    if (expressionToMatch != null && JsAstUtils.isEmptyExpression(expressionToMatch)) {
      return JsEmpty.INSTANCE;
    }

    JsIf currentIf = null;
    JsIf resultIf = null;
    for (KtWhenEntry entry : whenExpression.getEntries()) {
      JsBlock statementBlock = new JsBlock();
      JsStatement statement = translateEntryExpression(entry, context(), statementBlock);

      if (resultIf == null && entry.isElse()) {
        context().addStatementsToCurrentBlockFrom(statementBlock);
        return statement;
      }
      statement = JsAstUtils.mergeStatementInBlockIfNeeded(statement, statementBlock);

      if (resultIf == null) {
        currentIf = JsAstUtils.newJsIf(translateConditions(entry, context()), statement);
        resultIf = currentIf;
      } else {
        if (entry.isElse()) {
          currentIf.setElseStatement(statement);
          return resultIf;
        }
        JsBlock conditionsBlock = new JsBlock();
        JsIf nextIf =
            JsAstUtils.newJsIf(
                translateConditions(entry, context().innerBlock(conditionsBlock)), statement);
        JsStatement statementToAdd =
            JsAstUtils.mergeStatementInBlockIfNeeded(nextIf, conditionsBlock);
        currentIf.setElseStatement(statementToAdd);
        currentIf = nextIf;
      }
    }
    return resultIf;
  }