Search in sources :

Example 21 with GrIfStatement

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

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

the class GroovyElseUnwrapperBase method collectElementsToIgnore.

@Override
public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
    PsiElement parent = element.getParent();
    while (parent instanceof GrIfStatement) {
        result.add(parent);
        parent = parent.getParent();
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) PsiElement(com.intellij.psi.PsiElement)

Example 23 with GrIfStatement

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

the class GroovyIfUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    GrStatement then = ((GrIfStatement) element).getThenBranch();
    context.extractFromBlockOrSingleStatement(then, element);
    context.delete(element);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 24 with GrIfStatement

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

the class IfElseExprSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
    GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("if(a){4\n} else{\n}", context);
    replaceToOldExpression(ifStatement.getCondition(), expression);
    ifStatement = expression.replaceWithStatement(ifStatement);
    GrStatement psiElement = ifStatement.getThenBranch();
    assert psiElement instanceof GrBlockStatement;
    GrStatement[] statements = ((GrBlockStatement) psiElement).getBlock().getStatements();
    assert statements.length > 0;
    GrStatement statement = statements[0];
    int endOffset = statement.getTextRange().getStartOffset();
    statement.getNode().getTreeParent().removeChild(statement.getNode());
    return new TextRange(endOffset, endOffset);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) TextRange(com.intellij.openapi.util.TextRange) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 25 with GrIfStatement

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

the class GrMissingIfStatement method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrIfStatement))
        return;
    GrIfStatement ifStatement = (GrIfStatement) psiElement;
    final Document document = editor.getDocument();
    final GrStatement elseBranch = ifStatement.getElseBranch();
    final PsiElement elseElement = ifStatement.getElseKeyword();
    if (elseElement != null && (elseBranch == null || !(elseBranch instanceof GrBlockStatement) && GrForBodyFixer.startLine(editor.getDocument(), elseBranch) > GrForBodyFixer.startLine(editor.getDocument(), elseElement))) {
        document.insertString(elseElement.getTextRange().getEndOffset(), "{}");
    }
    GrStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch instanceof GrBlockStatement)
        return;
    boolean transformingOneLiner = false;
    if (thenBranch != null && GrForBodyFixer.startLine(editor.getDocument(), thenBranch) == GrForBodyFixer.startLine(editor.getDocument(), ifStatement)) {
        if (ifStatement.getCondition() != null) {
            return;
        }
        transformingOneLiner = true;
    }
    final PsiElement rParenth = ifStatement.getRParenth();
    if (rParenth == null)
        return;
    if (elseBranch == null && !transformingOneLiner || thenBranch == null) {
        document.insertString(rParenth.getTextRange().getEndOffset(), "{}");
    } else {
        document.insertString(rParenth.getTextRange().getEndOffset(), "{");
        document.insertString(thenBranch.getTextRange().getEndOffset() + 1, "}");
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) Document(com.intellij.openapi.editor.Document) 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

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