mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-192382 Override unused imports when adding class with same name
This commit is contained in:
@@ -430,6 +430,7 @@ public class ImportHelper{
|
||||
String packageName = getPackageOrClassName(className);
|
||||
String shortName = PsiNameHelper.getShortClassName(className);
|
||||
|
||||
findUnusedSingleImport(file, shortName).ifPresent(PsiElement::delete);
|
||||
PsiClass conflictSingleRef = findSingleImportByShortName(file, shortName);
|
||||
if (conflictSingleRef != null && !forceReimport){
|
||||
return className.equals(conflictSingleRef.getQualifiedName());
|
||||
@@ -522,6 +523,25 @@ public class ImportHelper{
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Optional<PsiImportStatement> findUnusedSingleImport(PsiJavaFile file, String name) {
|
||||
PsiImportList importList = file.getImportList();
|
||||
if (importList != null) {
|
||||
for (PsiImportStatement statement : importList.getImportStatements()) {
|
||||
PsiJavaCodeReferenceElement ref = statement.getImportReference();
|
||||
if (!statement.isOnDemand() && ref != null && name.equals(ref.getReferenceName())) {
|
||||
PsiElement target = statement.resolve();
|
||||
if (target instanceof PsiClass) {
|
||||
Collection<PsiReference> all = ReferencesSearch.search(target, new LocalSearchScope(file)).findAll();
|
||||
if (all.size() == 1 && PsiTreeUtil.isAncestor(statement, all.iterator().next().getElement(), true)) {
|
||||
return Optional.of(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static boolean containsInCurrentPackage(@NotNull PsiJavaFile file, PsiClass curRefClass) {
|
||||
if (curRefClass != null) {
|
||||
final String curRefClassQualifiedName = curRefClass.getQualifiedName();
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import foo.List;
|
||||
|
||||
class Scratch {
|
||||
Lis<caret>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import java.util.List;
|
||||
|
||||
class Scratch {
|
||||
List<caret>
|
||||
}
|
||||
+8
@@ -1859,4 +1859,12 @@ class Bar {{
|
||||
checkResult()
|
||||
}
|
||||
|
||||
void testRemoveUnusedImportOfSameName() {
|
||||
myFixture.addClass("package foo; public class List {}")
|
||||
configureByTestName()
|
||||
lookup.currentItem = myFixture.lookupElements.find { it.object instanceof PsiClass && ((PsiClass)it.object).qualifiedName == 'java.util.List' }
|
||||
myFixture.type('\n')
|
||||
checkResult()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user