[groovy] allow to initialize introduced field within current method if single expression occurence was selected

This commit is contained in:
Daniil Ovchinnikov
2016-02-17 20:32:27 +03:00
parent 1e38545259
commit e2f75c5f64
3 changed files with 96 additions and 19 deletions
@@ -252,8 +252,11 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
}
private EnumSet<GrIntroduceFieldSettings.Init> getApplicableInitPlaces() {
GrIntroduceContext context = getContext();
PsiElement[] occurrences = getOccurrences();
return getApplicableInitPlaces(getContext(), isReplaceAllOccurrences());
}
public static EnumSet<GrIntroduceFieldSettings.Init> getApplicableInitPlaces(GrIntroduceContext context,
boolean replaceAllOccurrences) {
EnumSet<GrIntroduceFieldSettings.Init> result = EnumSet.noneOf(GrIntroduceFieldSettings.Init.class);
if (context.getExpression() != null ||
@@ -268,7 +271,8 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
PsiElement scope = context.getScope();
if (isReplaceAllOccurrences() || occurrences.length == 1) {
if (replaceAllOccurrences || context.getExpression() != null) {
PsiElement[] occurrences = replaceAllOccurrences ? context.getOccurrences() : new PsiElement[]{context.getExpression()};
PsiElement parent = PsiTreeUtil.findCommonParent(occurrences);
PsiElement container = GrIntroduceHandlerBase.getEnclosingContainer(parent);
if (container != null && PsiTreeUtil.isAncestor(scope, container, false)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiModifier
import com.intellij.psi.PsiType
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.util.VisibilityUtil
import org.jetbrains.annotations.NotNull
@@ -93,9 +94,9 @@ public class IntroduceConstantTest extends LightCodeInsightFixtureTestCase {
final GrIntroduceConstantHandler handler = new GrIntroduceConstantHandler();
final Editor editor = myFixture.getEditor();
final GrExpression expression = findExpression();
final GrVariable variable = findVariable();
final StringPartInfo stringPart = findStringPart();
final GrExpression expression = findExpression(myFixture);
final GrVariable variable = findVariable(myFixture);
final StringPartInfo stringPart = findStringPart(myFixture);
PsiElement[] scopes = handler.findPossibleScopes(expression, variable, stringPart, editor);
final GrIntroduceContext context = handler.getContext(getProject(), editor, expression, variable, stringPart, scopes[0]);
@@ -123,27 +124,27 @@ public class IntroduceConstantTest extends LightCodeInsightFixtureTestCase {
}
@Nullable
private GrVariable findVariable() {
final Editor editor = myFixture.getEditor();
public static GrVariable findVariable(JavaCodeInsightTestFixture fixture) {
final Editor editor = fixture.getEditor();
final int start = editor.getSelectionModel().getSelectionStart();
final int end = editor.getSelectionModel().getSelectionEnd();
return GrIntroduceHandlerBase.findVariable(myFixture.getFile(), start, end);
return GrIntroduceHandlerBase.findVariable(fixture.getFile(), start, end);
}
@Nullable
private GrExpression findExpression() {
final Editor editor = myFixture.getEditor();
public static GrExpression findExpression(JavaCodeInsightTestFixture fixture) {
final Editor editor = fixture.getEditor();
final int start = editor.getSelectionModel().getSelectionStart();
final int end = editor.getSelectionModel().getSelectionEnd();
return GrIntroduceHandlerBase.findExpression(myFixture.getFile(), start, end);
return GrIntroduceHandlerBase.findExpression(fixture.getFile(), start, end);
}
@Nullable
private StringPartInfo findStringPart() {
final Editor editor = myFixture.getEditor();
public static StringPartInfo findStringPart(JavaCodeInsightTestFixture fixture) {
final Editor editor = fixture.getEditor();
final int start = editor.getSelectionModel().getSelectionStart();
final int end = editor.getSelectionModel().getSelectionEnd();
return StringPartInfo.findStringPart(myFixture.getFile(), start, end);
return StringPartInfo.findStringPart(fixture.getFile(), start, end);
}
private static class MockIntroduceConstantSettings implements GrIntroduceConstantSettings {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,18 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.refactoring.introduce.field;
package org.jetbrains.plugins.groovy.refactoring.introduce.field
import com.intellij.openapi.application.WriteAction
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiType
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser.ReplaceChoice
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
import org.jetbrains.plugins.groovy.GroovyFileType
import org.jetbrains.plugins.groovy.LightGroovyTestCase
import org.jetbrains.plugins.groovy.refactoring.introduce.IntroduceConstantTest
import org.jetbrains.plugins.groovy.refactoring.introduce.field.GrIntroduceFieldSettings.Init
import org.jetbrains.plugins.groovy.util.TestUtils
import static com.intellij.refactoring.introduce.inplace.OccurrencesChooser.ReplaceChoice.ALL
import static org.jetbrains.plugins.groovy.refactoring.introduce.field.GrIntroduceFieldSettings.Init.*
/**
@@ -540,6 +544,58 @@ class TestClass {
''', false, false, false, CONSTRUCTOR
}
void 'test cannot initialize in current method when introducing from field initializer'() {
doTestInitInTarget '''
class A {
def object = <selection>new Object()</selection>
}
''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION)
doTestInitInTarget '''
class A {
def object = <selection>new Object()</selection>
def object2 = new Object()
}
''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION)
doTestInitInTarget '''
class A {
def object = <selection>new Object()</selection>
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 = <selection>new Object()</selection>
}
}
''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION)
}
void 'test can initialize in current method from within method'() {
doTestInitInTarget '''
class A {
def foo() {
def a = <selection>new Object()</selection>
}
}
''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION, CUR_METHOD)
doTestInitInTarget '''
class A {
def field = new Object()
def foo() {
def a = <selection>new Object()</selection>
}
}
''', 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<Init> 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
}
}