Minor: refactoring createClassUtils.kt
This commit is contained in:
+23
-20
@@ -43,7 +43,6 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.Qualifier
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.substitutions.getTypeSubstitution
|
||||
import java.lang.AssertionError
|
||||
import java.util.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind as ClassDescriptorKind
|
||||
|
||||
@@ -52,9 +51,9 @@ internal fun String.checkClassName(): Boolean = isNotEmpty() && Character.isUppe
|
||||
private fun String.checkPackageName(): Boolean = isNotEmpty() && Character.isLowerCase(first())
|
||||
|
||||
internal fun getTargetParentsByQualifier(
|
||||
element: KtElement,
|
||||
isQualified: Boolean,
|
||||
qualifierDescriptor: DeclarationDescriptor?
|
||||
element: KtElement,
|
||||
isQualified: Boolean,
|
||||
qualifierDescriptor: DeclarationDescriptor?
|
||||
): List<PsiElement> {
|
||||
val file = element.containingKtFile
|
||||
val project = file.project
|
||||
@@ -66,8 +65,7 @@ internal fun getTargetParentsByQualifier(
|
||||
qualifierDescriptor is PackageViewDescriptor ->
|
||||
if (qualifierDescriptor.fqName != file.packageFqName) {
|
||||
listOfNotNull(JavaPsiFacade.getInstance(project).findPackage(qualifierDescriptor.fqName.asString()))
|
||||
}
|
||||
else listOf(file)
|
||||
} else listOf(file)
|
||||
else ->
|
||||
emptyList()
|
||||
}
|
||||
@@ -79,7 +77,11 @@ internal fun getTargetParentsByCall(call: Call, context: BindingContext): List<P
|
||||
val receiver = call.explicitReceiver
|
||||
return when (receiver) {
|
||||
null -> getTargetParentsByQualifier(callElement, false, null)
|
||||
is Qualifier -> getTargetParentsByQualifier(callElement, true, context[BindingContext.REFERENCE_TARGET, receiver.referenceExpression])
|
||||
is Qualifier -> getTargetParentsByQualifier(
|
||||
callElement,
|
||||
true,
|
||||
context[BindingContext.REFERENCE_TARGET, receiver.referenceExpression]
|
||||
)
|
||||
is ReceiverValue -> getTargetParentsByQualifier(callElement, true, receiver.type.constructor.declarationDescriptor)
|
||||
else -> throw AssertionError("Unexpected receiver: $receiver")
|
||||
}
|
||||
@@ -88,7 +90,7 @@ internal fun getTargetParentsByCall(call: Call, context: BindingContext): List<P
|
||||
internal fun isInnerClassExpected(call: Call) = call.explicitReceiver is ReceiverValue
|
||||
|
||||
internal fun KtExpression.guessTypeForClass(context: BindingContext, moduleDescriptor: ModuleDescriptor) =
|
||||
guessTypes(context, moduleDescriptor, coerceUnusedToUnit = false).singleOrNull()
|
||||
guessTypes(context, moduleDescriptor, coerceUnusedToUnit = false).singleOrNull()
|
||||
|
||||
internal fun KotlinType.toClassTypeInfo(): TypeInfo {
|
||||
return TypeInfo.ByType(this, Variance.OUT_VARIANCE).noSubstitutions()
|
||||
@@ -101,13 +103,14 @@ internal fun getClassKindFilter(expectedType: KotlinType, containingDeclaration:
|
||||
val isEnum = DescriptorUtils.isEnumClass(descriptor)
|
||||
|
||||
if (!(canHaveSubtypes || isEnum)
|
||||
|| descriptor is TypeParameterDescriptor) return { _ -> false }
|
||||
|| descriptor is TypeParameterDescriptor
|
||||
) return { _ -> false }
|
||||
|
||||
return { classKind ->
|
||||
when (classKind) {
|
||||
ClassKind.ENUM_ENTRY -> isEnum && containingDeclaration == DescriptorToSourceUtils.descriptorToDeclaration(descriptor)
|
||||
ClassKind.INTERFACE -> containingDeclaration !is PsiClass
|
||||
|| (descriptor as? ClassDescriptor)?.kind == ClassDescriptorKind.INTERFACE
|
||||
|| (descriptor as? ClassDescriptor)?.kind == ClassDescriptorKind.INTERFACE
|
||||
else -> canHaveSubtypes
|
||||
}
|
||||
}
|
||||
@@ -118,11 +121,11 @@ internal fun KtSimpleNameExpression.getCreatePackageFixIfApplicable(targetParent
|
||||
if (!name.checkPackageName()) return null
|
||||
|
||||
val basePackage: PsiPackage =
|
||||
when (targetParent) {
|
||||
is KtFile -> JavaPsiFacade.getInstance(targetParent.project).findPackage(targetParent.packageFqName.asString())
|
||||
is PsiPackage -> targetParent
|
||||
else -> null
|
||||
}
|
||||
when (targetParent) {
|
||||
is KtFile -> JavaPsiFacade.getInstance(targetParent.project).findPackage(targetParent.packageFqName.asString())
|
||||
is PsiPackage -> targetParent
|
||||
else -> null
|
||||
}
|
||||
?: return null
|
||||
|
||||
val baseName = basePackage.qualifiedName
|
||||
@@ -138,9 +141,9 @@ internal fun KtSimpleNameExpression.getCreatePackageFixIfApplicable(targetParent
|
||||
}
|
||||
|
||||
data class UnsubstitutedTypeConstraintInfo(
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
private val originalSubstitution: Map<TypeConstructor, TypeProjection>,
|
||||
val upperBound: KotlinType
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
private val originalSubstitution: Map<TypeConstructor, TypeProjection>,
|
||||
val upperBound: KotlinType
|
||||
) {
|
||||
fun performSubstitution(vararg substitution: Pair<TypeConstructor, TypeProjection>): TypeConstraintInfo? {
|
||||
val currentSubstitution = LinkedHashMap<TypeConstructor, TypeProjection>().apply {
|
||||
@@ -153,8 +156,8 @@ data class UnsubstitutedTypeConstraintInfo(
|
||||
}
|
||||
|
||||
data class TypeConstraintInfo(
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
val upperBound: KotlinType
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
val upperBound: KotlinType
|
||||
)
|
||||
|
||||
fun getUnsubstitutedTypeConstraintInfo(element: KtTypeElement): UnsubstitutedTypeConstraintInfo? {
|
||||
|
||||
Reference in New Issue
Block a user