Search in sources :

Example 11 with GrForInClause

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.

the class CodeBlockGenerator method visitForStatement.

@Override
public void visitForStatement(@NotNull GrForStatement forStatement) {
    //final StringBuilder builder = new StringBuilder();
    builder.append("for(");
    final GrForClause clause = forStatement.getClause();
    ExpressionContext forContext = context.extend();
    if (clause instanceof GrForInClause) {
        final GrExpression expression = ((GrForInClause) clause).getIteratedExpression();
        final GrVariable declaredVariable = clause.getDeclaredVariable();
        LOG.assertTrue(declaredVariable != null);
        writeVariableWithoutSemicolonAndInitializer(builder, declaredVariable, context);
        builder.append(" : ");
        if (expression != null) {
            final ExpressionContext context = forContext.copy();
            writeExpression(expression, builder, context);
        }
    } else if (clause instanceof GrTraditionalForClause) {
        final GrTraditionalForClause cl = (GrTraditionalForClause) clause;
        final GrCondition initialization = cl.getInitialization();
        final GrExpression condition = cl.getCondition();
        final GrExpression update = cl.getUpdate();
        if (initialization instanceof GrParameter) {
            StringBuilder partBuilder = new StringBuilder();
            writeVariableWithoutSemicolonAndInitializer(partBuilder, (GrParameter) initialization, context);
            final GrExpression initializer = ((GrParameter) initialization).getInitializerGroovy();
            if (initializer != null) {
                final ExpressionContext partContext = forContext.copy();
                partBuilder.append(" = ");
                writeExpression(initializer, partBuilder, partContext);
                for (String statement : partContext.myStatements) {
                    builder.append(statement).append(", ");
                }
                builder.append(partBuilder);
            }
        } else if (initialization != null) {
            StringBuilder partBuilder = new StringBuilder();
            final ExpressionContext partContext = forContext.copy();
            genForPart(builder, initialization, new CodeBlockGenerator(partBuilder, partContext, null));
        }
        builder.append(';');
        if (condition != null) {
            //todo???
            genForPart(builder, condition, forContext.copy());
        }
        builder.append(';');
        if (update != null) {
            genForPart(builder, update, forContext.copy());
        }
    }
    builder.append(')');
    final GrStatement body = forStatement.getBody();
    if (body != null) {
        body.accept(new CodeBlockGenerator(builder, forContext, null));
    }
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)

Aggregations

GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)4 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 PsiElement (com.intellij.psi.PsiElement)3 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 TextRange (com.intellij.openapi.util.TextRange)2 GrTraditionalForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)2 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PsiFile (com.intellij.psi.PsiFile)1 PsiParameter (com.intellij.psi.PsiParameter)1