diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java index 67924344b28c..3f463677f5d3 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java @@ -252,8 +252,11 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer getApplicableInitPlaces() { - GrIntroduceContext context = getContext(); - PsiElement[] occurrences = getOccurrences(); + return getApplicableInitPlaces(getContext(), isReplaceAllOccurrences()); + } + + public static EnumSet getApplicableInitPlaces(GrIntroduceContext context, + boolean replaceAllOccurrences) { EnumSet result = EnumSet.noneOf(GrIntroduceFieldSettings.Init.class); if (context.getExpression() != null || @@ -268,7 +271,8 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducernew Object() +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION) + + doTestInitInTarget ''' +class A { + def object = new Object() + def object2 = new Object() +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION) + + doTestInitInTarget ''' +class A { + def object = new Object() + def object2 = new Object() +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION), ReplaceChoice.NO + } + + void 'test can not initialize in current method with some occurence outside'() { + doTestInitInTarget ''' +class A { + def field = new Object() + def foo() { + def a = new Object() + } +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION) + } + + void 'test can initialize in current method from within method'() { + doTestInitInTarget ''' +class A { + def foo() { + def a = new Object() + } +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION, CUR_METHOD) + + doTestInitInTarget ''' +class A { + def field = new Object() + def foo() { + def a = new Object() + } +} +''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION, CUR_METHOD), ReplaceChoice.NO + } + private void doTest(final boolean isStatic, final boolean removeLocal, final boolean declareFinal, @@ -577,4 +633,20 @@ class TestClass { accessToken.finish() } } + + private void doTestInitInTarget(String text, EnumSet expected = EnumSet.noneOf(Init), ReplaceChoice replaceChoice = ALL) { + myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, text) + def handler = new GrIntroduceFieldHandler() + + def expression = IntroduceConstantTest.findExpression(myFixture) + def variable = IntroduceConstantTest.findVariable(myFixture) + def stringPart = IntroduceConstantTest.findStringPart(myFixture) + def scopes = handler.findPossibleScopes(expression, variable, stringPart, editor) + assert scopes.length == 1 + def scope = scopes[0] + + def context = handler.getContext(getProject(), myFixture.editor, expression, variable, stringPart, scope) + def initPlaces = GrInplaceFieldIntroducer.getApplicableInitPlaces(context, replaceChoice == ALL) + assert initPlaces == expected + } }