Refactoring: Move non-specific TypeSubstitutor factories to substitutionUtils.kt in compiler

Merge typeUtils.kt with changeSignatureUtils.kt
This commit is contained in:
Alexey Sedunov
2015-07-10 20:45:37 +03:00
parent d23da408a8
commit 83e28b360f
3 changed files with 35 additions and 26 deletions
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.refactoring.changeSignature
package org.jetbrains.kotlin.types.substitutions
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.JetCallableDefinitionUsage
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.TypeCheckingProcedure
import java.util.LinkedHashMap
private fun getTypeSubstitution(baseType: JetType, derivedType: JetType): LinkedHashMap<TypeConstructor, TypeProjection>? {
public fun getTypeSubstitution(baseType: JetType, derivedType: JetType): LinkedHashMap<TypeConstructor, TypeProjection>? {
val substitutedType = TypeCheckingProcedure.findCorrespondingSupertype(derivedType, baseType) ?: return null
val substitution = LinkedHashMap<TypeConstructor, TypeProjection>(substitutedType.getArguments().size())
@@ -35,7 +33,7 @@ private fun getTypeSubstitution(baseType: JetType, derivedType: JetType): Linked
return substitution
}
private fun getCallableSubstitution(
public fun getCallableSubstitution(
baseCallable: CallableDescriptor,
derivedCallable: CallableDescriptor
): MutableMap<TypeConstructor, TypeProjection>? {
@@ -50,23 +48,13 @@ private fun getCallableSubstitution(
return substitution
}
fun getTypeSubstitutor(baseType: JetType, derivedType: JetType): TypeSubstitutor? {
public fun getCallableSubstitutor(
baseCallable: CallableDescriptor,
derivedCallable: CallableDescriptor
): TypeSubstitutor? {
return getCallableSubstitution(baseCallable, derivedCallable)?.let { TypeSubstitutor.create(it) }
}
public fun getTypeSubstitutor(baseType: JetType, derivedType: JetType): TypeSubstitutor? {
return getTypeSubstitution(baseType, derivedType)?.let { TypeSubstitutor.create(it) }
}
fun getCallableSubstitutor(
baseFunction: JetCallableDefinitionUsage<*>,
derivedCallable: JetCallableDefinitionUsage<*>
): TypeSubstitutor? {
val currentBaseFunction = baseFunction.getCurrentCallableDescriptor()
val currentDerivedFunction = derivedCallable.getCurrentCallableDescriptor()
if (currentBaseFunction == null || currentDerivedFunction == null) return null
return getCallableSubstitution(currentBaseFunction, currentDerivedFunction)?.let { TypeSubstitutor.create(it) }
}
fun JetType.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
return renderer.renderType(newType)
}
@@ -22,9 +22,15 @@ import com.intellij.refactoring.changeSignature.OverriderUsageInfo
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.DeferredJavaMethodKotlinCallerUsage
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.JavaMethodKotlinUsageWithDelegate
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.JetCallableDefinitionUsage
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.KotlinCallerUsage
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.substitutions.getCallableSubstitutor
private fun JetNamedDeclaration.getDeclarationBody(): JetElement? {
return when {
@@ -56,4 +62,19 @@ public fun JetElement.isInsideOfCallerBody(allUsages: Array<out UsageInfo>): Boo
} as? JetNamedDeclaration ?: return false
val body = container.getDeclarationBody() ?: return false
return body.getTextRange().contains(getTextRange()) && container.isCaller(allUsages)
}
fun getCallableSubstitutor(
baseFunction: JetCallableDefinitionUsage<*>,
derivedCallable: JetCallableDefinitionUsage<*>
): TypeSubstitutor? {
val currentBaseFunction = baseFunction.getCurrentCallableDescriptor() ?: return null
val currentDerivedFunction = derivedCallable.getCurrentCallableDescriptor() ?: return null
return getCallableSubstitutor(currentBaseFunction, currentDerivedFunction)
}
fun JetType.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
return renderer.renderType(newType)
}
@@ -26,7 +26,6 @@ import kotlin.KotlinPackage;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
@@ -38,7 +37,6 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.ChangeSignaturePack
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetChangeInfo;
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetParameterInfo;
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetValVar;
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
import org.jetbrains.kotlin.idea.util.ShortenReferences;
import org.jetbrains.kotlin.idea.util.ShortenReferences.Options;
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken;
@@ -50,6 +48,8 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import org.jetbrains.kotlin.types.TypesPackage;
import org.jetbrains.kotlin.types.substitutions.SubstitutionsPackage;
import java.util.List;
@@ -126,7 +126,7 @@ public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageIn
if (!(classDescriptor instanceof ClassDescriptor)) return null;
typeSubstitutor = ChangeSignaturePackage.getTypeSubstitutor(
typeSubstitutor = SubstitutionsPackage.getTypeSubstitutor(
((ClassDescriptor) classDescriptor).getDefaultType(),
samCallType
);