Search in sources :

Example 91 with GrStatement

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

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

the class InvertIfIntention method generateElseBranchTextAndRemoveTailStatements.

private static void generateElseBranchTextAndRemoveTailStatements(@NotNull GrIfStatement ifStatement, @NotNull GrIfStatement newIf) {
    final GrStatement thenBranch = newIf.getThenBranch();
    assert thenBranch != null;
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch != null) {
        thenBranch.replaceWithStatement(elseBranch);
        return;
    }
    PsiElement parent = ifStatement.getParent();
    if (!(parent instanceof GrStatementOwner))
        return;
    if (!isTailAfterIf(ifStatement, ((GrStatementOwner) parent)))
        return;
    final PsiElement start = ifStatement.getNextSibling();
    PsiElement end = parent instanceof GrCodeBlock ? ((GrCodeBlock) parent).getRBrace().getPrevSibling() : parent.getLastChild();
    final GrOpenBlock block = ((GrBlockStatement) thenBranch).getBlock();
    block.addRangeAfter(start, end, block.getLBrace());
    parent.deleteChildRange(start, end);
}
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) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 93 with GrStatement

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

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

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

the class OldReferencesResolver method inlineParam.

private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
    if (myParamsToNotInline.contains(parameter))
        return newExpr;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    if (myExpr instanceof GrClosableBlock) {
        int count = 0;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
            count++;
            if (count > 1)
                break;
        }
        if (count > 1) {
            myParamsToNotInline.add(parameter);
            final PsiType type;
            if (parameter instanceof GrParameter) {
                type = ((GrParameter) parameter).getDeclaredType();
            } else {
                type = parameter.getType();
            }
            final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
            final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
            GrStatement anchor = statements.length > 0 ? statements[0] : null;
            return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
        }
    }
    int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
    if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
        actualArg = factory.createExpressionFromText(getTempVar(actualArg));
    }
    newExpr = newExpr.replace(actualArg);
    return newExpr;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6