public static boolean doNeedImport( @NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) { if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName()) .equals(JavaDescriptorResolver.JAVA_ROOT.getName())) { FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder()); } if (isImportedByDefault(importPath, null, JetPsiUtil.getFQName(file))) { return false; } List<JetImportDirective> importDirectives = file.getImportDirectives(); if (!importDirectives.isEmpty()) { // Check if import is already present for (JetImportDirective directive : importDirectives) { ImportPath existentImportPath = JetPsiUtil.getImportPath(directive); if (directive.getAliasName() == null && aliasName == null) { if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) { return false; } } } } return true; }
@Nullable public static Name getAliasName(@NotNull JetImportDirective importDirective) { String aliasName = importDirective.getAliasName(); JetExpression importedReference = importDirective.getImportedReference(); if (importedReference == null) { return null; } JetSimpleNameExpression referenceExpression = getLastReference(importedReference); if (aliasName == null) { aliasName = referenceExpression != null ? referenceExpression.getReferencedName() : null; } return aliasName != null && !aliasName.isEmpty() ? Name.identifier(aliasName) : null; }
@Nullable @IfNotParsed public static ImportPath getImportPath(@NotNull JetImportDirective importDirective) { if (PsiTreeUtil.hasErrorElements(importDirective)) { return null; } FqName importFqn = getFQName(importDirective.getImportedReference()); if (importFqn == null) { return null; } Name alias = null; String aliasName = importDirective.getAliasName(); if (aliasName != null) { alias = Name.identifier(aliasName); } return new ImportPath(importFqn, importDirective.isAllUnder(), alias); }