@NotNull private static Set<DeclarationDescriptor> getTopLevelDescriptorsByFqName( @NotNull KotlinCodeAnalyzer resolveSession, @NotNull FqName fqName) { FqName parentFqName = fqName.parent(); Set<DeclarationDescriptor> descriptors = new HashSet<DeclarationDescriptor>(); LazyPackageDescriptor parentFragment = resolveSession.getPackageFragment(parentFqName); if (parentFragment != null) { // Filter out extension properties descriptors.addAll( KotlinPackage.filter( parentFragment.getMemberScope().getProperties(fqName.shortName()), new Function1<VariableDescriptor, Boolean>() { @Override public Boolean invoke(VariableDescriptor descriptor) { return descriptor.getReceiverParameter() == null; } })); } ContainerUtil.addIfNotNull(descriptors, resolveSession.getPackageFragment(fqName)); descriptors.addAll(resolveSession.getTopLevelClassDescriptors(fqName)); return descriptors; }
/** * Get jet non-extension top-level function names. Method is allowed to give invalid names - all * result should be checked with getTopLevelFunctionDescriptorsByName(). * * @return */ @NotNull public Collection<String> getAllTopLevelFunctionNames() { Set<String> functionNames = new HashSet<String>(); functionNames.addAll(JetShortFunctionNameIndex.getInstance().getAllKeys(project)); functionNames.addAll( JetFromJavaDescriptorHelper.getPossiblePackageDeclarationsNames( project, GlobalSearchScope.allScope(project))); return functionNames; }
/** * Get jet extensions top-level function names. Method is allowed to give invalid names - all * result should be checked with getAllJetExtensionFunctionsByName(). * * @return */ @NotNull public Collection<String> getAllJetExtensionFunctionsNames(@NotNull GlobalSearchScope scope) { Set<String> extensionFunctionNames = new HashSet<String>(); extensionFunctionNames.addAll(JetExtensionFunctionNameIndex.getInstance().getAllKeys(project)); extensionFunctionNames.addAll( JetFromJavaDescriptorHelper.getTopExtensionFunctionNames(project, scope)); return extensionFunctionNames; }
@NotNull public Collection<String> getAllTopLevelObjectNames() { Set<String> topObjectNames = new HashSet<String>(); topObjectNames.addAll(JetTopLevelShortObjectNameIndex.getInstance().getAllKeys(project)); Collection<PsiClass> classObjects = JetFromJavaDescriptorHelper.getCompiledClassesForTopLevelObjects( project, GlobalSearchScope.allScope(project)); topObjectNames.addAll( Collections2.transform( classObjects, new Function<PsiClass, String>() { @Override public String apply(@Nullable PsiClass aClass) { assert aClass != null; return aClass.getName(); } })); return topObjectNames; }
@NotNull public Collection<ClassDescriptor> getTopLevelObjectsByName( @NotNull String name, @NotNull JetSimpleNameExpression expression, @NotNull ResolveSession resolveSession, @NotNull GlobalSearchScope scope) { BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression); JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression); if (jetScope == null) { return Collections.emptyList(); } Set<ClassDescriptor> result = Sets.newHashSet(); Collection<JetObjectDeclaration> topObjects = JetTopLevelShortObjectNameIndex.getInstance().get(name, project, scope); for (JetObjectDeclaration objectDeclaration : topObjects) { FqName fqName = JetPsiUtil.getFQName(objectDeclaration); assert fqName != null : "Local object declaration in JetTopLevelShortObjectNameIndex:" + objectDeclaration.getText(); result.addAll( ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true)); } for (PsiClass psiClass : JetFromJavaDescriptorHelper.getCompiledClassesForTopLevelObjects( project, GlobalSearchScope.allScope(project))) { String qualifiedName = psiClass.getQualifiedName(); if (qualifiedName != null) { FqName fqName = new FqName(qualifiedName); result.addAll( ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true)); } } return result; }
@NotNull public Collection<FunctionDescriptor> getTopLevelFunctionDescriptorsByName( @NotNull String name, @NotNull JetSimpleNameExpression expression, @NotNull ResolveSession resolveSession, @NotNull GlobalSearchScope scope) { // name parameter can differ from expression.getReferenceName() when expression contains // completion suffix Name referenceName = expression.getIdentifier() == null ? JetPsiUtil.getConventionName(expression) : Name.identifier(name); if (referenceName == null || referenceName.toString().isEmpty()) { return Collections.emptyList(); } BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression); JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression); if (jetScope == null) { return Collections.emptyList(); } Set<FunctionDescriptor> result = Sets.newHashSet(); Collection<PsiMethod> topLevelFunctionPrototypes = JetFromJavaDescriptorHelper.getTopLevelFunctionPrototypesByName( referenceName.getName(), project, scope); for (PsiMethod method : topLevelFunctionPrototypes) { FqName functionFQN = JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(method); if (functionFQN != null) { JetImportDirective importDirective = JetPsiFactory.createImportDirective(project, new ImportPath(functionFQN, false)); Collection<? extends DeclarationDescriptor> declarationDescriptors = new QualifiedExpressionResolver() .analyseImportReference( importDirective, jetScope, new BindingTraceContext(), resolveSession.getModuleConfiguration()); for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) { if (declarationDescriptor instanceof FunctionDescriptor) { result.add((FunctionDescriptor) declarationDescriptor); } } } } Set<FqName> affectedPackages = Sets.newHashSet(); Collection<JetNamedFunction> jetNamedFunctions = JetShortFunctionNameIndex.getInstance().get(referenceName.getName(), project, scope); for (JetNamedFunction jetNamedFunction : jetNamedFunctions) { PsiFile containingFile = jetNamedFunction.getContainingFile(); if (containingFile instanceof JetFile) { JetFile jetFile = (JetFile) containingFile; String packageName = jetFile.getPackageName(); if (packageName != null) { affectedPackages.add(new FqName(packageName)); } } } for (FqName affectedPackage : affectedPackages) { NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName(affectedPackage); assert packageDescriptor != null : "There's a function in stub index with invalid package: " + affectedPackage; JetScope memberScope = packageDescriptor.getMemberScope(); result.addAll(memberScope.getFunctions(referenceName)); } return result; }