extract constant: allow class-constants to be used from enum constant initializer outside argument list (IDEA-161237)

This commit is contained in:
Anna Kozlova
2016-09-15 13:55:48 +03:00
parent 7f76d7a101
commit ca0109df46
4 changed files with 27 additions and 1 deletions
@@ -737,7 +737,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
}
PsiMember anchorMember = finalAnchorElement instanceof PsiMember ? (PsiMember)finalAnchorElement : null;
if (anchorMember instanceof PsiEnumConstant && destClass == anchorMember.getContainingClass()) {
if (anchorMember instanceof PsiEnumConstant && destClass == anchorMember.getContainingClass() &&
PsiTreeUtil.isAncestor(((PsiEnumConstant)anchorMember).getArgumentList(), initializer, false)) {
final String initialName = "Constants";
String constantsClassName = initialName;
@@ -0,0 +1,9 @@
enum Foo {
FOO1 {
public String bar() {
return <selection>"bar"</selection>;
}
}
}
@@ -0,0 +1,10 @@
enum Foo {
FOO1 {
public String bar() {
return xxx;
}
};
public static final String xxx = "bar";
}
@@ -48,6 +48,12 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testFromEnumConstantInitializer2() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testEnumConstant() throws Exception {
doTest(true);
}