Groovy: in-place introduce field from reference to local var

This commit is contained in:
Max Medvedev
2014-01-31 16:04:16 +04:00
parent fa3a51c9d9
commit 25d5d2e932
3 changed files with 42 additions and 30 deletions
@@ -741,19 +741,29 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
}
}
@NotNull
@Nullable
public static GrVariable resolveLocalVar(@NotNull GrIntroduceContext context) {
final GrVariable var = context.getVar();
if (var != null) {
return var;
}
final GrReferenceExpression expression = (GrReferenceExpression)context.getExpression();
assert expression != null;
return resolveLocalVar(context.getExpression());
}
final PsiElement resolved = expression.resolve();
assert resolved instanceof GrVariable : resolved;
return (GrVariable)resolved;
@Nullable
private static GrVariable resolveLocalVar(@Nullable GrExpression expression) {
if (expression instanceof GrReferenceExpression) {
final GrReferenceExpression ref = (GrReferenceExpression)expression;
final PsiElement resolved = ref.resolve();
if (GroovyRefactoringUtil.isLocalVariable(resolved)) {
return (GrVariable)resolved;
}
return null;
}
return null;
}
public static boolean hasLhs(@NotNull final PsiElement[] occurrences) {
@@ -53,6 +53,29 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
private final GrFinalListener finalListener;
private String[] mySuggestedNames;
private boolean myIsStatic;
private final GrVariable myLocalVar;
public GrInplaceFieldIntroducer(GrIntroduceContext context, OccurrencesChooser.ReplaceChoice choice) {
super(IntroduceFieldHandler.REFACTORING_NAME, choice, context);
finalListener = new GrFinalListener(myEditor);
myLocalVar = GrIntroduceHandlerBase.resolveLocalVar(context);
if (myLocalVar != null) {
//myLocalVariable = myLocalVar;
ArrayList<String> result = ContainerUtil.newArrayList(myLocalVar.getName());
GrExpression initializer = myLocalVar.getInitializerGroovy();
if (initializer != null) {
ContainerUtil.addAll(result, GroovyNameSuggestionUtil.suggestVariableNames(initializer, new GroovyInplaceFieldValidator(getContext()), false));
}
mySuggestedNames = ArrayUtil.toStringArray(result);
}
else {
mySuggestedNames = GroovyNameSuggestionUtil.suggestVariableNames(context.getExpression(), new GroovyInplaceFieldValidator(getContext()), false);
}
myApplicablePlaces = getApplicableInitPlaces();
}
@Nullable
@Override
@@ -72,28 +95,6 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
}
}
public GrInplaceFieldIntroducer(GrIntroduceContext context, OccurrencesChooser.ReplaceChoice choice) {
super(IntroduceFieldHandler.REFACTORING_NAME, choice, context);
finalListener = new GrFinalListener(myEditor);
GrVariable var = context.getVar();
if (var != null) {
ArrayList<String> result = ContainerUtil.newArrayList(var.getName());
GrExpression initializer = var.getInitializerGroovy();
if (initializer != null) {
ContainerUtil.addAll(result, GroovyNameSuggestionUtil.suggestVariableNames(initializer, new GroovyInplaceFieldValidator(getContext()), false));
}
mySuggestedNames = ArrayUtil.toStringArray(result);
}
else {
mySuggestedNames = GroovyNameSuggestionUtil.suggestVariableNames(context.getExpression(), new GroovyInplaceFieldValidator(getContext()), false);
}
myApplicablePlaces = getApplicableInitPlaces();
}
@Override
protected GrVariable runRefactoring(GrIntroduceContext context, GrIntroduceFieldSettings settings, boolean processUsages) {
GrIntroduceFieldProcessor processor = new GrIntroduceFieldProcessor(context, settings);
@@ -146,7 +147,7 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
@Override
public boolean removeLocalVar() {
return context.getVar() != null;
return myLocalVar != null;
}
@Nullable
@@ -199,7 +200,7 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
@Override
public boolean removeLocalVar() {
return getContext().getVar() != null;
return myLocalVar != null;
}
@Nullable
@@ -87,6 +87,7 @@ public class GrIntroduceFieldProcessor {
if (mySettings.removeLocalVar()) {
myLocalVariable = GrIntroduceHandlerBase.resolveLocalVar(myContext);
assert myLocalVariable != null : myContext.getExpression() + ", " + myContext.getVar() + ", " + myContext.getStringPart();
}
myInitializer = (GrExpression)getInitializer().copy();