Create From Usage: Always try to shorten return type reference if there is one
This commit is contained in:
+21
-19
@@ -95,6 +95,9 @@ class TypeCandidate(val theType: JetType, scope: JetScope? = null) {
|
|||||||
override fun toString() = theType.toString()
|
override fun toString() = theType.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun List<TypeCandidate>.getTypeByRenderedType(renderedType: String): JetType? =
|
||||||
|
firstOrNull { it.renderedType == renderedType }?.theType
|
||||||
|
|
||||||
class CallableBuilderConfiguration(
|
class CallableBuilderConfiguration(
|
||||||
val callableInfo: CallableInfo,
|
val callableInfo: CallableInfo,
|
||||||
val originalExpression: JetExpression,
|
val originalExpression: JetExpression,
|
||||||
@@ -377,10 +380,9 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
|||||||
return allTypeParametersNotInScope.zip(typeParameterNames).toMap()
|
return allTypeParametersNotInScope.zip(typeParameterNames).toMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupTypeReferencesForShortening(
|
private fun setupTypeReferencesForShortening(declaration: JetCallableDeclaration,
|
||||||
declaration: JetCallableDeclaration,
|
typeRefsToShorten: MutableList<JetTypeReference>,
|
||||||
typeRefsToShorten: MutableList<JetTypeReference>, parameterTypeExpressions: List<TypeExpression>,
|
parameterTypeExpressions: List<TypeExpression>) {
|
||||||
returnTypeExpression: TypeExpression?) {
|
|
||||||
if (isExtension) {
|
if (isExtension) {
|
||||||
val receiverTypeRef = JetPsiFactory(declaration).createType(receiverTypeCandidate!!.theType.renderLong(typeParameterNameMap))
|
val receiverTypeRef = JetPsiFactory(declaration).createType(receiverTypeCandidate!!.theType.renderLong(typeParameterNameMap))
|
||||||
replaceWithLongerName(receiverTypeRef, receiverTypeCandidate.theType)
|
replaceWithLongerName(receiverTypeRef, receiverTypeCandidate.theType)
|
||||||
@@ -391,18 +393,16 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (returnTypeExpression != null) {
|
val returnTypeRef = declaration.getTypeReference()
|
||||||
val returnTypeRef = declaration.getTypeReference()
|
if (returnTypeRef != null) {
|
||||||
if (returnTypeRef != null) {
|
val returnType = typeCandidates[config.callableInfo.returnTypeInfo]!!.getTypeByRenderedType(
|
||||||
val returnType = returnTypeExpression.getTypeFromSelection(
|
returnTypeRef.getText()
|
||||||
returnTypeRef.getText()
|
?: throw AssertionError("Expression for return type shouldn't be empty: declaration = ${declaration.getText()}")
|
||||||
?: throw AssertionError("Expression for return type shouldn't be empty: declaration = ${declaration.getText()}")
|
)
|
||||||
)
|
if (returnType != null) {
|
||||||
if (returnType != null) {
|
// user selected a given type
|
||||||
// user selected a given type
|
replaceWithLongerName(returnTypeRef, returnType)
|
||||||
replaceWithLongerName(returnTypeRef, returnType)
|
typeRefsToShorten.add(declaration.getTypeReference()!!)
|
||||||
typeRefsToShorten.add(declaration.getTypeReference()!!)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,7 +412,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
|||||||
for ((i, parameter) in valueParameters.stream().withIndices()) {
|
for ((i, parameter) in valueParameters.stream().withIndices()) {
|
||||||
val parameterTypeRef = parameter.getTypeReference()
|
val parameterTypeRef = parameter.getTypeReference()
|
||||||
if (parameterTypeRef != null) {
|
if (parameterTypeRef != null) {
|
||||||
val parameterType = parameterTypeExpressions[i].getTypeFromSelection(
|
val parameterType = parameterTypeExpressions[i].typeCandidates.getTypeByRenderedType(
|
||||||
parameterTypeRef.getText()
|
parameterTypeRef.getText()
|
||||||
?: throw AssertionError("Expression for parameter type shouldn't be empty: declaration = ${declaration.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) {
|
if (declaration is JetProperty) {
|
||||||
setupValVarTemplate(builder, declaration)
|
setupValVarTemplate(builder, declaration)
|
||||||
}
|
}
|
||||||
val returnTypeExpression = if (skipReturnType) null else setupReturnTypeTemplate(builder, declaration)
|
if (!skipReturnType) {
|
||||||
|
setupReturnTypeTemplate(builder, declaration)
|
||||||
|
}
|
||||||
val parameterTypeExpressions =
|
val parameterTypeExpressions =
|
||||||
setupParameterTypeTemplates(builder, declaration.getValueParameterList()?.getParameters() ?: Collections.emptyList())
|
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)
|
// change short type names to fully qualified ones (to be shortened below)
|
||||||
setupTypeReferencesForShortening(newDeclaration, typeRefsToShorten, parameterTypeExpressions, returnTypeExpression)
|
setupTypeReferencesForShortening(newDeclaration, typeRefsToShorten, parameterTypeExpressions)
|
||||||
ShortenReferences.process(typeRefsToShorten)
|
ShortenReferences.process(typeRefsToShorten)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -15,6 +15,8 @@ import java.util.HashSet
|
|||||||
import org.jetbrains.jet.plugin.refactoring.CollectingValidator
|
import org.jetbrains.jet.plugin.refactoring.CollectingValidator
|
||||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||||
import org.jetbrains.jet.lang.types.JetType
|
import org.jetbrains.jet.lang.types.JetType
|
||||||
|
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Special <code>Expression</code> for parameter names based on its type.
|
* Special <code>Expression</code> for parameter names based on its type.
|
||||||
@@ -90,9 +92,6 @@ private class TypeExpression(public val typeCandidates: List<TypeCandidate>) : E
|
|||||||
override fun calculateQuickResult(context: ExpressionContext?) = calculateResult(context)
|
override fun calculateQuickResult(context: ExpressionContext?) = calculateResult(context)
|
||||||
|
|
||||||
override fun calculateLookupItems(context: ExpressionContext?) = cachedLookupElements
|
override fun calculateLookupItems(context: ExpressionContext?) = cachedLookupElements
|
||||||
|
|
||||||
public fun getTypeFromSelection(selection: String): JetType? =
|
|
||||||
typeCandidates.firstOrNull { it.renderedType == selection }?.theType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user