A couple utility methods
PsiCodegenPredictor.getFileForCodegenNamedClass() returns JetFile which contains a declaration which will produce a class with a given name. PsiCodegenPredictor.getFileForNamespacePartName() returns JetFile which produces a namespace part with the given name. BindingContextUtils.getContainingFile() returns JetFile which contains a declaration which is matched to a given descriptor. DescriptorUtils.findTopLevelParent() finds a top-level (i.e. its containing declaration is NamespaceDescriptor) parent for a descriptor. Also DescriptorUtils.isTopLevelFunction() is changed a little to accept not only a function, but any descriptor
This commit is contained in:
@@ -112,6 +112,19 @@ public class BindingContextUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static JetFile getContainingFile(@NotNull BindingContext context, @NotNull DeclarationDescriptor declarationDescriptor) {
|
||||
// declarationDescriptor may describe a synthesized element which doesn't have PSI
|
||||
// To workaround that, we find a top-level parent (which is inside a NamespaceDescriptor), which is guaranteed to have PSI
|
||||
DeclarationDescriptor descriptor = DescriptorUtils.findTopLevelParent(declarationDescriptor);
|
||||
if (descriptor == null) return null;
|
||||
|
||||
PsiElement declaration = descriptorToDeclaration(context, descriptor);
|
||||
if (declaration == null) return null;
|
||||
|
||||
return (JetFile) declaration.getContainingFile();
|
||||
}
|
||||
|
||||
// TODO these helper methods are added as a workaround to some compiler bugs in Kotlin...
|
||||
|
||||
// NOTE this is used by KDoc
|
||||
|
||||
@@ -129,8 +129,17 @@ public class DescriptorUtils {
|
||||
return getFQName(containingDeclaration).child(descriptor.getName());
|
||||
}
|
||||
|
||||
public static boolean isTopLevelFunction(@NotNull SimpleFunctionDescriptor functionDescriptor) {
|
||||
return functionDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor;
|
||||
public static boolean isTopLevelDeclaration(@NotNull DeclarationDescriptor descriptor) {
|
||||
return descriptor.getContainingDeclaration() instanceof NamespaceDescriptor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static DeclarationDescriptor findTopLevelParent(@NotNull DeclarationDescriptor declarationDescriptor) {
|
||||
DeclarationDescriptor descriptor = declarationDescriptor;
|
||||
while (!(descriptor == null || isTopLevelDeclaration(descriptor))) {
|
||||
descriptor = descriptor.getContainingDeclaration();
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user