mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
disable create enum constant when not applicable (IDEA-154293)
This commit is contained in:
@@ -108,14 +108,42 @@ public class CreateEnumConstantFromUsageFix extends CreateVarFromUsageFix implem
|
||||
if (enumClass != null) {
|
||||
return Collections.singletonList(enumClass);
|
||||
}
|
||||
ExpectedTypeInfo[] typeInfos = CreateFromUsageUtils.guessExpectedTypes(myReferenceExpression, false);
|
||||
|
||||
if (myReferenceExpression.isQualified()) {
|
||||
final PsiClass aClass = getTargetClassByExpectedTypes(myReferenceExpression.getQualifierExpression());
|
||||
if (aClass != null) {
|
||||
return Collections.singletonList(aClass);
|
||||
}
|
||||
}
|
||||
else {
|
||||
final PsiClass targetClass = getTargetClassByExpectedTypes(myReferenceExpression);
|
||||
if (targetClass != null) {
|
||||
final PsiFile containingFile = myReferenceExpression.getContainingFile();
|
||||
if (containingFile instanceof PsiJavaFile) {
|
||||
final PsiImportList importList = ((PsiJavaFile)containingFile).getImportList();
|
||||
if (importList != null) {
|
||||
for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) {
|
||||
if (statement.isOnDemand() && targetClass.equals(statement.resolveTargetClass())) {
|
||||
return Collections.singletonList(targetClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private static PsiClass getTargetClassByExpectedTypes(PsiExpression expression) {
|
||||
ExpectedTypeInfo[] typeInfos = CreateFromUsageUtils.guessExpectedTypes(expression, false);
|
||||
for (final ExpectedTypeInfo typeInfo : typeInfos) {
|
||||
final PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(typeInfo.getType());
|
||||
if (psiClass != null && psiClass.isEnum()) {
|
||||
return Collections.singletonList(psiClass);
|
||||
return psiClass;
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// "Create enum constant 'CRIME'" "false"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class AutomaticTypeInference {
|
||||
|
||||
AutomaticTypeInference(List<E> gs) {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
new AutomaticTypeInference(Arrays.asList(E.ACTION, CRI<caret>ME));
|
||||
|
||||
}
|
||||
|
||||
enum E {
|
||||
ACTION;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// "Create enum constant 'CRIME'" "false"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class AutomaticTypeInference {
|
||||
|
||||
AutomaticTypeInference(List<E> gs) {
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
new AutomaticTypeInference(Arrays.asList(E.ACTION, this.CRI<caret>ME));
|
||||
|
||||
}
|
||||
|
||||
enum E {
|
||||
ACTION;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user