Search in sources :

Example 11 with GrIfStatement

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

the class GroovyInlineMethodUtil method checkTailOpenBlock.

private static boolean checkTailOpenBlock(GrOpenBlock block, Collection<GrStatement> returnStatements) {
    if (block == null)
        return false;
    GrStatement[] statements = block.getStatements();
    if (statements.length == 0)
        return false;
    GrStatement last = statements[statements.length - 1];
    if (returnStatements.contains(last)) {
        returnStatements.remove(last);
        return true;
    }
    if (last instanceof GrIfStatement) {
        return checkTailIfStatement(((GrIfStatement) last), returnStatements);
    }
    return false;
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 12 with GrIfStatement

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

the class GrIfConditionFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrIfStatement) {
        final Document doc = editor.getDocument();
        final GrIfStatement ifStatement = (GrIfStatement) psiElement;
        final PsiElement rParen = ifStatement.getRParenth();
        final PsiElement lParen = ifStatement.getLParenth();
        final GrExpression condition = ifStatement.getCondition();
        if (condition == null) {
            if (lParen == null || rParen == null) {
                int stopOffset = doc.getLineEndOffset(doc.getLineNumber(ifStatement.getTextRange().getStartOffset()));
                final GrStatement then = ifStatement.getThenBranch();
                if (then != null) {
                    stopOffset = Math.min(stopOffset, then.getTextRange().getStartOffset());
                }
                stopOffset = Math.min(stopOffset, ifStatement.getTextRange().getEndOffset());
                doc.replaceString(ifStatement.getTextRange().getStartOffset(), stopOffset, "if ()");
                processor.registerUnresolvedError(ifStatement.getTextRange().getStartOffset() + "if (".length());
            } else {
                processor.registerUnresolvedError(lParen.getTextRange().getEndOffset());
            }
        } else if (rParen == null) {
            doc.insertString(condition.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 13 with GrIfStatement

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

the class GroovyIfStatementWithTooManyBranchesInspectionBase method buildErrorString.

@Override
protected String buildErrorString(Object... args) {
    final GrIfStatement statement = (GrIfStatement) args[0];
    final int branches = calculateNumBranches(statement);
    return "'#ref' statement with too many branches (" + branches + ") #loc";
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)

Example 14 with GrIfStatement

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

the class FlipIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement ifStatement = DefaultGroovyMethods.asType(element.getParent(), GrIfStatement.class);
    final GrIfStatement elseIf = getElseIf(ifStatement);
    final GrIfStatement elseIfCopy = DefaultGroovyMethods.asType(elseIf.copy(), GrIfStatement.class);
    elseIf.getCondition().replaceWithExpression(ifStatement.getCondition(), true);
    elseIf.getThenBranch().replaceWithStatement(ifStatement.getThenBranch());
    ifStatement.getCondition().replaceWithExpression(elseIfCopy.getCondition(), true);
    ifStatement.getThenBranch().replaceWithStatement(elseIfCopy.getThenBranch());
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)

Example 15 with GrIfStatement

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

the class FlipIfIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!element.getNode().getElementType().equals(GroovyTokenTypes.kIF))
                return false;
            if (!(element.getParent() instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = DefaultGroovyMethods.asType(element.getParent(), GrIfStatement.class);
            final GrIfStatement elseIf = getElseIf(ifStatement);
            return elseIf != null && checkIf(ifStatement) && checkIf(elseIf);
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)17 PsiElement (com.intellij.psi.PsiElement)11 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 TextRange (com.intellij.openapi.util.TextRange)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 NotNull (org.jetbrains.annotations.NotNull)3 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)3 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)3 Document (com.intellij.openapi.editor.Document)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrConditionalExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrCondition (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition)1