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; }
public static void addImportDirective( @NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) { if (!doNeedImport(importPath, aliasName, file)) { return; } JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath, aliasName); List<JetImportDirective> importDirectives = file.getImportDirectives(); if (!importDirectives.isEmpty()) { JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1); lastDirective.getParent().addAfter(newDirective, lastDirective); } else { file.getNamespaceHeader().getParent().addAfter(newDirective, file.getNamespaceHeader()); } }
public static void writeImportToFile(ImportPath importPath, JetFile file) { JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath); List<JetImportDirective> importDirectives = file.getImportDirectives(); if (!importDirectives.isEmpty()) { JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1); lastDirective.getParent().addAfter(newDirective, lastDirective); } else { JetNamespaceHeader header = file.getNamespaceHeader(); if (header == null) { throw new IllegalStateException("Scripts are not supported: " + file.getName()); } header.getParent().addAfter(newDirective, file.getNamespaceHeader()); } }
@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); }
@NotNull public Collection<DeclarationDescriptor> processImportReference( @NotNull JetImportDirective importDirective, @NotNull JetScope scope, @NotNull JetScope scopeToCheckVisibility, @Nullable Importer importer, @NotNull BindingTrace trace, @NotNull LookupMode lookupMode) { if (importDirective.isAbsoluteInRootPackage()) { trace.report(UNSUPPORTED.on(importDirective, "TypeHierarchyResolver")); // TODO return Collections.emptyList(); } JetExpression importedReference = importDirective.getImportedReference(); if (importedReference == null) { return Collections.emptyList(); } Collection<DeclarationDescriptor> descriptors; if (importedReference instanceof JetQualifiedExpression) { // store result only when we find all descriptors, not only classes on the second phase descriptors = lookupDescriptorsForQualifiedExpression( (JetQualifiedExpression) importedReference, scope, scopeToCheckVisibility, trace, lookupMode, lookupMode == LookupMode.EVERYTHING); } else { assert importedReference instanceof JetSimpleNameExpression; descriptors = lookupDescriptorsForSimpleNameReference( (JetSimpleNameExpression) importedReference, scope, scopeToCheckVisibility, trace, lookupMode, true, lookupMode == LookupMode.EVERYTHING); } JetSimpleNameExpression referenceExpression = JetPsiUtil.getLastReference(importedReference); if (importDirective.isAllUnder()) { if (!canAllUnderImportFrom(descriptors) && referenceExpression != null) { ClassDescriptor toReportOn = KotlinPackage.filterIsInstance(descriptors, ClassDescriptor.class).iterator().next(); trace.report(CANNOT_IMPORT_ON_DEMAND_FROM_SINGLETON.on(referenceExpression, toReportOn)); } if (referenceExpression == null || !canImportMembersFrom(descriptors, referenceExpression, trace, lookupMode)) { return Collections.emptyList(); } if (importer != null) { for (DeclarationDescriptor descriptor : descriptors) { importer.addAllUnderImport(descriptor); } } return Collections.emptyList(); } Name aliasName = JetPsiUtil.getAliasName(importDirective); if (aliasName == null) { return Collections.emptyList(); } if (importer != null) { for (DeclarationDescriptor descriptor : descriptors) { importer.addAliasImport(descriptor, aliasName); } } return descriptors; }
@NotNull public Collection<? extends DeclarationDescriptor> processImportReference( @NotNull JetImportDirective importDirective, @NotNull JetScope scope, @NotNull JetScope scopeToCheckVisibility, @NotNull Importer importer, @NotNull BindingTrace trace, @NotNull ModuleConfiguration moduleConfiguration, @NotNull LookupMode lookupMode) { if (importDirective.isAbsoluteInRootNamespace()) { trace.report(UNSUPPORTED.on(importDirective, "TypeHierarchyResolver")); // TODO return Collections.emptyList(); } JetExpression importedReference = importDirective.getImportedReference(); if (importedReference == null) { return Collections.emptyList(); } Collection<? extends DeclarationDescriptor> descriptors; if (importedReference instanceof JetQualifiedExpression) { // store result only when we find all descriptors, not only classes on the second phase descriptors = lookupDescriptorsForQualifiedExpression( (JetQualifiedExpression) importedReference, scope, scopeToCheckVisibility, trace, lookupMode, lookupMode == LookupMode.EVERYTHING); } else { assert importedReference instanceof JetSimpleNameExpression; descriptors = lookupDescriptorsForSimpleNameReference( (JetSimpleNameExpression) importedReference, scope, scopeToCheckVisibility, trace, lookupMode, true, lookupMode == LookupMode.EVERYTHING); } JetSimpleNameExpression referenceExpression = JetPsiUtil.getLastReference(importedReference); if (importDirective.isAllUnder()) { if (referenceExpression == null || !canImportMembersFrom(descriptors, referenceExpression, trace, lookupMode)) { return Collections.emptyList(); } for (DeclarationDescriptor descriptor : descriptors) { importer.addAllUnderImport(descriptor, moduleConfiguration.getPlatformToKotlinClassMap()); } return Collections.emptyList(); } Name aliasName = JetPsiUtil.getAliasName(importDirective); if (aliasName == null) { return Collections.emptyList(); } for (DeclarationDescriptor descriptor : descriptors) { importer.addAliasImport(descriptor, aliasName); } return descriptors; }