From 06f7e7700604894edf8d10f28539af44c767015a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 15 May 2018 13:09:49 +0300 Subject: [PATCH] Introduce new type renderers (no parameters & built-in annotations) --- .../idea/util/IdeDescriptorRenderers.kt | 15 ++++++++++++++ .../kotlin/idea/quickfix/CastExpressionFix.kt | 4 ++-- .../quickfix/ChangeCallableReturnTypeFix.kt | 4 ++-- .../ChangeFunctionLiteralReturnTypeFix.kt | 4 ++-- .../idea/quickfix/ChangeVariableTypeFix.kt | 4 ++-- .../QuickFixFactoryForTypeMismatchError.kt | 20 ++----------------- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/IdeDescriptorRenderers.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/IdeDescriptorRenderers.kt index a66bc198dd7..26ce4dcf489 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/IdeDescriptorRenderers.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/IdeDescriptorRenderers.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.util +import org.jetbrains.kotlin.descriptors.annotations.BuiltInAnnotationDescriptor import org.jetbrains.kotlin.renderer.* import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.isDynamic @@ -57,6 +58,20 @@ object IdeDescriptorRenderers { typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) } } + @JvmField val SOURCE_CODE_TYPES: DescriptorRenderer = BASE.withOptions { + classifierNamePolicy = ClassifierNamePolicy.SOURCE_CODE_QUALIFIED + typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) } + annotationFilter = { it !is BuiltInAnnotationDescriptor } + parameterNamesInFunctionalTypes = false + } + + @JvmField val SOURCE_CODE_TYPES_WITH_SHORT_NAMES: DescriptorRenderer = BASE.withOptions { + classifierNamePolicy = ClassifierNamePolicy.SHORT + typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) } + modifiers -= DescriptorRendererModifier.ANNOTATIONS + parameterNamesInFunctionalTypes = false + } + @JvmField val SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION: DescriptorRenderer = BASE.withOptions { classifierNamePolicy = ClassifierNamePolicy.SOURCE_CODE_QUALIFIED typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES_NOT_NULL(unwrapAnonymousType(it)) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt index 113d089179a..b62566e02f0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt @@ -34,8 +34,8 @@ import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf import org.jetbrains.kotlin.types.typeUtil.makeNullable class CastExpressionFix(element: KtExpression, type: KotlinType) : KotlinQuickFixAction(element) { - private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type) - private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type) + private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_TYPES_WITH_SHORT_NAMES.renderType(type) + private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE_TYPES.renderType(type) private val upOrDownCast: Boolean = run { val expressionType = element.analyze(BodyResolveMode.PARTIAL).getType(element) expressionType != null && (type.isSubtypeOf(expressionType) || expressionType.isSubtypeOf(type)) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt index 8cc23a30cd7..90630569dec 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt @@ -55,8 +55,8 @@ abstract class ChangeCallableReturnTypeFix( private val changeFunctionLiteralReturnTypeFix: ChangeFunctionLiteralReturnTypeFix? private val typeContainsError = ErrorUtils.containsErrorType(type) - private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type) - private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type) + private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_TYPES_WITH_SHORT_NAMES.renderType(type) + private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE_TYPES.renderType(type) private val isUnitType = type.isUnit() init { diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt index 1c4bb38b4c5..da4e26caf8b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt @@ -41,8 +41,8 @@ class ChangeFunctionLiteralReturnTypeFix( type: KotlinType ) : KotlinQuickFixAction(functionLiteralExpression) { - private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type) - private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type) + private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_TYPES_WITH_SHORT_NAMES.renderType(type) + private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE_TYPES.renderType(type) private val functionLiteralReturnTypeRef: KtTypeReference? get() = element?.functionLiteral?.typeReference private val appropriateQuickFix = createAppropriateQuickFix(functionLiteralExpression, type) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt index 94d62198bea..dc083d7d0e1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableTypeFix.kt @@ -41,8 +41,8 @@ import java.util.* open class ChangeVariableTypeFix(element: KtVariableDeclaration, type: KotlinType) : KotlinQuickFixAction(element) { private val typeContainsError = ErrorUtils.containsErrorType(type) - private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type) - private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type) + private val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_TYPES_WITH_SHORT_NAMES.renderType(type) + private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE_TYPES.renderType(type) open fun variablePresentation(): String? { val element = element!! diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt index eb4e039b079..b2d1895b30e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt @@ -21,9 +21,6 @@ import com.intellij.openapi.diagnostic.Logger import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.builtins.* import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl -import org.jetbrains.kotlin.descriptors.annotations.BuiltInAnnotationDescriptor import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages @@ -46,22 +43,12 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinTypeFactory -import org.jetbrains.kotlin.types.TypeProjectionImpl import org.jetbrains.kotlin.types.typeUtil.* import java.util.* //TODO: should use change signature to deal with cases of multiple overridden descriptors class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() { - private fun KotlinType.dropParameterNameAnnotations(): KotlinType { - return KotlinTypeFactory.simpleNotNullType(annotations, constructor.declarationDescriptor as ClassDescriptor, arguments.map { - TypeProjectionImpl( - it.projectionKind, - it.type.replaceAnnotations(AnnotationsImpl(it.type.annotations.filter { it !is BuiltInAnnotationDescriptor })) - ) - }) - } - private fun KotlinType.reflectToRegularFunctionType(): KotlinType { val isTypeAnnotatedWithExtensionFunctionType = annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.extensionFunctionType) != null val parameterCount = if (isTypeAnnotatedWithExtensionFunctionType) arguments.size - 2 else arguments.size - 1 @@ -168,12 +155,9 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() { val scope = callable.getResolutionScope(context, callable.getResolutionFacade()) val typeToInsert = expressionType.approximateWithResolvableType(scope, false) if (typeToInsert.constructor.declarationDescriptor?.getFunctionalClassKind() == FunctionClassDescriptor.Kind.KFunction) { - val reflectType = typeToInsert.dropParameterNameAnnotations() - actions.add(createFix(callable, reflectType.reflectToRegularFunctionType())) - actions.add(createFix(callable, reflectType)) - } else { - actions.add(createFix(callable, typeToInsert)) + actions.add(createFix(callable, typeToInsert.reflectToRegularFunctionType())) } + actions.add(createFix(callable, typeToInsert)) } // Property initializer type mismatch property type: