From 7cb77314bcdee2303f3ab2f3ed0ecf33c63b48b5 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 7 Nov 2014 15:17:42 +0300 Subject: [PATCH] Callable Builder: Allow to forbid substitution for any TypeInfo --- .../callableBuilder/CallableBuilder.kt | 2 +- .../callableBuilder/CallableInfo.kt | 16 ++++++++++++++-- .../CreateBinaryOperationActionFactory.kt | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt index d93da9c0ced..7392650f740 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt @@ -158,7 +158,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { typeInfo: TypeInfo, substitutions: List, scope: JetScope): List { - if (typeInfo is TypeInfo.ByType && typeInfo.keepUnsubstituted) return computeTypeCandidates(typeInfo) + if (!typeInfo.substitutionsAllowed) return computeTypeCandidates(typeInfo) return typeCandidates.getOrPut(typeInfo) { val types = typeInfo.getPossibleTypes(this).reverse() diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt index fffaddd2577..24bfe7705c8 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt @@ -10,7 +10,6 @@ import org.jetbrains.jet.plugin.refactoring.JetNameSuggester import org.jetbrains.jet.plugin.refactoring.EmptyValidator import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.plugin.util.supertypes -import org.jetbrains.jet.lang.types.TypeUtils import org.jetbrains.jet.lang.types.ErrorUtils import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns import org.jetbrains.jet.lang.psi.JetElement @@ -38,7 +37,7 @@ abstract class TypeInfo(val variance: Variance) { builder.currentFileContext[BindingContext.TYPE, typeReference].getPossibleSupertypes(variance) } - class ByType(val theType: JetType, variance: Variance, val keepUnsubstituted: Boolean = false): TypeInfo(variance) { + class ByType(val theType: JetType, variance: Variance): TypeInfo(variance) { override fun getPossibleTypes(builder: CallableBuilder): List = theType.getPossibleSupertypes(variance) } @@ -48,6 +47,17 @@ abstract class TypeInfo(val variance: Variance) { (builder.placement as CallablePlacement.WithReceiver).receiverTypeCandidate.theType.getPossibleSupertypes(variance) } + abstract class DelegatingTypeInfo(val delegate: TypeInfo): TypeInfo(delegate.variance) { + override val substitutionsAllowed: Boolean = delegate.substitutionsAllowed + override val possibleNamesFromExpression: Array get() = delegate.possibleNamesFromExpression + override fun getPossibleTypes(builder: CallableBuilder): List = delegate.getPossibleTypes(builder) + } + + class NoSubstitutions(delegate: TypeInfo): DelegatingTypeInfo(delegate) { + override val substitutionsAllowed: Boolean = false + } + + open val substitutionsAllowed: Boolean = true open val possibleNamesFromExpression: Array get() = ArrayUtil.EMPTY_STRING_ARRAY abstract fun getPossibleTypes(builder: CallableBuilder): List @@ -65,6 +75,8 @@ fun TypeInfo(expressionOfType: JetExpression, variance: Variance): TypeInfo = Ty fun TypeInfo(typeReference: JetTypeReference, variance: Variance): TypeInfo = TypeInfo.ByTypeReference(typeReference, variance) fun TypeInfo(theType: JetType, variance: Variance): TypeInfo = TypeInfo.ByType(theType, variance) +fun TypeInfo.noSubstitutions(): TypeInfo = (this as? TypeInfo.NoSubstitutions) ?: TypeInfo.NoSubstitutions(this) + /** * Encapsulates information about a function parameter that is going to be created. */ diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateBinaryOperationActionFactory.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateBinaryOperationActionFactory.kt index f9dd837c2a5..248098b856f 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateBinaryOperationActionFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateBinaryOperationActionFactory.kt @@ -33,8 +33,8 @@ public object CreateBinaryOperationActionFactory: JetSingleIntentionActionFactor val receiverType = TypeInfo(receiverExpr, Variance.IN_VARIANCE) val returnType = when { - inOperation -> TypeInfo.ByType(builtIns.getBooleanType(), Variance.INVARIANT, true) - comparisonOperation -> TypeInfo.ByType(builtIns.getIntType(), Variance.INVARIANT, true) + inOperation -> TypeInfo.ByType(builtIns.getBooleanType(), Variance.INVARIANT).noSubstitutions() + comparisonOperation -> TypeInfo.ByType(builtIns.getIntType(), Variance.INVARIANT).noSubstitutions() else -> TypeInfo(callExpr, Variance.OUT_VARIANCE) } val parameters = Collections.singletonList(ParameterInfo(TypeInfo(argumentExpr, Variance.IN_VARIANCE)))