mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-104969 IDEA should not offer to add class to import section when class already imported
This commit is contained in:
+26
@@ -44,12 +44,14 @@ import com.intellij.psi.search.PsiShortNamesCache;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
@@ -150,9 +152,33 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
|
||||
classList = filtered;
|
||||
}
|
||||
|
||||
filterAlreadyImportedButUnresolved(classList);
|
||||
return classList;
|
||||
}
|
||||
|
||||
private void filterAlreadyImportedButUnresolved(@NotNull List<PsiClass> list) {
|
||||
PsiElement element = myRef.getElement();
|
||||
PsiFile containingFile = element == null ? null : element.getContainingFile();
|
||||
if (!(containingFile instanceof PsiJavaFile)) return;
|
||||
PsiJavaFile javaFile = (PsiJavaFile)containingFile;
|
||||
PsiImportList importList = javaFile.getImportList();
|
||||
PsiImportStatementBase[] importStatements = importList == null ? PsiImportStatementBase.EMPTY_ARRAY : importList.getAllImportStatements();
|
||||
Set<String> importedNames = new THashSet<String>(importStatements.length);
|
||||
for (PsiImportStatementBase statement : importStatements) {
|
||||
PsiJavaCodeReferenceElement ref = statement.getImportReference();
|
||||
String name = ref == null ? null : ref.getReferenceName();
|
||||
if (name != null && ref.resolve() == null) importedNames.add(name);
|
||||
}
|
||||
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
PsiClass aClass = list.get(i);
|
||||
String className = aClass.getName();
|
||||
if (className != null && importedNames.contains(className)) {
|
||||
list.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected String getRequiredMemberName(T reference) {
|
||||
return null;
|
||||
|
||||
@@ -473,4 +473,14 @@ public class ImportHelperTest extends DaemonAnalyzerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testAutoImportIgnoresUnresolvedImportReferences() throws Throwable {
|
||||
@NonNls String text = "package x; import xxx.yyy.ArrayList; class S {{ ArrayList<caret> r; }}";
|
||||
configureByText(StdFileTypes.JAVA, text);
|
||||
|
||||
PsiJavaFile javaFile = (PsiJavaFile)getFile();
|
||||
PsiReference ref = javaFile.findReferenceAt(getEditor().getCaretModel().getOffset()-1);
|
||||
ImportClassFix fix = new ImportClassFix((PsiJavaCodeReferenceElement)ref);
|
||||
assertFalse(fix.isAvailable(getProject(), getEditor(), getFile()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user