Renames + parameter to receiver
This commit is contained in:
@@ -16,18 +16,18 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.util
|
||||
|
||||
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
||||
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy
|
||||
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||
|
||||
object IdeDescriptorRenderers {
|
||||
@JvmField val APPROXIMATE_FLEXIBLE_TYPES: (KotlinType) -> KotlinType = { approximateFlexibleTypes(it, true) }
|
||||
@JvmField val APPROXIMATE_FLEXIBLE_TYPES: (KotlinType) -> KotlinType = { it.approximateFlexibleTypes(preferNotNull = false) }
|
||||
|
||||
@JvmField val APPROXIMATE_FLEXIBLE_TYPES_IN_ARGUMENTS: (KotlinType) -> KotlinType = { approximateFlexibleTypes(it, false) }
|
||||
@JvmField val APPROXIMATE_FLEXIBLE_TYPES_NOT_NULL: (KotlinType) -> KotlinType = { it.approximateFlexibleTypes(preferNotNull = true) }
|
||||
|
||||
private fun unwrapAnonymousType(type: KotlinType): KotlinType {
|
||||
if (type.isDynamic()) return type
|
||||
@@ -58,9 +58,9 @@ object IdeDescriptorRenderers {
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) }
|
||||
}
|
||||
|
||||
@JvmField val SOURCE_CODE_FOR_TYPE_ARGUMENTS: DescriptorRenderer = BASE.withOptions {
|
||||
@JvmField val SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION: DescriptorRenderer = BASE.withOptions {
|
||||
classifierNamePolicy = ClassifierNamePolicy.SOURCE_CODE_QUALIFIED
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES_IN_ARGUMENTS(unwrapAnonymousType(it)) }
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES_NOT_NULL(unwrapAnonymousType(it)) }
|
||||
}
|
||||
|
||||
@JvmField val SOURCE_CODE_SHORT_NAMES_IN_TYPES: DescriptorRenderer = BASE.withOptions {
|
||||
|
||||
@@ -22,9 +22,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_READONLY_ANNOTATION
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
@@ -34,10 +32,10 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.*
|
||||
|
||||
fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): KotlinType {
|
||||
if (jetType.isDynamic()) return jetType
|
||||
if (jetType.isFlexible()) {
|
||||
val flexible = jetType.flexibility()
|
||||
fun KotlinType.approximateFlexibleTypes(preferNotNull: Boolean = false): KotlinType {
|
||||
if (isDynamic()) return this
|
||||
if (isFlexible()) {
|
||||
val flexible = flexibility()
|
||||
val lowerClass = flexible.lowerBound.constructor.declarationDescriptor as? ClassDescriptor?
|
||||
val isCollection = lowerClass != null && JavaToKotlinClassMap.INSTANCE.isMutable(lowerClass)
|
||||
// (Mutable)Collection<T>! -> MutableCollection<T>?
|
||||
@@ -46,13 +44,13 @@ fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): Ko
|
||||
// Foo<Bar!>! -> Foo<Bar>?
|
||||
var approximation =
|
||||
if (isCollection)
|
||||
TypeUtils.makeNullableAsSpecified(if (jetType.isAnnotatedReadOnly()) flexible.upperBound else flexible.lowerBound, outermost)
|
||||
TypeUtils.makeNullableAsSpecified(if (isAnnotatedReadOnly()) flexible.upperBound else flexible.lowerBound, !preferNotNull)
|
||||
else
|
||||
if (outermost) flexible.upperBound else flexible.lowerBound
|
||||
if (preferNotNull) flexible.lowerBound else flexible.upperBound
|
||||
|
||||
approximation = approximateFlexibleTypes(approximation)
|
||||
approximation = approximation.approximateFlexibleTypes()
|
||||
|
||||
approximation = if (jetType.isAnnotatedNotNull()) approximation.makeNotNullable() else approximation
|
||||
approximation = if (isAnnotatedNotNull()) approximation.makeNotNullable() else approximation
|
||||
|
||||
if (approximation.isMarkedNullable && !flexible.lowerBound.isMarkedNullable && TypeUtils.isTypeParameter(approximation) && TypeUtils.hasNullableSuperType(approximation)) {
|
||||
approximation = approximation.makeNotNullable()
|
||||
@@ -61,10 +59,10 @@ fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): Ko
|
||||
return approximation
|
||||
}
|
||||
return KotlinTypeImpl.create(
|
||||
jetType.annotations,
|
||||
jetType.constructor,
|
||||
jetType.isMarkedNullable,
|
||||
jetType.arguments.map { it.substitute { type -> approximateFlexibleTypes(type, false)} },
|
||||
annotations,
|
||||
constructor,
|
||||
isMarkedNullable,
|
||||
arguments.map { it.substitute { type -> type.approximateFlexibleTypes(preferNotNull = true) } },
|
||||
ErrorUtils.createErrorScope("This type is not supposed to be used in member resolution", true)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
class ChangeParameterTypeFix(element: KtParameter, type: KotlinType) : KotlinQuickFixAction<KtParameter>(element) {
|
||||
private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
|
||||
private val typeInfo = KotlinTypeInfo(isCovariant = false, text = IdeDescriptorRenderers.SOURCE_CODE_FOR_TYPE_ARGUMENTS.renderType(type))
|
||||
private val typeInfo = KotlinTypeInfo(isCovariant = false, text = IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION.renderType(type))
|
||||
|
||||
private val containingDeclarationName: String?
|
||||
private val isPrimaryConstructorParameter: Boolean
|
||||
|
||||
@@ -25,7 +25,7 @@ data class KotlinTypeInfo(val isCovariant: Boolean, val type: KotlinType? = null
|
||||
fun KotlinTypeInfo.render(): String {
|
||||
return when {
|
||||
text != null -> text
|
||||
type != null -> (if (isCovariant) IdeDescriptorRenderers.SOURCE_CODE else IdeDescriptorRenderers.SOURCE_CODE_FOR_TYPE_ARGUMENTS).renderType(type)
|
||||
type != null -> (if (isCovariant) IdeDescriptorRenderers.SOURCE_CODE else IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION).renderType(type)
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -76,7 +76,7 @@ fun getCallableSubstitutor(
|
||||
|
||||
fun KotlinType.renderTypeWithSubstitution(substitutor: TypeSubstitutor?, defaultText: String, inArgumentPosition: Boolean): String {
|
||||
val newType = substitutor?.substitute(this, Variance.INVARIANT) ?: return defaultText
|
||||
val renderer = if (inArgumentPosition) IdeDescriptorRenderers.SOURCE_CODE_FOR_TYPE_ARGUMENTS else IdeDescriptorRenderers.SOURCE_CODE
|
||||
val renderer = if (inArgumentPosition) IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION else IdeDescriptorRenderers.SOURCE_CODE
|
||||
return renderer.renderType(newType)
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -332,7 +332,7 @@ val ControlFlow.possibleReturnTypes: List<KotlinType>
|
||||
!returnType.isNullabilityFlexible() ->
|
||||
listOf(returnType)
|
||||
returnType.isAnnotatedNotNull() || returnType.isAnnotatedNullable() ->
|
||||
listOf(approximateFlexibleTypes(returnType))
|
||||
listOf(returnType.approximateFlexibleTypes())
|
||||
else ->
|
||||
returnType.getCapability(Flexibility::class.java).let { listOf(it!!.upperBound, it.lowerBound) }
|
||||
}
|
||||
|
||||
@@ -748,7 +748,7 @@ fun getQualifiedTypeArgumentList(initializer: KtExpression): KtTypeArgumentList?
|
||||
val typeArgumentMap = call.typeArguments
|
||||
val typeArguments = call.candidateDescriptor.typeParameters.mapNotNull { typeArgumentMap[it] }
|
||||
val renderedList = typeArguments.joinToString(prefix = "<", postfix = ">") {
|
||||
IdeDescriptorRenderers.SOURCE_CODE_FOR_TYPE_ARGUMENTS.renderType(it)
|
||||
IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION.renderType(it)
|
||||
}
|
||||
return KtPsiFactory(initializer).createTypeArguments(renderedList)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user