Remove 'allowSpecialClassNames' extraction option

This commit is contained in:
Yan Zhulanow
2019-02-05 19:55:59 +03:00
parent 11dfbd5d3d
commit 6cb2146d31
19 changed files with 32 additions and 57 deletions
@@ -36,7 +36,7 @@ open class ExtractFunctionParameterTablePanel : AbstractParameterTablePanel<Para
originalParameter: Parameter,
val isReceiver: Boolean
) : AbstractParameterTablePanel.AbstractParameterInfo<Parameter>(originalParameter) {
var type = originalParameter.getParameterType(false)
var type = originalParameter.parameterType
init {
name = if (isReceiver) "<receiver>" else originalParameter.name
@@ -78,7 +78,7 @@ open class ExtractFunctionParameterTablePanel : AbstractParameterTablePanel<Para
val info = parameterInfos[row]
myEditorComponent.setCell(table, row, column)
myEditorComponent.setOptions(*info.originalParameter.getParameterTypeCandidates(false).toTypedArray())
myEditorComponent.setOptions(*info.originalParameter.getParameterTypeCandidates().toTypedArray())
myEditorComponent.setDefaultValue(info.type)
myEditorComponent.setToString { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(it as KotlinType) }
@@ -118,7 +118,7 @@ open class ExtractFunctionParameterTablePanel : AbstractParameterTablePanel<Para
val info = parameterInfos[rowIndex]
return when (columnIndex) {
AbstractParameterTablePanel.PARAMETER_NAME_COLUMN -> super.isCellEditable(rowIndex, columnIndex) && !info.isReceiver
PARAMETER_TYPE_COLUMN -> isEnabled && info.isEnabled && info.originalParameter.getParameterTypeCandidates(false).size > 1
PARAMETER_TYPE_COLUMN -> isEnabled && info.isEnabled && info.originalParameter.getParameterTypeCandidates().size > 1
else -> super.isCellEditable(rowIndex, columnIndex)
}
}
@@ -56,9 +56,9 @@ interface Parameter {
val mirrorVarName: String?
val receiverCandidate: Boolean
fun getParameterType(allowSpecialClassNames: Boolean): KotlinType
val parameterType: KotlinType
fun getParameterTypeCandidates(allowSpecialClassNames: Boolean): List<KotlinType>
fun getParameterTypeCandidates(): List<KotlinType>
fun copy(name: String, parameterType: KotlinType): Parameter
}
@@ -167,7 +167,7 @@ interface OutputValue {
val parameter: Parameter,
override val originalExpressions: List<KtExpression>
) : OutputValue {
override val valueType: KotlinType get() = parameter.getParameterType(false)
override val valueType: KotlinType get() = parameter.parameterType
}
class Initializer(
@@ -58,7 +58,6 @@ data class ExtractionOptions(
val inferUnitTypeForUnusedValues: Boolean = true,
val enableListBoxing: Boolean = false,
val extractAsProperty: Boolean = false,
val allowSpecialClassNames: Boolean = false,
val captureLocalFunctions: Boolean = false,
val canWrapInWith: Boolean = false
) {
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine
import com.intellij.openapi.diagnostic.Logger
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNamedElement
import com.intellij.psi.util.PsiTreeUtil
@@ -159,8 +158,7 @@ private fun ExtractionData.getResultTypeAndExpressions(
val resultTypes = instructions.mapNotNull(::instructionToType)
val commonSupertype = if (resultTypes.isNotEmpty()) CommonSupertypes.commonSupertype(resultTypes) else module.builtIns.defaultReturnType
val resultType =
if (options.allowSpecialClassNames) commonSupertype else commonSupertype.approximateWithResolvableType(targetScope, false)
val resultType = commonSupertype.approximateWithResolvableType(targetScope, false)
val expressions = instructions.mapNotNull { instructionToExpression(it, false) }
@@ -482,9 +480,6 @@ internal fun KotlinType.processTypeIfExtractable(
extractable
}
options.allowSpecialClassNames && typeToCheck.isSpecial() ->
extractable
typeToCheck.isError ->
false
@@ -536,7 +531,7 @@ internal class MutableParameter(
} else originalType
}
private val parameterTypeCandidates: List<KotlinType> by lazy {
private val allParameterTypeCandidates: List<KotlinType> by lazy {
writable = false
val typePredicate = and(typePredicates)
@@ -562,17 +557,12 @@ internal class MutableParameter(
typeSet.toList()
}
override fun getParameterTypeCandidates(allowSpecialClassNames: Boolean): List<KotlinType> {
return if (!allowSpecialClassNames) {
parameterTypeCandidates.filter { it.isExtractable(targetScope) }
} else {
parameterTypeCandidates
}
override fun getParameterTypeCandidates(): List<KotlinType> {
return allParameterTypeCandidates.filter { it.isExtractable(targetScope) }
}
override fun getParameterType(allowSpecialClassNames: Boolean): KotlinType {
return getParameterTypeCandidates(allowSpecialClassNames).firstOrNull() ?: defaultType
}
override val parameterType: KotlinType
get() = getParameterTypeCandidates().firstOrNull() ?: defaultType
override fun copy(name: String, parameterType: KotlinType): Parameter = DelegatingParameter(this, name, parameterType)
}
@@ -580,10 +570,9 @@ internal class MutableParameter(
private class DelegatingParameter(
val original: Parameter,
override val name: String,
val parameterType: KotlinType
override val parameterType: KotlinType
) : Parameter by original {
override fun copy(name: String, parameterType: KotlinType): Parameter = DelegatingParameter(original, name, parameterType)
override fun getParameterType(allowSpecialClassNames: Boolean) = parameterType
}
private fun ExtractionData.checkDeclarationsMovingOutOfScope(
@@ -46,7 +46,6 @@ import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.Weak
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtPsiFactory.CallableBuilder
import org.jetbrains.kotlin.psi.codeFragmentUtil.DEBUG_TYPE_REFERENCE_STRING
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.DescriptorUtils
@@ -97,16 +96,10 @@ private fun buildSignature(config: ExtractionGeneratorConfiguration, renderer: D
}
)
fun KotlinType.typeAsString(): String {
return if (config.descriptor.extractionData.options.allowSpecialClassNames && isSpecial()) {
DEBUG_TYPE_REFERENCE_STRING
} else {
renderer.renderType(this)
}
}
fun KotlinType.typeAsString() = renderer.renderType(this)
config.descriptor.receiverParameter?.let {
val receiverType = it.getParameterType(config.descriptor.extractionData.options.allowSpecialClassNames)
val receiverType = it.parameterType
val receiverTypeAsString = receiverType.typeAsString()
receiver(if (receiverType.isFunctionType) "($receiverTypeAsString)" else receiverTypeAsString)
}
@@ -114,10 +107,7 @@ private fun buildSignature(config: ExtractionGeneratorConfiguration, renderer: D
name(config.generatorOptions.dummyName ?: config.descriptor.name)
config.descriptor.parameters.forEach { parameter ->
param(
parameter.name,
parameter.getParameterType(config.descriptor.extractionData.options.allowSpecialClassNames).typeAsString()
)
param(parameter.name, parameter.parameterType.typeAsString())
}
with(config.descriptor.returnType) {
@@ -225,8 +215,7 @@ fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
return if (matched) newControlFlow else null
}
val unifierParameters =
parameters.map { UnifierParameter(it.originalDescriptor, it.getParameterType(extractionData.options.allowSpecialClassNames)) }
val unifierParameters = parameters.map { UnifierParameter(it.originalDescriptor, it.parameterType) }
val unifier = KotlinPsiUnifier(unifierParameters, true)
@@ -149,15 +149,13 @@ internal fun ExtractionData.inferParametersInfo(
val existingParameterNames = hashSetOf<String>()
for ((descriptorToExtract, parameter) in extractedDescriptorToParameter) {
if (!parameter
.getParameterType(options.allowSpecialClassNames)
.parameterType
.processTypeIfExtractable(info.typeParameters, info.nonDenotableTypes, options, targetScope)
) continue
with(parameter) {
if (currentName == null) {
currentName =
KotlinNameSuggester.suggestNamesByType(getParameterType(options.allowSpecialClassNames), varNameValidator, "p")
.first()
currentName = KotlinNameSuggester.suggestNamesByType(parameterType, varNameValidator, "p").first()
}
require(currentName != null)
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// OPTIONS: true, true, false, false, false, false
// OPTIONS: true, true, false, false, false
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// OPTIONS: true, true, false, false, false, false
// OPTIONS: true, true, false, false, false
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: local final fun baz(m: kotlin.Int): kotlin.Int defined in foo.bar
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_TYPES: (m: kotlin.Int) -> kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: local final fun baz(m: kotlin.Int): kotlin.Int defined in foo.bar
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_TYPES: (m: kotlin.Int) -> kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_TYPES: kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_TYPES: kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar1(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar2(m: kotlin.Int): kotlin.Int defined in foo
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar1(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar2(m: kotlin.Int): kotlin.Int defined in foo
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_TYPES: kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: local final fun kotlin.Int.bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_TYPES: kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: local final fun bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_TYPES: (m: kotlin.Int) -> kotlin.Int
@@ -1,4 +1,4 @@
// OPTIONS: true, false, false, false, true, false
// OPTIONS: true, false, false, true, false
// PARAM_DESCRIPTOR: local final fun bar(m: kotlin.Int): kotlin.Int defined in foo
// PARAM_DESCRIPTOR: value-parameter n: kotlin.Int defined in foo
// PARAM_TYPES: (m: kotlin.Int) -> kotlin.Int
@@ -435,7 +435,7 @@ fun doExtractFunction(fixture: CodeInsightTestFixture, file: KtFile) {
val allParameters = listOfNotNull(descriptor.receiverParameter) + descriptor.parameters
val actualDescriptors = allParameters.map { renderer.render(it.originalDescriptor) }.joinToString()
val actualTypes = allParameters.map {
it.getParameterTypeCandidates(false).map { renderer.renderType(it) }.joinToString(", ", "[", "]")
it.getParameterTypeCandidates().map { renderer.renderType(it) }.joinToString(", ", "[", "]")
}.joinToString()
if (actualNames.size != 1 || expectedNames.isNotEmpty()) {