Search in sources :

Example 21 with GrReturnStatement

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

the class EachToForIntention method updateReturnStatements.

private static GrForStatement updateReturnStatements(GrForStatement forStatement) {
    GrStatement body = forStatement.getBody();
    assert body != null;
    final Set<String> usedLabels = ContainerUtil.newHashSet();
    final Ref<Boolean> needLabel = Ref.create(false);
    body.accept(new GroovyRecursiveElementVisitor() {

        private int myLoops = 0;

        @Override
        public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
            if (returnStatement.getReturnValue() != null)
                return;
            if (myLoops > 0)
                needLabel.set(true);
        }

        @Override
        public void visitLabeledStatement(@NotNull GrLabeledStatement labeledStatement) {
            super.visitLabeledStatement(labeledStatement);
            usedLabels.add(labeledStatement.getName());
        }

        @Override
        public void visitForStatement(@NotNull GrForStatement forStatement) {
            myLoops++;
            super.visitForStatement(forStatement);
            myLoops--;
        }

        @Override
        public void visitWhileStatement(@NotNull GrWhileStatement whileStatement) {
            myLoops++;
            super.visitWhileStatement(whileStatement);
            myLoops--;
        }

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
        //don't go into closures
        }

        @Override
        public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
        //don't go into anonymous
        }
    });
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(forStatement.getProject());
    final String continueText;
    if (needLabel.get()) {
        int i = 0;
        String label = OUTER;
        while (usedLabels.contains(label)) {
            label = OUTER + i;
            i++;
        }
        continueText = "continue " + label;
        GrLabeledStatement labeled = (GrLabeledStatement) factory.createStatementFromText(label + ": while (true){}");
        labeled.getStatement().replaceWithStatement(forStatement);
        labeled = forStatement.replaceWithStatement(labeled);
        forStatement = (GrForStatement) labeled.getStatement();
        body = forStatement.getBody();
        assert body != null;
    } else {
        continueText = "continue";
    }
    final GrStatement continueStatement = factory.createStatementFromText(continueText);
    body.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
            if (returnStatement.getReturnValue() == null) {
                returnStatement.replaceWithStatement(continueStatement);
            }
        }

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
        //don't go into closures
        }

        @Override
        public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
        //don't go into anonymous
        }
    });
    return forStatement;
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

Example 22 with GrReturnStatement

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

the class ExpandBooleanPredicate method isBooleanReturn.

public static boolean isBooleanReturn(GrStatement statement) {
    if (!(statement instanceof GrReturnStatement)) {
        return false;
    }
    final GrReturnStatement returnStatement = (GrReturnStatement) statement;
    final GrExpression returnValue = returnStatement.getReturnValue();
    if (returnValue == null) {
        return false;
    }
    if (returnValue instanceof GrLiteral) {
        return false;
    }
    final PsiType returnType = returnValue.getType();
    if (returnType == null) {
        return false;
    }
    return returnType.equals(PsiType.BOOLEAN) || returnType.equalsToText("java.lang.Boolean");
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 23 with GrReturnStatement

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

the class ReplaceIfWithTernaryIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement ifStatement = (GrIfStatement) element.getParent();
    final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
    final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
    if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression) {
        final GrAssignmentExpression assignment = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(project).createStatementFromText("a = b ? c : d");
        assignment.getLValue().replaceWithExpression(((GrAssignmentExpression) thenBranch).getLValue(), true);
        final GrConditionalExpression conditional = (GrConditionalExpression) assignment.getRValue();
        replaceConditional(conditional, ifStatement.getCondition(), ((GrAssignmentExpression) thenBranch).getRValue(), ((GrAssignmentExpression) elseBranch).getRValue());
        ifStatement.replaceWithStatement(assignment);
    }
    if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement) {
        final GrReturnStatement returnSt = (GrReturnStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("return a ? b : c");
        final GrConditionalExpression conditional = (GrConditionalExpression) returnSt.getReturnValue();
        replaceConditional(conditional, ifStatement.getCondition(), ((GrReturnStatement) thenBranch).getReturnValue(), ((GrReturnStatement) elseBranch).getReturnValue());
        ifStatement.replaceWithStatement(returnSt);
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement)

Example 24 with GrReturnStatement

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

the class ReplaceTernaryWithIfElseIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrConditionalExpression parentTernary = findTernary(element);
    GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
    GrReturnStatement parentReturn = (GrReturnStatement) parentTernary.getParent();
    String condition = parentTernary.getCondition().getText();
    GrExpression thenBranch = parentTernary.getThenBranch();
    String thenText = thenBranch != null ? thenBranch.getText() : "";
    GrExpression elseBranch = parentTernary.getElseBranch();
    String elseText = elseBranch != null ? elseBranch.getText() : "";
    String text = "if (" + condition + ") { \nreturn " + thenText + "\n} else {\n return " + elseText + "\n}";
    GrIfStatement ifStatement = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(text);
    ifStatement = parentReturn.replaceWithStatement(ifStatement);
    editor.getCaretModel().moveToOffset(ifStatement.getRParenth().getTextRange().getEndOffset());
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)

Example 25 with GrReturnStatement

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

the class MethodTypeInferencer method compute.

@Override
@Nullable
public PsiType compute() {
    List<GrStatement> returns = ControlFlowUtils.collectReturns(myBlock);
    if (returns.isEmpty())
        return PsiType.VOID;
    PsiType result = null;
    PsiManager manager = myBlock.getManager();
    for (GrStatement returnStatement : returns) {
        GrExpression value = null;
        if (returnStatement instanceof GrReturnStatement) {
            value = ((GrReturnStatement) returnStatement).getReturnValue();
        } else if (returnStatement instanceof GrExpression) {
            value = (GrExpression) returnStatement;
        }
        if (value != null) {
            result = TypesUtil.getLeastUpperBoundNullable(result, value.getType(), manager);
        }
    }
    return result;
}
Also used : PsiManager(com.intellij.psi.PsiManager) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)27 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)16 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)5 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)5 PsiType (com.intellij.psi.PsiType)4 NotNull (org.jetbrains.annotations.NotNull)4 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrThrowStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement)3 GrVariableDeclarationOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)2