IDEA-110721 Groovy: introduce field from var, static checking

This commit is contained in:
Max Medvedev
2013-07-18 16:09:03 +04:00
parent 4b5d1b2e24
commit d5a751f246
6 changed files with 33 additions and 24 deletions
@@ -48,7 +48,4 @@ public interface GrIntroduceContext {
@NotNull
PsiElement getPlace();
@NotNull
PsiElement getElementToIntroduce();
}
@@ -96,12 +96,4 @@ public class GrIntroduceContextImpl implements GrIntroduceContext {
public PsiElement getPlace() {
return myPlace;
}
@NotNull
@Override
public PsiElement getElementToIntroduce() {
return myExpression != null ? myExpression :
myVar != null ? myVar :
myStringPart.getLiteral();
}
}
@@ -325,7 +325,7 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
final Settings settings = isInplace ? getSettingsForInplace(context, choice) : showDialog(context);
if (settings == null) return;
CommandProcessor.getInstance().executeCommand(context.getProject(), new Runnable() {
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
public void run() {
List<RangeMarker> occurrences = ContainerUtil.newArrayList();
Document document = editor.getDocument();
@@ -345,7 +345,7 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
if (isInplace && var != null) {
InplaceVariableIntroducer<PsiElement> introducer = getIntroducer(var, context, settings, occurrences, varRangeMarker, expressionRangeMarker, stringPartRangeMarker);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(context.getEditor().getDocument());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
introducer.performInplaceRefactoring(getDialog(context).suggestNames());
}
}
@@ -405,7 +405,7 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
final RefactoringSupportProvider supportProvider = LanguageRefactoringSupport.INSTANCE.forLanguage(context.getPlace().getLanguage());
return supportProvider != null &&
context.getEditor().getSettings().isVariableInplaceRenameEnabled() &&
supportProvider.isInplaceIntroduceAvailable(context.getElementToIntroduce(), context.getPlace()) &&
supportProvider.isInplaceIntroduceAvailable(context.getPlace(), context.getPlace()) &&
!ApplicationManager.getApplication().isUnitTestMode();
}
@@ -525,7 +525,7 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
}
if (candidate == null) return null;
if ((container instanceof GrWhileStatement) &&
candidate.equals(((GrWhileStatement)container).getCondition())) {
return container;
@@ -69,7 +69,7 @@ public class GrInplaceFieldIntroducer extends GrInplaceIntroducer {
@Nullable RangeMarker expressionRangeMarker,
@Nullable RangeMarker stringPartRangeMarker,
GrExpression initializer) {
super(var, context.getEditor(), context.getProject(), IntroduceFieldHandler.REFACTORING_NAME, occurrences, context.getElementToIntroduce());
super(var, context.getEditor(), context.getProject(), IntroduceFieldHandler.REFACTORING_NAME, occurrences, context.getPlace());
myContext = context;
myReplaceAll = replaceAll;
@@ -167,7 +167,7 @@ public class GrInplaceFieldIntroducer extends GrInplaceIntroducer {
@Override
public boolean isStatic() {
return false;
return getVariable().hasModifierProperty(PsiModifier.STATIC);
}
@Override
@@ -122,7 +122,10 @@ public class GrIntroduceFieldHandler extends GrIntroduceHandlerBase<GrIntroduceF
@NotNull List<RangeMarker> occurrenceMarkers,
RangeMarker varRangeMarker, @Nullable RangeMarker expressionRangeMarker,
@Nullable RangeMarker stringPartRangeMarker) {
if (expressionRangeMarker != null) {
if (varRangeMarker != null) {
context.getEditor().getCaretModel().moveToOffset(var.getNameIdentifierGroovy().getTextRange().getStartOffset());
}
else if (expressionRangeMarker != null) {
context.getEditor().getCaretModel().moveToOffset(expressionRangeMarker.getStartOffset());
}
else if (stringPartRangeMarker != null) {
@@ -178,12 +181,29 @@ public class GrIntroduceFieldHandler extends GrIntroduceHandlerBase<GrIntroduceF
@Override
public boolean isStatic() {
return false;
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 false;
return context.getVar() != null;
}
@Nullable
@@ -194,7 +214,7 @@ public class GrIntroduceFieldHandler extends GrIntroduceHandlerBase<GrIntroduceF
@Override
public boolean replaceAllOccurrences() {
return choice == OccurrencesChooser.ReplaceChoice.ALL;
return context.getVar() != null || choice == OccurrencesChooser.ReplaceChoice.ALL;
}
@Nullable
@@ -203,8 +223,8 @@ public class GrIntroduceFieldHandler extends GrIntroduceHandlerBase<GrIntroduceF
GrExpression expression = context.getExpression();
GrVariable var = context.getVar();
StringPartInfo stringPart = context.getStringPart();
return expression != null ? expression.getType() :
var != null ? var.getType() :
return var != null ? var.getDeclaredType() :
expression != null ? expression.getType() :
stringPart != null ? stringPart.getLiteral().getType() :
null;
}
@@ -178,7 +178,7 @@ public class GrIntroduceFieldProcessor {
final PsiElement _scope = context.getScope();
final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass)_scope).getContainingFile() : _scope;
final PsiElement place = context.getElementToIntroduce();
final PsiElement place = context.getPlace();
final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
LOG.assertTrue(member != null);