Search in sources :

Example 16 with GrIfStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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)

Example 17 with GrIfStatement

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

the class InvertIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement)) {
        throw new IncorrectOperationException("Not invoked on an if");
    }
    GrIfStatement parentIf = (GrIfStatement) parent;
    GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
    GrExpression condition = parentIf.getCondition();
    if (condition == null) {
        throw new IncorrectOperationException("Invoked on an if with empty condition");
    }
    GrExpression negatedCondition = null;
    if (condition instanceof GrUnaryExpression) {
        GrUnaryExpression unaryCondition = (GrUnaryExpression) condition;
        if ("!".equals(unaryCondition.getOperationToken().getText())) {
            negatedCondition = stripParenthesis(unaryCondition.getOperand());
        }
    }
    if (negatedCondition == null) {
        // Now check whether this is a simple expression
        condition = stripParenthesis(condition);
        String negatedExpressionText;
        if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
            negatedExpressionText = "!" + condition.getText();
        } else {
            negatedExpressionText = "!(" + condition.getText() + ")";
        }
        negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
    }
    GrStatement thenBranch = parentIf.getThenBranch();
    final boolean thenIsNotEmpty = isNotEmpty(thenBranch);
    String newIfText = "if (" + negatedCondition.getText() + ") {}";
    if (thenIsNotEmpty) {
        newIfText += " else {}";
    }
    GrIfStatement newIf = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
    generateElseBranchTextAndRemoveTailStatements(parentIf, newIf);
    if (thenIsNotEmpty) {
        final GrStatement elseBranch = newIf.getElseBranch();
        assert elseBranch != null;
        elseBranch.replaceWithStatement(thenBranch);
    }
    parentIf.replace(newIf);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 18 with GrIfStatement

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

the class SplitElseIfIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement parentStatement = (GrIfStatement) element;
    final GrStatement elseBranch = parentStatement.getElseBranch();
    GrIfStatement ifStatement = (GrIfStatement) parentStatement.copy();
    GrBlockStatement blockStatement = GroovyPsiElementFactory.getInstance(project).createBlockStatementFromText("{\nabc()\n}", null);
    GrBlockStatement newBlock = ifStatement.replaceElseBranch(blockStatement);
    newBlock.getBlock().getStatements()[0].replace(elseBranch);
    parentStatement.replaceWithStatement(ifStatement);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 19 with GrIfStatement

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

the class SplitElseIfPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrIfStatement)) {
        return false;
    }
    final GrIfStatement ifStatement = (GrIfStatement) element;
    final GrStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch == null) {
        return false;
    }
    final GrStatement elseBranch = ifStatement.getElseBranch();
    return elseBranch instanceof GrIfStatement;
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 20 with GrIfStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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)

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