Search in sources :

Example 1 with GrControlStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.

the class ExpressionGenerator method visitUnaryExpression.

@Override
public void visitUnaryExpression(@NotNull GrUnaryExpression expression) {
    final boolean postfix = expression.isPostfix();
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    final GrExpression operand = expression.getOperand();
    IElementType opType = expression.getOperationTokenType();
    if (resolved instanceof PsiMethod) {
        if (opType == GroovyTokenTypes.mINC || opType == GroovyTokenTypes.mDEC) {
            if (!postfix || expression.getParent() instanceof GrStatementOwner || expression.getParent() instanceof GrControlStatement) {
                if (generatePrefixIncDec((PsiMethod) resolved, operand, expression))
                    return;
            }
        }
        if (operand != null && shouldNotReplaceOperatorWithMethod(operand.getType(), null, expression.getOperationTokenType())) {
            writeSimpleUnary(operand, expression, this);
        } else {
            if (opType == GroovyTokenTypes.mLNOT) {
                builder.append('!');
            }
            invokeMethodOn(((PsiMethod) resolved), operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
        }
    } else if (operand != null) {
        if (postfix) {
            operand.accept(this);
            builder.append(expression.getOperationToken().getText());
        } else {
            builder.append(expression.getOperationToken().getText());
            operand.accept(this);
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 2 with GrControlStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.

the class GroovyCompletionData method isControlStructure.

private static boolean isControlStructure(PsiElement context) {
    final int offset = context.getTextRange().getStartOffset();
    PsiElement prevSibling = context.getPrevSibling();
    if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
        ASTNode node = prevSibling.getNode();
        return !TokenSets.DOTS.contains(node.getElementType());
    }
    if (GroovyCompletionUtil.isNewStatement(context, true)) {
        final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
        if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
            return true;
        }
    }
    if (context.getParent() != null) {
        PsiElement parent = context.getParent();
        if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
            return true;
        }
        if (parent instanceof GrReferenceExpression) {
            PsiElement superParent = parent.getParent();
            if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
                return true;
            }
        }
        return false;
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 3 with GrControlStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.

the class GrBracesSurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    final PsiElement e0 = elements[0];
    final PsiElement parent = e0.getParent();
    final GrCodeBlock block;
    if (parent instanceof GrControlStatement) {
        block = factory.createMethodBodyFromText("\n");
        final PsiElement prev = e0.getPrevSibling();
        if (prev != null && prev.getNode().getElementType().equals(GroovyTokenTypes.mNLS)) {
            final ASTNode parentNode = e0.getParent().getNode();
            parentNode.addLeaf(TokenType.WHITE_SPACE, " ", prev.getNode());
            parentNode.removeChild(prev.getNode());
        }
    } else {
        block = factory.createClosureFromText("{}");
    }
    GroovyManyStatementsSurrounder.addStatements(block, elements);
    return block;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ASTNode(com.intellij.lang.ASTNode) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 4 with GrControlStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.

the class GroovyFix method replaceStatement.

/**
   * unwraps surrounding blocks from newStatement.
   */
protected static void replaceStatement(GrStatement oldStatement, GrStatement newStatement) throws IncorrectOperationException {
    if (newStatement instanceof GrBlockStatement) {
        GrBlockStatement blockStatement = (GrBlockStatement) newStatement;
        final GrOpenBlock openBlock = blockStatement.getBlock();
        final GrStatement[] statements = openBlock.getStatements();
        if (statements.length == 0) {
            oldStatement.removeStatement();
        } else {
            final PsiElement parent = oldStatement.getParent();
            if (parent instanceof GrStatementOwner) {
                GrStatementOwner statementOwner = (GrStatementOwner) parent;
                for (GrStatement statement : statements) {
                    statementOwner.addStatementBefore(statement, oldStatement);
                }
                oldStatement.removeStatement();
            } else if (parent instanceof GrControlStatement) {
                oldStatement.replace(newStatement);
            }
        }
    } else {
        oldStatement.replaceWithStatement(newStatement);
    }
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 5 with GrControlStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.

the class GenerationUtil method writeStatement.

static void writeStatement(@NotNull StringBuilder codeBlockBuilder, @NotNull StringBuilder statementBuilder, @Nullable GrStatement statement, @Nullable ExpressionContext context) {
    final PsiElement parent = statement == null ? null : statement.getParent();
    final boolean addParentheses;
    if (statement == null) {
        addParentheses = context != null && context.shouldInsertCurlyBrackets();
    } else {
        addParentheses = context != null && (context.shouldInsertCurlyBrackets() || !context.myStatements.isEmpty()) && parent instanceof GrControlStatement;
    }
    if (addParentheses) {
        codeBlockBuilder.append("{\n");
    }
    if (context != null) {
        insertStatementFromContextBefore(codeBlockBuilder, context);
    }
    codeBlockBuilder.append(statementBuilder);
    if (addParentheses) {
        codeBlockBuilder.append("}\n");
    }
}
Also used : GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrControlStatement (org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)8 PsiElement (com.intellij.psi.PsiElement)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)3 ASTNode (com.intellij.lang.ASTNode)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 IElementType (com.intellij.psi.tree.IElementType)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrBreakStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement)1 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1