mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
allow to add static import for containing class when it already contains imported members (IDEA-96156)
This commit is contained in:
+30
-23
@@ -55,11 +55,11 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
@Nullable
|
||||
public static String getStaticImportClass(@NotNull PsiElement element) {
|
||||
if (!PsiUtil.isLanguageLevel5OrHigher(element)) return null;
|
||||
PsiFile file = element.getContainingFile();
|
||||
if (element instanceof PsiIdentifier) {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiMethodReferenceExpression) return null;
|
||||
if (parent instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)parent).getQualifier() != null) {
|
||||
if (PsiTreeUtil.getParentOfType(parent, PsiImportList.class) != null) return null;
|
||||
PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement)parent;
|
||||
if (checkParameterizedReference(refExpr)) return null;
|
||||
PsiElement resolved = refExpr.resolve();
|
||||
@@ -68,27 +68,8 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
if (aClass != null && !PsiTreeUtil.isAncestor(aClass, element, true) && !aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null && !Comparing.strEqual(qName, aClass.getName())) {
|
||||
qName = qName + "." +refExpr.getReferenceName();
|
||||
if (file instanceof PsiJavaFile) {
|
||||
PsiImportList importList = ((PsiJavaFile)file).getImportList();
|
||||
if (importList != null) {
|
||||
for (PsiImportStaticStatement staticStatement : importList.getImportStaticStatements()) {
|
||||
if (staticStatement.isOnDemand()) {
|
||||
if (staticStatement.resolveTargetClass() == aClass) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
final PsiImportStatementBase importStatement = importList.findSingleImportStatement(refExpr.getReferenceName());
|
||||
if (importStatement == null) {
|
||||
return qName;
|
||||
}
|
||||
final PsiElement resolve = importStatement.resolve();
|
||||
if (resolve instanceof PsiMember && ((PsiMember)resolve).getContainingClass() != aClass) {
|
||||
return qName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return qName + "." +refExpr.getReferenceName();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +79,28 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PsiImportStatementBase findExistingImport(PsiFile file, PsiClass aClass, String refName) {
|
||||
if (file instanceof PsiJavaFile) {
|
||||
PsiImportList importList = ((PsiJavaFile)file).getImportList();
|
||||
if (importList != null) {
|
||||
for (PsiImportStaticStatement staticStatement : importList.getImportStaticStatements()) {
|
||||
if (staticStatement.isOnDemand()) {
|
||||
if (staticStatement.resolveTargetClass() == aClass) {
|
||||
return staticStatement;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final PsiImportStatementBase importStatement = importList.findSingleImportStatement(refName);
|
||||
final PsiElement resolve = importStatement != null ? importStatement.resolve() : null;
|
||||
if (resolve instanceof PsiMember && ((PsiMember)resolve).getContainingClass() == aClass) {
|
||||
return importStatement;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean checkParameterizedReference(PsiJavaCodeReferenceElement refExpr) {
|
||||
PsiReferenceParameterList parameterList = refExpr instanceof PsiReferenceExpression ? refExpr.getParameterList() : null;
|
||||
return parameterList != null && parameterList.getFirstChild() != null;
|
||||
@@ -154,11 +157,15 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
}
|
||||
});
|
||||
|
||||
if (resolved != null) {
|
||||
if (resolved != null && findExistingImport(file, resolvedClass, referenceName) == null) {
|
||||
PsiReferenceExpressionImpl.bindToElementViaStaticImport(resolvedClass, referenceName, ((PsiJavaFile)file).getImportList());
|
||||
}
|
||||
|
||||
file.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitImportList(PsiImportList list) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
|
||||
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import static foo.Clazz.Foo.Const_1;
|
||||
|
||||
class MyObject{
|
||||
public void doIt(){
|
||||
foo.Clazz.Fo<caret>o.Const_1;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import static foo.Clazz.Foo;
|
||||
import static foo.Clazz.Foo.Const_1;
|
||||
|
||||
class MyObject{
|
||||
public void doIt(){
|
||||
Foo.Const_1;
|
||||
}
|
||||
}
|
||||
+15
@@ -44,6 +44,21 @@ public class AddSingleStaticImportActionTest extends JavaCodeInsightFixtureTestC
|
||||
myFixture.checkResultByFile(getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testAllowStaticImportWhenAlreadyImported() {
|
||||
myFixture.addClass("package foo; " +
|
||||
"public class Clazz {\n" +
|
||||
" public enum Foo{\n" +
|
||||
" Const_1, Const_2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
final IntentionAction intentionAction = myFixture.findSingleIntention("Add static import for 'foo.Clazz.Foo'");
|
||||
assertNotNull(intentionAction);
|
||||
myFixture.launchAction(intentionAction);
|
||||
myFixture.checkResultByFile(getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testInsideParameterizedReferenceInsideParameterizedReference() {
|
||||
myFixture.addClass("package foo; " +
|
||||
"public class Class1 {" +
|
||||
|
||||
Reference in New Issue
Block a user