Search in sources :

Example 51 with GrVariable

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

the class GrInplaceParameterIntroducer method getInitialSettingsForInplace.

@Nullable
@Override
protected GrIntroduceParameterSettings getInitialSettingsForInplace(@NotNull GrIntroduceContext context, @NotNull OccurrencesChooser.ReplaceChoice choice, String[] names) {
    GrExpression expression = context.getExpression();
    GrVariable var = context.getVar();
    PsiType type = var != null ? var.getDeclaredType() : expression != null ? expression.getType() : null;
    return new GrIntroduceExpressionSettingsImpl(myInfo, names[0], false, new TIntArrayList(), false, IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, expression, var, type, false, false, false);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TIntArrayList(gnu.trove.TIntArrayList) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with GrVariable

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

the class GrIntroduceParameterDialog method suggestNames.

@NotNull
public LinkedHashSet<String> suggestNames() {
    GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
    GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
    StringPartInfo stringPart = findStringPart();
    return GroovyIntroduceParameterUtil.suggestNames(var, expr, stringPart, myInfo.getToReplaceIn(), myProject);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with GrVariable

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

the class GroovyInlineLocalProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
    final GrExpression initializer = mySettings.getInitializer();
    GrExpression initializerToUse = GrIntroduceHandlerBase.insertExplicitCastIfNeeded(myLocal, mySettings.getInitializer());
    for (UsageInfo usage : usages) {
        GrVariableInliner.inlineReference(usage, myLocal, initializerToUse);
    }
    final PsiElement initializerParent = initializer.getParent();
    if (initializerParent instanceof GrAssignmentExpression) {
        initializerParent.delete();
        return;
    }
    if (initializerParent instanceof GrVariable) {
        final Collection<PsiReference> all = ReferencesSearch.search(myLocal).findAll();
        if (!all.isEmpty()) {
            initializer.delete();
            return;
        }
    }
    final PsiElement owner = myLocal.getParent().getParent();
    if (owner instanceof GrVariableDeclarationOwner) {
        ((GrVariableDeclarationOwner) owner).removeVariable(myLocal);
    } else {
        myLocal.delete();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiReference(com.intellij.psi.PsiReference) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 54 with GrVariable

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

the class GroovyInlineLocalProcessor method collectRefs.

private static void collectRefs(final GrVariable variable, Instruction[] flow, final List<BitSet> writes, final int writeInstructionNumber, final ArrayList<UsageInfo> toInline) {
    for (Instruction instruction : flow) {
        final PsiElement element = instruction.getElement();
        if (instruction instanceof ReadWriteVariableInstruction) {
            if (((ReadWriteVariableInstruction) instruction).isWrite())
                continue;
            if (element instanceof GrVariable && element != variable)
                continue;
            if (!(element instanceof GrReferenceExpression))
                continue;
            final GrReferenceExpression ref = (GrReferenceExpression) element;
            if (ref.isQualified() || ref.resolve() != variable)
                continue;
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber)) {
                toInline.add(new UsageInfo(ref));
            } else if (writeInstructionNumber == -1 && prev.cardinality() == 0) {
                toInline.add(new ClosureUsage(ref));
            }
        } else if (element instanceof GrClosableBlock) {
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
                final Instruction[] closureFlow = ((GrClosableBlock) element).getControlFlow();
                collectRefs(variable, closureFlow, ControlFlowUtils.inferWriteAccessMap(closureFlow, variable), -1, toInline);
            }
        } else if (element instanceof GrAnonymousClassDefinition) {
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
                ((GrAnonymousClassDefinition) element).acceptChildren(new GroovyRecursiveElementVisitor() {

                    @Override
                    public void visitField(@NotNull GrField field) {
                        GrExpression initializer = field.getInitializerGroovy();
                        if (initializer != null) {
                            Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
                            collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                        }
                    }

                    @Override
                    public void visitMethod(@NotNull GrMethod method) {
                        GrOpenBlock block = method.getBlock();
                        if (block != null) {
                            Instruction[] flow = block.getControlFlow();
                            collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                        }
                    }

                    @Override
                    public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
                        GrOpenBlock block = initializer.getBlock();
                        Instruction[] flow = block.getControlFlow();
                        collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                    }
                });
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) BitSet(java.util.BitSet) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ControlFlowBuilder(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 55 with GrVariable

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

the class GrInplaceFieldIntroducer method getInitialSettingsForInplace.

@Nullable
@Override
protected GrIntroduceFieldSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
    return new GrIntroduceFieldSettings() {

        @Override
        public boolean declareFinal() {
            return false;
        }

        @Override
        public Init initializeIn() {
            return Init.FIELD_DECLARATION;
        }

        @Override
        public String getVisibilityModifier() {
            return PsiModifier.PRIVATE;
        }

        @Override
        public boolean isStatic() {
            boolean hasInstanceInScope = true;
            PsiClass clazz = (PsiClass) context.getScope();
            if (replaceAllOccurrences()) {
                for (PsiElement occurrence : context.getOccurrences()) {
                    if (!PsiUtil.hasEnclosingInstanceInScope(clazz, occurrence, false)) {
                        hasInstanceInScope = false;
                        break;
                    }
                }
            } else if (context.getExpression() != null) {
                hasInstanceInScope = PsiUtil.hasEnclosingInstanceInScope(clazz, context.getExpression(), false);
            } else if (context.getStringPart() != null) {
                hasInstanceInScope = PsiUtil.hasEnclosingInstanceInScope(clazz, context.getStringPart().getLiteral(), false);
            }
            return !hasInstanceInScope;
        }

        @Override
        public boolean removeLocalVar() {
            return myLocalVar != null;
        }

        @Nullable
        @Override
        public String getName() {
            return names[0];
        }

        @Override
        public boolean replaceAllOccurrences() {
            return context.getVar() != null || choice == OccurrencesChooser.ReplaceChoice.ALL;
        }

        @Nullable
        @Override
        public PsiType getSelectedType() {
            GrExpression expression = context.getExpression();
            GrVariable var = context.getVar();
            StringPartInfo stringPart = context.getStringPart();
            return var != null ? var.getDeclaredType() : expression != null ? expression.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5