Search in sources :

Example 1 with GrTraditionalForClause

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

the class GrUnnecessarySemicolonInspection method isSemicolonNecessary.

private static boolean isSemicolonNecessary(@NotNull PsiElement semicolon) {
    if (semicolon.getParent() instanceof GrTraditionalForClause)
        return true;
    PsiElement prevSibling = PsiUtil.skipSet(semicolon, false, BACKWARD_SET);
    PsiElement nextSibling = PsiUtil.skipSet(semicolon, true, FORWARD_SET);
    return prevSibling instanceof GrStatement && (nextSibling instanceof GrStatement || nextSibling != null && nextSibling.getNextSibling() instanceof GrClosableBlock);
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrTraditionalForClause

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

the class GrParameterImpl method getTypeGroovy.

@Override
@Nullable
public PsiType getTypeGroovy() {
    final PsiType declaredType = getDeclaredType();
    if (declaredType != null)
        return declaredType;
    if (isVarArgs()) {
        PsiClassType type = TypesUtil.getJavaLangObject(this);
        return new PsiEllipsisType(type);
    }
    PsiElement parent = getParent();
    if (parent instanceof GrForInClause) {
        GrExpression iteratedExpression = ((GrForInClause) parent).getIteratedExpression();
        if (iteratedExpression == null)
            return null;
        PsiType result = ClosureParameterEnhancer.findTypeForIteration(iteratedExpression, this);
        if (result != null) {
            return result;
        }
    } else if (parent instanceof GrTraditionalForClause) {
        return super.getTypeGroovy();
    } else if (parent instanceof GrCatchClause) {
        return TypesUtil.createTypeByFQClassName(CommonClassNames.JAVA_LANG_EXCEPTION, this);
    }
    return GrVariableEnhancer.getEnhancedType(this);
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrCatchClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrTraditionalForClause

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

the class GroovyBlockGenerator method generateSubBlocks.

public List<Block> generateSubBlocks() {
    //For binary expressions
    PsiElement blockPsi = myNode.getPsi();
    IElementType elementType = myNode.getElementType();
    if (blockPsi instanceof GrBinaryExpression && !(blockPsi.getParent() instanceof GrBinaryExpression)) {
        return generateForBinaryExpr();
    }
    //For multiline strings
    if ((elementType == GroovyTokenTypes.mSTRING_LITERAL || elementType == GroovyTokenTypes.mGSTRING_LITERAL) && myBlock.getTextRange().equals(myNode.getTextRange())) {
        String text = myNode.getText();
        if (text.length() > 6) {
            if (text.substring(0, 3).equals("'''") && text.substring(text.length() - 3).equals("'''") || text.substring(0, 3).equals("\"\"\"") & text.substring(text.length() - 3).equals("\"\"\"")) {
                return generateForMultiLineString();
            }
        }
    }
    //for gstrings
    if (elementType == GroovyElementTypes.GSTRING || elementType == GroovyElementTypes.REGEX || elementType == GroovyTokenTypes.mREGEX_LITERAL || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        final FormattingContext context = myNode.getPsi() instanceof GrString && ((GrString) myNode.getPsi()).isPlainString() ? myContext.createContext(true) : myContext;
        final ArrayList<Block> subBlocks = new ArrayList<>();
        ASTNode[] children = getGroovyChildren(myNode);
        for (ASTNode childNode : children) {
            if (childNode.getTextRange().getLength() > 0) {
                subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), Wrap.createWrap(WrapType.NONE, false), context));
            }
        }
        return subBlocks;
    }
    // chained properties, calls, indexing, etc
    if (NESTED.contains(elementType) && blockPsi.getParent() != null && !NESTED.contains(blockPsi.getParent().getNode().getElementType())) {
        final List<Block> subBlocks = new ArrayList<>();
        AlignmentProvider.Aligner dotsAligner = myContext.getSettings().ALIGN_MULTILINE_CHAINED_METHODS ? myAlignmentProvider.createAligner(false) : null;
        final Wrap wrap = myWrappingProcessor.getChainedMethodCallWrap();
        addNestedChildren(myNode.getPsi(), subBlocks, dotsAligner, true, wrap);
        return subBlocks;
    }
    if (blockPsi instanceof GrListOrMap && ((GrListOrMap) blockPsi).isMap() && myContext.getGroovySettings().ALIGN_NAMED_ARGS_IN_MAP) {
        AlignmentProvider.Aligner labels = myAlignmentProvider.createAligner(false);
        AlignmentProvider.Aligner exprs = myAlignmentProvider.createAligner(true);
        GrNamedArgument[] namedArgs = ((GrListOrMap) blockPsi).getNamedArguments();
        for (GrNamedArgument arg : namedArgs) {
            GrArgumentLabel label = arg.getLabel();
            if (label != null)
                labels.append(label);
            PsiElement colon = arg.getColon();
            if (colon == null)
                colon = arg.getExpression();
            if (colon != null)
                exprs.append(colon);
        }
    }
    // For Parameter lists
    if (isListLikeClause(blockPsi)) {
        final ArrayList<Block> subBlocks = new ArrayList<>();
        List<ASTNode> astNodes = visibleChildren(myNode);
        if (mustAlign(blockPsi, astNodes)) {
            final AlignmentProvider.Aligner aligner = myAlignmentProvider.createAligner(false);
            for (ASTNode node : astNodes) {
                if (!isKeyword(node))
                    aligner.append(node.getPsi());
            }
        }
        for (ASTNode childNode : astNodes) {
            subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
        }
        return subBlocks;
    }
    boolean classLevel = blockPsi instanceof GrTypeDefinitionBody;
    if (blockPsi instanceof GrClosableBlock && ((GrClosableBlock) blockPsi).getArrow() != null && ((GrClosableBlock) blockPsi).getParameters().length > 0 && !getClosureBodyVisibleChildren(myNode).isEmpty()) {
        GrClosableBlock closableBlock = (GrClosableBlock) blockPsi;
        ArrayList<Block> blocks = new ArrayList<>();
        PsiElement lbrace = closableBlock.getLBrace();
        if (lbrace != null) {
            ASTNode node = lbrace.getNode();
            blocks.add(new GroovyBlock(node, getIndent(node), Wrap.createWrap(WrapType.NONE, false), myContext));
        }
        /* {
        Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, parameterListNode);
        GroovyBlock block = new GroovyBlock(parameterListNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider);
        blocks.add(block);
      }

      {
        PsiElement arrow = closableBlock.getArrow();
        ASTNode node = arrow.getNode();
        Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, node);
        GroovyBlock block = new GroovyBlock(node, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider);
        blocks.add(block);
      }*/
        {
            Indent indent = Indent.getNormalIndent();
            ASTNode parameterListNode = closableBlock.getParameterList().getNode();
            ClosureBodyBlock bodyBlock = new ClosureBodyBlock(parameterListNode, indent, Wrap.createWrap(WrapType.NONE, false), myContext);
            blocks.add(bodyBlock);
        }
        PsiElement rbrace = closableBlock.getRBrace();
        if (rbrace != null) {
            ASTNode node = rbrace.getNode();
            blocks.add(new GroovyBlock(node, getIndent(node), Wrap.createWrap(WrapType.NONE, false), myContext));
        }
        return blocks;
    }
    if (blockPsi instanceof GrCodeBlock || blockPsi instanceof GroovyFile || classLevel) {
        return generateSubBlockForCodeBlocks(classLevel, visibleChildren(myNode), myContext.getGroovySettings().INDENT_LABEL_BLOCKS);
    }
    if (blockPsi instanceof GrMethod) {
        final ArrayList<Block> subBlocks = new ArrayList<>();
        for (ASTNode childNode : getGroovyChildren(myNode)) {
            if (childNode.getElementType() == GroovyTokenTypes.mLPAREN)
                continue;
            if (childNode.getElementType() == GroovyTokenTypes.mRPAREN)
                continue;
            if (childNode.getElementType() == GroovyElementTypes.PARAMETERS_LIST) {
                subBlocks.add(new ParameterListBlock(((GrMethod) blockPsi), Indent.getNoneIndent(), Wrap.createWrap(WrapType.NONE, false), myContext));
            } else if (canBeCorrectBlock(childNode)) {
                subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
            }
        }
        return subBlocks;
    } else if (blockPsi instanceof GrTraditionalForClause) {
        if (myContext.getSettings().ALIGN_MULTILINE_FOR) {
            final GrTraditionalForClause clause = (GrTraditionalForClause) blockPsi;
            final AlignmentProvider.Aligner parenthesesAligner = myAlignmentProvider.createAligner(false);
            parenthesesAligner.append(clause.getInitialization());
            parenthesesAligner.append(clause.getCondition());
            parenthesesAligner.append(clause.getUpdate());
        }
    } else if (blockPsi instanceof GrBinaryExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_BINARY_OPERATION) {
            final GrBinaryExpression binary = (GrBinaryExpression) blockPsi;
            final GrExpression left = binary.getLeftOperand();
            final GrExpression right = binary.getRightOperand();
            if (left != null && right != null) {
                myAlignmentProvider.addPair(left, right, false);
            }
        }
    } else if (blockPsi instanceof GrAssignmentExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_ASSIGNMENT) {
            final GrAssignmentExpression assignment = (GrAssignmentExpression) blockPsi;
            final GrExpression lValue = assignment.getLValue();
            final GrExpression rValue = assignment.getRValue();
            if (lValue != null && rValue != null) {
                myAlignmentProvider.addPair(lValue, rValue, false);
            }
        }
    } else if (blockPsi instanceof GrConditionalExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_TERNARY_OPERATION) {
            final GrConditionalExpression conditional = (GrConditionalExpression) blockPsi;
            final AlignmentProvider.Aligner exprAligner = myAlignmentProvider.createAligner(false);
            exprAligner.append(conditional.getCondition());
            if (!(conditional instanceof GrElvisExpression)) {
                exprAligner.append(conditional.getThenBranch());
            }
            exprAligner.append(conditional.getElseBranch());
            ASTNode question = conditional.getNode().findChildByType(GroovyTokenTypes.mQUESTION);
            ASTNode colon = conditional.getNode().findChildByType(GroovyTokenTypes.mCOLON);
            if (question != null && colon != null) {
                AlignmentProvider.Aligner questionColonAligner = myAlignmentProvider.createAligner(false);
                questionColonAligner.append(question.getPsi());
                questionColonAligner.append(colon.getPsi());
            }
        }
    }
    // For other cases
    final ArrayList<Block> subBlocks = new ArrayList<>();
    for (ASTNode childNode : visibleChildren(myNode)) {
        subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
    }
    return subBlocks;
}
Also used : FormattingContext(org.jetbrains.plugins.groovy.formatter.FormattingContext) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) ArrayList(java.util.ArrayList) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) AlignmentProvider(org.jetbrains.plugins.groovy.formatter.AlignmentProvider) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IElementType(com.intellij.psi.tree.IElementType) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)

Example 4 with GrTraditionalForClause

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause 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

GrTraditionalForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)4 PsiElement (com.intellij.psi.PsiElement)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)2 GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 ASTNode (com.intellij.lang.ASTNode)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1 AlignmentProvider (org.jetbrains.plugins.groovy.formatter.AlignmentProvider)1 FormattingContext (org.jetbrains.plugins.groovy.formatter.FormattingContext)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GrCondition (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrCatchClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)1 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1