Search in sources :

Example 6 with GrBlockStatement

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

the class GrWhileBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrWhileStatement))
        return;
    GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
    final Document doc = editor.getDocument();
    PsiElement body = whileStatement.getBody();
    if (body instanceof GrBlockStatement)
        return;
    if (body != null && GrForBodyFixer.startLine(editor.getDocument(), body) == GrForBodyFixer.startLine(editor.getDocument(), whileStatement) && whileStatement.getCondition() != null)
        return;
    final PsiElement rParenth = whileStatement.getRParenth();
    assert rParenth != null;
    doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) Document(com.intellij.openapi.editor.Document) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement)

Example 7 with GrBlockStatement

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

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

the class ConditionalUtils method stripBraces.

public static GrStatement stripBraces(GrStatement branch) {
    if (branch instanceof GrBlockStatement) {
        final GrBlockStatement block = (GrBlockStatement) branch;
        final GrStatement[] statements = block.getBlock().getStatements();
        if (statements.length == 1) {
            return statements[0];
        } else {
            return block;
        }
    } else {
        return branch;
    }
}
Also used : GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 9 with GrBlockStatement

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

the class ConditionalUtils method stripBraces.

public static GrStatement stripBraces(GrStatement branch) {
    if (branch instanceof GrBlockStatement) {
        final GrBlockStatement block = (GrBlockStatement) branch;
        final GrOpenBlock codeBlock = block.getBlock();
        final GrStatement[] statements = codeBlock.getStatements();
        if (statements.length == 1) {
            return statements[0];
        } else {
            return block;
        }
    } else {
        return branch;
    }
}
Also used : 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 10 with GrBlockStatement

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

the class GrRedundantElseIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrIfStatement))
        return;
    final GrIfStatement ifStatement = GroovyRefactoringUtil.addBlockIntoParent((GrIfStatement) parent);
    assert ifStatement.getParent() instanceof GrStatementOwner;
    final PsiElement statementOwner = ifStatement.getParent();
    final GrStatement branch = ifStatement.getElseBranch();
    if (branch == null)
        return;
    final PsiElement pos;
    if (branch instanceof GrBlockStatement) {
        final GrOpenBlock block = ((GrBlockStatement) branch).getBlock();
        final PsiElement first = inferFirst(block.getLBrace());
        final PsiElement last = inferLast(block.getRBrace());
        pos = statementOwner.addRangeAfter(first, last, ifStatement);
    } else {
        pos = statementOwner.addAfter(branch, ifStatement);
    }
    branch.delete();
    editor.getCaretModel().moveToOffset(pos.getTextRange().getStartOffset());
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) 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) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

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