@NotNull public static Collection<FqName> getTopLevelFunctionFqNames( @NotNull Project project, @NotNull GlobalSearchScope scope, boolean shouldBeExtension) { Collection<FqName> result = Sets.newHashSet(); Collection<PsiClass> packageClasses = getClassesByAnnotation(KotlinPackage.class.getSimpleName(), project, scope); for (PsiClass psiClass : packageClasses) { String qualifiedName = psiClass.getQualifiedName(); if (qualifiedName == null) { continue; } FqName packageFqName = new FqName(qualifiedName).parent(); PackageData data = getPackageData(psiClass); if (data == null) { continue; } NameResolver nameResolver = data.getNameResolver(); for (ProtoBuf.Callable callable : data.getPackageProto().getMemberList()) { if (callable.hasReceiverType() == shouldBeExtension) { Name name = nameResolver.getName(callable.getName()); result.add(packageFqName.child(name)); } } } return result; }
@Override public ClassDescriptor getObjectDescriptor(@NotNull Name name) { ClassDescriptor classDescriptor = getResolver() .resolveClass(packageFQN.child(name), DescriptorSearchRule.IGNORE_IF_FOUND_IN_KOTLIN); if (classDescriptor != null && classDescriptor.getKind().isObject()) { return classDescriptor; } return null; }
@Nullable private static FqName getFQName(@Nullable JetExpression expression) { if (expression == null) { return null; } if (expression instanceof JetDotQualifiedExpression) { JetDotQualifiedExpression dotQualifiedExpression = (JetDotQualifiedExpression) expression; FqName parentFqn = getFQName(dotQualifiedExpression.getReceiverExpression()); Name child = getName(dotQualifiedExpression.getSelectorExpression()); return parentFqn != null && child != null ? parentFqn.child(child) : null; } else if (expression instanceof JetSimpleNameExpression) { JetSimpleNameExpression simpleNameExpression = (JetSimpleNameExpression) expression; return FqName.topLevel(simpleNameExpression.getReferencedNameAsName()); } else { throw new IllegalArgumentException( "Can't construct fqn for: " + expression.getClass().toString()); } }
@Nullable public static FqName getFQName(@NotNull JetNamedDeclaration namedDeclaration) { Name name = namedDeclaration.getNameAsName(); if (name == null) { return null; } PsiElement parent = namedDeclaration.getParent(); if (parent instanceof JetClassBody) { // One nesting to JetClassBody doesn't affect to qualified name parent = parent.getParent(); } FqName firstPart = null; if (parent instanceof JetFile) { firstPart = getFQName((JetFile) parent); } else if (parent instanceof JetNamedFunction || parent instanceof JetClass) { firstPart = getFQName((JetNamedDeclaration) parent); } else if (namedDeclaration instanceof JetParameter) { JetClass constructorClass = getClassIfParameterIsProperty((JetParameter) namedDeclaration); if (constructorClass != null) { firstPart = getFQName(constructorClass); } } else if (parent instanceof JetObjectDeclaration) { if (parent.getParent() instanceof JetClassObject) { JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(parent, JetClassOrObject.class); if (classOrObject != null) { firstPart = getFQName(classOrObject); } } else { firstPart = getFQName((JetNamedDeclaration) parent); } } if (firstPart == null) { return null; } return firstPart.child(name); }
@Override public NamespaceDescriptor getNamespace(@NotNull Name name) { return getResolver() .resolveNamespace(packageFQN.child(name), DescriptorSearchRule.INCLUDE_KOTLIN); }
@Override public boolean isPackageDeclared(@NotNull Name name) { return factory.isPackageDeclared(fqName.child(name)); }
@NotNull public static FqName combine(@NotNull FqName first, @NotNull Name second) { return first.child(second); }