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);
}
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);
}
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();
}
}
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);
}
});
}
}
}
}
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;
}
};
}
Aggregations