Renames + parameter to receiver

This commit is contained in:
Valentin Kipyatkov
2016-03-29 21:29:26 +03:00
parent 1f39511950
commit 40e69318b8
7 changed files with 23 additions and 25 deletions
@@ -16,18 +16,18 @@
package org.jetbrains.kotlin.idea.util package org.jetbrains.kotlin.idea.util
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy
import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isDynamic import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.types.typeUtil.builtIns
object IdeDescriptorRenderers { 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 { private fun unwrapAnonymousType(type: KotlinType): KotlinType {
if (type.isDynamic()) return type if (type.isDynamic()) return type
@@ -58,9 +58,9 @@ object IdeDescriptorRenderers {
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) } 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 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 { @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.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport
import org.jetbrains.kotlin.incremental.components.NoLookupLocation 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.*
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_READONLY_ANNOTATION
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
import org.jetbrains.kotlin.resolve.DescriptorUtils 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.*
import org.jetbrains.kotlin.types.typeUtil.* import org.jetbrains.kotlin.types.typeUtil.*
fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): KotlinType { fun KotlinType.approximateFlexibleTypes(preferNotNull: Boolean = false): KotlinType {
if (jetType.isDynamic()) return jetType if (isDynamic()) return this
if (jetType.isFlexible()) { if (isFlexible()) {
val flexible = jetType.flexibility() val flexible = flexibility()
val lowerClass = flexible.lowerBound.constructor.declarationDescriptor as? ClassDescriptor? val lowerClass = flexible.lowerBound.constructor.declarationDescriptor as? ClassDescriptor?
val isCollection = lowerClass != null && JavaToKotlinClassMap.INSTANCE.isMutable(lowerClass) val isCollection = lowerClass != null && JavaToKotlinClassMap.INSTANCE.isMutable(lowerClass)
// (Mutable)Collection<T>! -> MutableCollection<T>? // (Mutable)Collection<T>! -> MutableCollection<T>?
@@ -46,13 +44,13 @@ fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): Ko
// Foo<Bar!>! -> Foo<Bar>? // Foo<Bar!>! -> Foo<Bar>?
var approximation = var approximation =
if (isCollection) if (isCollection)
TypeUtils.makeNullableAsSpecified(if (jetType.isAnnotatedReadOnly()) flexible.upperBound else flexible.lowerBound, outermost) TypeUtils.makeNullableAsSpecified(if (isAnnotatedReadOnly()) flexible.upperBound else flexible.lowerBound, !preferNotNull)
else 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)) { if (approximation.isMarkedNullable && !flexible.lowerBound.isMarkedNullable && TypeUtils.isTypeParameter(approximation) && TypeUtils.hasNullableSuperType(approximation)) {
approximation = approximation.makeNotNullable() approximation = approximation.makeNotNullable()
@@ -61,10 +59,10 @@ fun approximateFlexibleTypes(jetType: KotlinType, outermost: Boolean = true): Ko
return approximation return approximation
} }
return KotlinTypeImpl.create( return KotlinTypeImpl.create(
jetType.annotations, annotations,
jetType.constructor, constructor,
jetType.isMarkedNullable, isMarkedNullable,
jetType.arguments.map { it.substitute { type -> approximateFlexibleTypes(type, false)} }, arguments.map { it.substitute { type -> type.approximateFlexibleTypes(preferNotNull = true) } },
ErrorUtils.createErrorScope("This type is not supposed to be used in member resolution", 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) { class ChangeParameterTypeFix(element: KtParameter, type: KotlinType) : KotlinQuickFixAction<KtParameter>(element) {
private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type) 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 containingDeclarationName: String?
private val isPrimaryConstructorParameter: Boolean private val isPrimaryConstructorParameter: Boolean
@@ -25,7 +25,7 @@ data class KotlinTypeInfo(val isCovariant: Boolean, val type: KotlinType? = null
fun KotlinTypeInfo.render(): String { fun KotlinTypeInfo.render(): String {
return when { return when {
text != null -> text 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 -> "" else -> ""
} }
} }
@@ -76,7 +76,7 @@ fun getCallableSubstitutor(
fun KotlinType.renderTypeWithSubstitution(substitutor: TypeSubstitutor?, defaultText: String, inArgumentPosition: Boolean): String { fun KotlinType.renderTypeWithSubstitution(substitutor: TypeSubstitutor?, defaultText: String, inArgumentPosition: Boolean): String {
val newType = substitutor?.substitute(this, Variance.INVARIANT) ?: return defaultText 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) return renderer.renderType(newType)
} }
@@ -332,7 +332,7 @@ val ControlFlow.possibleReturnTypes: List<KotlinType>
!returnType.isNullabilityFlexible() -> !returnType.isNullabilityFlexible() ->
listOf(returnType) listOf(returnType)
returnType.isAnnotatedNotNull() || returnType.isAnnotatedNullable() -> returnType.isAnnotatedNotNull() || returnType.isAnnotatedNullable() ->
listOf(approximateFlexibleTypes(returnType)) listOf(returnType.approximateFlexibleTypes())
else -> else ->
returnType.getCapability(Flexibility::class.java).let { listOf(it!!.upperBound, it.lowerBound) } 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 typeArgumentMap = call.typeArguments
val typeArguments = call.candidateDescriptor.typeParameters.mapNotNull { typeArgumentMap[it] } val typeArguments = call.candidateDescriptor.typeParameters.mapNotNull { typeArgumentMap[it] }
val renderedList = typeArguments.joinToString(prefix = "<", postfix = ">") { 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) return KtPsiFactory(initializer).createTypeArguments(renderedList)
} }