Search in sources :

Example 1 with GrStatementOwner

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

the class GrIntroduceVariableHandler method addVariable.

private static GrVariable addVariable(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
    GrStatement anchor = findAnchor(context, settings.replaceAllOccurrences());
    PsiElement parent = anchor.getParent();
    assert parent instanceof GrStatementOwner;
    GrVariableDeclaration generated = generateDeclaration(context, settings);
    GrStatement declaration = ((GrStatementOwner) parent).addStatementBefore(generated, anchor);
    declaration = (GrStatement) JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(declaration);
    return ((GrVariableDeclaration) declaration).getVariables()[0];
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrStatementOwner

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

the class GrIntroduceClosureParameterProcessor method insertDeclaration.

private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
    if (original instanceof GrField) {
        final PsiClass containingClass = ((GrField) original).getContainingClass();
        LOG.assertTrue(containingClass != null);
        return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
    }
    final GrStatementOwner block;
    if (original instanceof PsiParameter) {
        final PsiElement container = original.getParent().getParent();
        if (container instanceof GrMethod) {
            block = ((GrMethod) container).getBlock();
        } else if (container instanceof GrClosableBlock) {
            block = (GrCodeBlock) container;
        } else if (container instanceof GrForStatement) {
            final GrStatement body = ((GrForStatement) container).getBody();
            if (body instanceof GrBlockStatement) {
                block = ((GrBlockStatement) body).getBlock();
            } else {
                GrBlockStatement blockStatement = myFactory.createBlockStatement();
                LOG.assertTrue(blockStatement != null);
                if (body != null) {
                    blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
                    blockStatement = (GrBlockStatement) body.replace(blockStatement);
                } else {
                    blockStatement = (GrBlockStatement) container.add(blockStatement);
                }
                block = blockStatement.getBlock();
            }
        } else {
            throw new IncorrectOperationException();
        }
        LOG.assertTrue(block != null);
        return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
    }
    PsiElement parent = original.getParent();
    LOG.assertTrue(parent instanceof GrVariableDeclaration);
    final PsiElement pparent = parent.getParent();
    if (pparent instanceof GrIfStatement) {
        if (((GrIfStatement) pparent).getThenBranch() == parent) {
            block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
        } else {
            block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
        }
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else if (pparent instanceof GrLoopStatement) {
        block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else {
        LOG.assertTrue(pparent instanceof GrStatementOwner);
        block = (GrStatementOwner) pparent;
    }
    return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 3 with GrStatementOwner

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

the class GrIntroduceLocalVariableProcessor method insertVariableDefinition.

@NotNull
private GrVariable insertVariableDefinition(@NotNull GrVariableDeclaration declaration, @NotNull GrStatement anchor, @Nullable PsiElement expression) throws IncorrectOperationException {
    GrLabeledStatement labeledStatement = expression != null && expression.getParent() instanceof GrLabeledStatement ? (GrLabeledStatement) expression.getParent() : null;
    boolean expressionMustBeDeleted = expression != null && PsiUtil.isExpressionStatement(expression) && !isSingleGStringInjectionExpr(expression);
    boolean anchorEqualsExpression = anchor == expression || labeledStatement == anchor;
    String usedLabel = labeledStatement != null ? labeledStatement.getName() : null;
    if (expressionMustBeDeleted && !anchorEqualsExpression) {
        expression.delete();
    }
    boolean isInsideControlStatement = isControlStatementBranch(anchor);
    if (isInsideControlStatement) {
        anchor = insertBraces(anchor);
    }
    LOG.assertTrue(myOccurrences.length > 0);
    GrStatementOwner block = (GrStatementOwner) anchor.getParent();
    if (usedLabel != null && expressionMustBeDeleted && anchorEqualsExpression) {
        GrLabeledStatement definitionWithLabel = (GrLabeledStatement) GroovyPsiElementFactory.getInstance(anchor.getProject()).createStatementFromText(usedLabel + ": foo()");
        GrLabeledStatement inserted = insertStatement(definitionWithLabel, anchor, block, true);
        declaration = inserted.getStatement().replaceWithStatement(declaration);
    } else {
        declaration = insertStatement(declaration, anchor, block, expressionMustBeDeleted && anchorEqualsExpression);
    }
    final GrVariable variable = declaration.getVariables()[0];
    JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
    PsiElement markerPlace = expressionMustBeDeleted ? variable : isInsideControlStatement ? declaration.getParent() : expression;
    refreshPositionMarker(markerPlace);
    return variable;
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GrStatementOwner

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

the class ExtractClosureFromClosureProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    GrIntroduceClosureParameterProcessor.processExternalUsages(usages, myHelper, generateClosure(myHelper));
    GrIntroduceClosureParameterProcessor.processClosure(usages, myHelper);
    GrStatementOwner declarationOwner = GroovyRefactoringUtil.getDeclarationOwner(myHelper.getStatements()[0]);
    ExtractUtil.replaceStatement(declarationOwner, myHelper);
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Example 5 with GrStatementOwner

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

the class GrIntroduceFieldProcessor method initializeInMethod.

void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
    final PsiElement _scope = myContext.getScope();
    final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
    final PsiElement place = replaced.get(0);
    final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
    GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
    assert container != null;
    final PsiElement anchor;
    if (mySettings.removeLocalVar()) {
        GrVariable variable = myLocalVariable;
        anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
    } else {
        anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
        GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
    }
    initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)38 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)11 PsiElement (com.intellij.psi.PsiElement)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 File (java.io.File)4 List (java.util.List)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 ImmutableList (com.google.common.collect.ImmutableList)3 GrControlStatement (org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)3 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)3 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)2 ValueFactory (com.android.tools.idea.gradle.parser.ValueFactory)2 ASTNode (com.intellij.lang.ASTNode)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 NotNull (org.jetbrains.annotations.NotNull)2