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