import is redundant if imported class is located in the same directory (IDEA-52733)

This commit is contained in:
anna
2010-03-17 14:29:21 +03:00
parent 17ce947291
commit dc8db62ae4
8 changed files with 40 additions and 5 deletions
@@ -21,6 +21,7 @@ package com.intellij.psi.impl.source.codeStyle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
@@ -107,7 +108,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
return new ImportHelper(getSettings()).addImport(file, refClass);
}
public void removeRedundantImports(@NotNull PsiJavaFile file) throws IncorrectOperationException {
public void removeRedundantImports(final @NotNull PsiJavaFile file) throws IncorrectOperationException {
final PsiImportList importList = file.getImportList();
if (importList == null) return;
final PsiImportStatementBase[] imports = importList.getAllImportStatements();
@@ -134,14 +135,26 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
@Override public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
if (!reference.isQualified()) {
final JavaResolveResult resolveResult = reference.advancedResolve(false);
final PsiElement resolveScope = resolveResult.getCurrentFileResolveScope();
if (resolveScope instanceof PsiImportStatementBase) {
final PsiImportStatementBase importStatementBase = (PsiImportStatementBase)resolveScope;
redundants.remove(importStatementBase);
if (!inTheSamePackage(file, resolveResult.getElement())) {
final PsiElement resolveScope = resolveResult.getCurrentFileResolveScope();
if (resolveScope instanceof PsiImportStatementBase) {
final PsiImportStatementBase importStatementBase = (PsiImportStatementBase)resolveScope;
redundants.remove(importStatementBase);
}
}
}
super.visitReferenceElement(reference);
}
private boolean inTheSamePackage(PsiJavaFile file, PsiElement element) {
if (element instanceof PsiClass && ((PsiClass)element).getContainingClass() == null) {
final PsiFile containingFile = element.getContainingFile();
if (containingFile instanceof PsiJavaFile) {
return Comparing.strEqual(file.getPackageName(), ((PsiJavaFile)containingFile).getPackageName());
}
}
return false;
}
});
}
}
@@ -0,0 +1,5 @@
package p1;
class F1 {
F2 f2;
}
@@ -0,0 +1,3 @@
package p1;
public class F2 {}
@@ -0,0 +1,7 @@
package p1;
import p2.F2;
class F1 {
F2 f2;
}
@@ -0,0 +1,3 @@
package p2;
public class F2 {}
@@ -67,6 +67,10 @@ public class MoveClassTest extends CodeInsightTestCase {
doTest("ideadev27996", new String[] { "pack1.X" }, "pack2");
}
public void testUnusedImport() throws Exception {
doTest("unusedImport", new String[]{"p2.F2"}, "p1");
}
private void doTest(String testName, String[] classNames, String newPackageName) throws Exception{
String root = JavaTestUtil.getJavaTestDataPath() + "/refactoring/moveClass/" + testName;