@Override @Nullable public Collection<PsiImportStatementBase> findRedundantImports(final PsiJavaFile file) { final PsiImportList importList = file.getImportList(); if (importList == null) return null; final PsiImportStatementBase[] imports = importList.getAllImportStatements(); if (imports.length == 0) return null; Set<PsiImportStatementBase> allImports = new THashSet<PsiImportStatementBase>(Arrays.asList(imports)); final Collection<PsiImportStatementBase> redundant; if (FileTypeUtils.isInServerPageFile(file)) { // remove only duplicate imports redundant = ContainerUtil.newIdentityTroveSet(); ContainerUtil.addAll(redundant, imports); redundant.removeAll(allImports); for (PsiImportStatementBase importStatement : imports) { if (importStatement instanceof JspxImportStatement && importStatement.isForeignFileImport()) { redundant.remove(importStatement); } } } else { redundant = allImports; final List<PsiFile> roots = file.getViewProvider().getAllFiles(); for (PsiElement root : roots) { root.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { if (!reference.isQualified()) { final JavaResolveResult resolveResult = reference.advancedResolve(false); if (!inTheSamePackage(file, resolveResult.getElement())) { final PsiElement resolveScope = resolveResult.getCurrentFileResolveScope(); if (resolveScope instanceof PsiImportStatementBase) { final PsiImportStatementBase importStatementBase = (PsiImportStatementBase) resolveScope; redundant.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; } }); } } return redundant; }
@NotNull private static Import importToImport(@NotNull PsiImportStatementBase i) { PsiJavaCodeReferenceElement reference = i.getImportReference(); if (reference != null) { return new Import(quoteKeywords(reference.getQualifiedName()) + (i.isOnDemand() ? ".*" : "")); } return new Import(""); }
private static void assertOrder(PsiJavaFile file, @NonNls String... expectedOrder) { PsiImportStatementBase[] statements = file.getImportList().getAllImportStatements(); assertEquals(expectedOrder.length, statements.length); for (int i = 0; i < statements.length; i++) { PsiImportStatementBase statement = statements[i]; String text = StringUtil.trimEnd(StringUtil.trimStart(statement.getText(), "import "), ";"); assertEquals(expectedOrder[i], text); } }
@Override public int compare( PsiImportStatementBase importStatementBase1, PsiImportStatementBase importStatementBase2) { final boolean onDemand = importStatementBase1.isOnDemand(); if (onDemand != importStatementBase2.isOnDemand()) { return onDemand ? -1 : 1; } // just sort on demand imports first, and sort the rest in reverse file order. return -PsiElementOrderComparator.getInstance() .compare(importStatementBase1, importStatementBase2); }
public ASTNode getDefaultAnchor( @NotNull PsiImportList list, @NotNull PsiImportStatementBase statement) { PsiJavaCodeReferenceElement ref = statement.getImportReference(); if (ref == null) return null; int entryIndex = findEntryIndex(statement); PsiImportStatementBase[] allStatements = list.getAllImportStatements(); int[] entries = ArrayUtil.newIntArray(allStatements.length); List<PsiImportStatementBase> statements = new ArrayList<PsiImportStatementBase>(); for (int i = 0; i < allStatements.length; i++) { PsiImportStatementBase statement1 = allStatements[i]; int entryIndex1 = findEntryIndex(statement1); entries[i] = entryIndex1; if (entryIndex1 == entryIndex) { statements.add(statement1); } } if (statements.isEmpty()) { int index; for (index = entries.length - 1; index >= 0; index--) { if (entries[index] < entryIndex) break; } index++; return index < entries.length ? SourceTreeToPsiMap.psiElementToTree(allStatements[index]) : null; } else { String refText = ref.getCanonicalText(); if (statement.isOnDemand()) { refText += "."; } PsiImportStatementBase insertBefore = null; PsiImportStatementBase insertAfter = null; for (PsiImportStatementBase statement1 : statements) { PsiJavaCodeReferenceElement ref1 = statement1.getImportReference(); if (ref1 == null) { continue; } String refTextThis = ref1.getCanonicalText(); if (statement1.isOnDemand()) { refTextThis += "."; } int comp = Comparing.compare(refText, refTextThis); if (comp < 0 && insertBefore == null) { insertBefore = statement1; } if (comp > 0) { insertAfter = statement1; } } if (insertBefore != null) return insertBefore.getNode(); if (insertAfter != null) return insertAfter.getNode().getTreeNext(); return null; } }
public int findEntryIndex(@NotNull PsiImportStatementBase statement) { PsiJavaCodeReferenceElement ref = statement.getImportReference(); if (ref == null) return -1; String packageName; if (statement.isOnDemand()) { packageName = ref.getCanonicalText(); } else { String className = ref.getCanonicalText(); packageName = getPackageOrClassName(className); } return findEntryIndex(packageName); }
@Override public void removeRedundantImports(@NotNull final PsiJavaFile file) throws IncorrectOperationException { final Collection<PsiImportStatementBase> redundant = findRedundantImports(file); if (redundant == null) return; for (final PsiImportStatementBase importStatement : redundant) { final PsiJavaCodeReferenceElement ref = importStatement.getImportReference(); // Do not remove non-resolving refs if (ref == null || ref.resolve() == null) { continue; } importStatement.delete(); } }
private static void addUnresolvedImportNames( @NotNull Set<String> set, @NotNull PsiJavaFile file, @NotNull Set<String> namesToImportStaticly) { PsiImportStatementBase[] imports = file.getImportList().getAllImportStatements(); for (PsiImportStatementBase anImport : imports) { PsiJavaCodeReferenceElement ref = anImport.getImportReference(); if (ref == null) continue; JavaResolveResult[] results = ref.multiResolve(false); if (results.length == 0) { String text = ref.getCanonicalText(); if (anImport.isOnDemand()) { text += ".*"; } if (anImport instanceof PsiImportStaticStatement) { namesToImportStaticly.add(text); } set.add(text); } } }