From 9206b571f44a080c555686c6a6c0edf765e00c4f Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 8 Oct 2014 17:40:03 +0400 Subject: [PATCH] Create From Usage: Always try to shorten return type reference if there is one --- .../callableBuilder/CallableBuilder.kt | 40 ++++++++++--------- .../callableBuilder/templateExpressions.kt | 5 +-- 2 files changed, 23 insertions(+), 22 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 1f8899d5080..9a4ff38ae29 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 @@ -95,6 +95,9 @@ class TypeCandidate(val theType: JetType, scope: JetScope? = null) { override fun toString() = theType.toString() } +fun List.getTypeByRenderedType(renderedType: String): JetType? = + firstOrNull { it.renderedType == renderedType }?.theType + class CallableBuilderConfiguration( val callableInfo: CallableInfo, val originalExpression: JetExpression, @@ -377,10 +380,9 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { return allTypeParametersNotInScope.zip(typeParameterNames).toMap() } - private fun setupTypeReferencesForShortening( - declaration: JetCallableDeclaration, - typeRefsToShorten: MutableList, parameterTypeExpressions: List, - returnTypeExpression: TypeExpression?) { + private fun setupTypeReferencesForShortening(declaration: JetCallableDeclaration, + typeRefsToShorten: MutableList, + parameterTypeExpressions: List) { if (isExtension) { val receiverTypeRef = JetPsiFactory(declaration).createType(receiverTypeCandidate!!.theType.renderLong(typeParameterNameMap)) replaceWithLongerName(receiverTypeRef, receiverTypeCandidate.theType) @@ -391,18 +393,16 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { } } - if (returnTypeExpression != null) { - val returnTypeRef = declaration.getTypeReference() - if (returnTypeRef != null) { - val returnType = returnTypeExpression.getTypeFromSelection( - returnTypeRef.getText() - ?: throw AssertionError("Expression for return type shouldn't be empty: declaration = ${declaration.getText()}") - ) - if (returnType != null) { - // user selected a given type - replaceWithLongerName(returnTypeRef, returnType) - typeRefsToShorten.add(declaration.getTypeReference()!!) - } + val returnTypeRef = declaration.getTypeReference() + if (returnTypeRef != null) { + val returnType = typeCandidates[config.callableInfo.returnTypeInfo]!!.getTypeByRenderedType( + returnTypeRef.getText() + ?: throw AssertionError("Expression for return type shouldn't be empty: declaration = ${declaration.getText()}") + ) + if (returnType != null) { + // user selected a given type + replaceWithLongerName(returnTypeRef, returnType) + typeRefsToShorten.add(declaration.getTypeReference()!!) } } @@ -412,7 +412,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { for ((i, parameter) in valueParameters.stream().withIndices()) { val parameterTypeRef = parameter.getTypeReference() if (parameterTypeRef != null) { - val parameterType = parameterTypeExpressions[i].getTypeFromSelection( + val parameterType = parameterTypeExpressions[i].typeCandidates.getTypeByRenderedType( parameterTypeRef.getText() ?: throw AssertionError("Expression for parameter type shouldn't be empty: declaration = ${declaration.getText()}") ) @@ -555,7 +555,9 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { if (declaration is JetProperty) { setupValVarTemplate(builder, declaration) } - val returnTypeExpression = if (skipReturnType) null else setupReturnTypeTemplate(builder, declaration) + if (!skipReturnType) { + setupReturnTypeTemplate(builder, declaration) + } val parameterTypeExpressions = setupParameterTypeTemplates(builder, declaration.getValueParameterList()?.getParameters() ?: Collections.emptyList()) @@ -598,7 +600,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { } // change short type names to fully qualified ones (to be shortened below) - setupTypeReferencesForShortening(newDeclaration, typeRefsToShorten, parameterTypeExpressions, returnTypeExpression) + setupTypeReferencesForShortening(newDeclaration, typeRefsToShorten, parameterTypeExpressions) ShortenReferences.process(typeRefsToShorten) } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/templateExpressions.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/templateExpressions.kt index 3daf85bac04..1d467f9eef6 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/templateExpressions.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/templateExpressions.kt @@ -15,6 +15,8 @@ import java.util.HashSet import org.jetbrains.jet.plugin.refactoring.CollectingValidator import com.intellij.codeInsight.lookup.LookupElementBuilder import org.jetbrains.jet.lang.types.JetType +import org.jetbrains.jet.lang.psi.JetCallableDeclaration +import java.util.Collections /** * Special Expression for parameter names based on its type. @@ -90,9 +92,6 @@ private class TypeExpression(public val typeCandidates: List) : E override fun calculateQuickResult(context: ExpressionContext?) = calculateResult(context) override fun calculateLookupItems(context: ExpressionContext?) = cachedLookupElements - - public fun getTypeFromSelection(selection: String): JetType? = - typeCandidates.firstOrNull { it.renderedType == selection }?.theType } /**