Search in sources :

Example 6 with GrCodeBlock

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

the class GroovyGenerationInfo method positionCaret.

@Override
public void positionCaret(@NotNull Editor editor, boolean toEditMethodBody) {
    final T firstMember = getPsiMember();
    LOG.assertTrue(firstMember.isValid());
    if (toEditMethodBody) {
        GrMethod method = (GrMethod) firstMember;
        GrOpenBlock body = method.getBlock();
        if (body != null) {
            PsiElement l = body.getLBrace();
            if (l != null)
                l = l.getNextSibling();
            while (PsiImplUtil.isWhiteSpaceOrNls(l)) l = l.getNextSibling();
            if (l == null)
                l = body;
            PsiElement r = body.getRBrace();
            if (r != null)
                r = r.getPrevSibling();
            while (PsiImplUtil.isWhiteSpaceOrNls(r)) r = r.getPrevSibling();
            if (r == null)
                r = body;
            int start = l.getTextRange().getStartOffset();
            int end = r.getTextRange().getEndOffset();
            editor.getCaretModel().moveToOffset(Math.min(start, end));
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
            if (start < end) {
                //Not an empty body
                editor.getSelectionModel().setSelection(start, end);
            }
            return;
        }
    }
    int offset;
    if (firstMember instanceof GrMethod) {
        GrMethod method = (GrMethod) firstMember;
        GrCodeBlock body = method.getBlock();
        if (body == null) {
            offset = method.getTextRange().getStartOffset();
        } else {
            offset = body.getLBrace().getTextRange().getEndOffset();
        }
    } else {
        offset = firstMember.getTextRange().getStartOffset();
    }
    editor.getCaretModel().moveToOffset(offset);
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) 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 7 with GrCodeBlock

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

the class GrIntroduceClosureParameterProcessor method insertDeclaration.

private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
    if (original instanceof GrField) {
        final PsiClass containingClass = ((GrField) original).getContainingClass();
        LOG.assertTrue(containingClass != null);
        return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
    }
    final GrStatementOwner block;
    if (original instanceof PsiParameter) {
        final PsiElement container = original.getParent().getParent();
        if (container instanceof GrMethod) {
            block = ((GrMethod) container).getBlock();
        } else if (container instanceof GrClosableBlock) {
            block = (GrCodeBlock) container;
        } else if (container instanceof GrForStatement) {
            final GrStatement body = ((GrForStatement) container).getBody();
            if (body instanceof GrBlockStatement) {
                block = ((GrBlockStatement) body).getBlock();
            } else {
                GrBlockStatement blockStatement = myFactory.createBlockStatement();
                LOG.assertTrue(blockStatement != null);
                if (body != null) {
                    blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
                    blockStatement = (GrBlockStatement) body.replace(blockStatement);
                } else {
                    blockStatement = (GrBlockStatement) container.add(blockStatement);
                }
                block = blockStatement.getBlock();
            }
        } else {
            throw new IncorrectOperationException();
        }
        LOG.assertTrue(block != null);
        return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
    }
    PsiElement parent = original.getParent();
    LOG.assertTrue(parent instanceof GrVariableDeclaration);
    final PsiElement pparent = parent.getParent();
    if (pparent instanceof GrIfStatement) {
        if (((GrIfStatement) pparent).getThenBranch() == parent) {
            block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
        } else {
            block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
        }
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else if (pparent instanceof GrLoopStatement) {
        block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else {
        LOG.assertTrue(pparent instanceof GrStatementOwner);
        block = (GrStatementOwner) pparent;
    }
    return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 8 with GrCodeBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock 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 9 with GrCodeBlock

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

the class RecursionUtils method tryStatementMayReturnBeforeRecursing.

private static boolean tryStatementMayReturnBeforeRecursing(GrTryCatchStatement tryStatement, GrMethod method) {
    final GrFinallyClause finallyBlock = tryStatement.getFinallyClause();
    if (finallyBlock != null) {
        final GrOpenBlock body = finallyBlock.getBody();
        if (codeBlockMayReturnBeforeRecursing(body, method, false)) {
            return true;
        }
        if (codeBlockDefinitelyRecurses(body, method)) {
            return false;
        }
    }
    final GrCodeBlock tryBlock = tryStatement.getTryBlock();
    if (codeBlockMayReturnBeforeRecursing(tryBlock, method, false)) {
        return true;
    }
    final GrCatchClause[] catchBlocks = tryStatement.getCatchClauses();
    for (final GrCatchClause catchBlock : catchBlocks) {
        if (codeBlockMayReturnBeforeRecursing(catchBlock.getBody(), method, false)) {
            return true;
        }
    }
    return false;
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 10 with GrCodeBlock

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

the class ControlFlowUtils method openBlockCompletesWithStatement.

public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
    GroovyPsiElement elementToCheck = statement;
    while (true) {
        if (elementToCheck == null)
            return false;
        final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
        if (container == null)
            return false;
        if (isLoop(container))
            return false;
        if (container instanceof GrCaseSection) {
            final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (container == sections[sections.length - 1])
                return false;
        }
        if (container instanceof GrCodeBlock) {
            if (elementToCheck instanceof GrStatement) {
                final GrCodeBlock codeBlock = (GrCodeBlock) container;
                if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
                    return false;
                }
            }
            if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
                if (container.equals(body)) {
                    return true;
                }
                elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
            } else {
                elementToCheck = container;
            }
        } else {
            elementToCheck = container;
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Aggregations

GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)22 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)10 PsiElement (com.intellij.psi.PsiElement)6 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 ASTNode (com.intellij.lang.ASTNode)2 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1