Search in sources :

Example 1 with GrBlockStatement

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

the class ForToEachIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrForStatement parentStatement = (GrForStatement) element;
    final GrForInClause clause = (GrForInClause) parentStatement.getClause();
    final GrVariable var = clause.getDeclaredVariable();
    final GrStatement body = parentStatement.getBody();
    final String bodyText;
    if (body instanceof GrBlockStatement) {
        final String text = body.getText();
        bodyText = text.substring(1, text.length() - 1);
    } else {
        bodyText = body.getText();
    }
    GrExpression collection = clause.getIteratedExpression();
    assert collection != null;
    @NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
    final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
    ((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
    parentStatement.replaceWithStatement(eachExpression);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NonNls(org.jetbrains.annotations.NonNls) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrBlockStatement

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

the class WhileExprSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
    GrWhileStatement whileStatement = (GrWhileStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("while(a){4\n}", context);
    replaceToOldExpression((GrExpression) whileStatement.getCondition(), expression);
    whileStatement = expression.replaceWithStatement(whileStatement);
    GrStatement body = whileStatement.getBody();
    assert body instanceof GrBlockStatement;
    GrStatement[] statements = ((GrBlockStatement) body).getBlock().getStatements();
    assert statements.length > 0;
    GrStatement statement = statements[0];
    int offset = statement.getTextRange().getStartOffset();
    statement.getNode().getTreeParent().removeChild(statement.getNode());
    return new TextRange(offset, offset);
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) TextRange(com.intellij.openapi.util.TextRange) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 3 with GrBlockStatement

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

the class MergeElseIfIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement parentStatement = (GrIfStatement) element;
    GrBlockStatement elseBlockStatement = (GrBlockStatement) parentStatement.getElseBranch();
    assert elseBlockStatement != null;
    final GrOpenBlock elseBranch = elseBlockStatement.getBlock();
    final GrStatement elseBranchContents = elseBranch.getStatements()[0];
    PsiImplUtil.replaceStatement("if(" + parentStatement.getCondition().getText() + ")" + parentStatement.getThenBranch().getText() + "else " + elseBranchContents.getText(), parentStatement);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 4 with GrBlockStatement

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

the class GroovyInlineMethodUtil method checkTailIfStatement.

public static boolean checkTailIfStatement(GrIfStatement ifStatement, Collection<GrStatement> returnStatements) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch == null)
        return false;
    boolean tb = false;
    boolean eb = false;
    if (thenBranch instanceof GrReturnStatement) {
        tb = returnStatements.remove(thenBranch);
    } else if (thenBranch instanceof GrBlockStatement) {
        tb = checkTailOpenBlock(((GrBlockStatement) thenBranch).getBlock(), returnStatements);
    }
    if (elseBranch instanceof GrReturnStatement) {
        eb = returnStatements.remove(elseBranch);
    } else if (elseBranch instanceof GrBlockStatement) {
        eb = checkTailOpenBlock(((GrBlockStatement) elseBranch).getBlock(), returnStatements);
    }
    return tb && eb;
}
Also used : GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 5 with GrBlockStatement

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

the class GrForBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    GrForStatement forStatement = PsiTreeUtil.getParentOfType(psiElement, GrForStatement.class);
    if (forStatement == null)
        return;
    final Document doc = editor.getDocument();
    PsiElement body = forStatement.getBody();
    if (body instanceof GrBlockStatement)
        return;
    if (body != null && startLine(doc, body) == startLine(doc, forStatement))
        return;
    PsiElement eltToInsertAfter = forStatement.getRParenth();
    String text = "{}";
    if (eltToInsertAfter == null) {
        eltToInsertAfter = forStatement;
        text = "){}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) Document(com.intellij.openapi.editor.Document) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)15 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)13 PsiElement (com.intellij.psi.PsiElement)6 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)6 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 Document (com.intellij.openapi.editor.Document)3 TextRange (com.intellij.openapi.util.TextRange)3 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)3 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)2 GrWhileStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement)2 NonNls (org.jetbrains.annotations.NonNls)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrControlStatement (org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)1 GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1