Extract function: avoid same parameter names
This commit is contained in:
+13
@@ -126,6 +126,7 @@ internal fun ExtractionData.inferParametersInfo(
|
||||
NewDeclarationNameValidator.Target.VARIABLES
|
||||
)
|
||||
|
||||
val existingParameterNames = hashSetOf<String>()
|
||||
for ((descriptorToExtract, parameter) in extractedDescriptorToParameter) {
|
||||
if (!parameter
|
||||
.getParameterType(options.allowSpecialClassNames)
|
||||
@@ -135,8 +136,20 @@ internal fun ExtractionData.inferParametersInfo(
|
||||
if (currentName == null) {
|
||||
currentName = KotlinNameSuggester.suggestNamesByType(getParameterType(options.allowSpecialClassNames), varNameValidator, "p").first()
|
||||
}
|
||||
|
||||
require(currentName != null)
|
||||
|
||||
if ("$currentName" in existingParameterNames) {
|
||||
var index = 0
|
||||
while ("$currentName$index" in existingParameterNames) {
|
||||
index++
|
||||
}
|
||||
currentName = "$currentName$index"
|
||||
}
|
||||
|
||||
mirrorVarName = if (descriptorToExtract in modifiedVarDescriptors) KotlinNameSuggester.suggestNameByName(name, varNameValidator) else null
|
||||
info.parameters.add(this)
|
||||
currentName?.let { existingParameterNames += it }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -27,5 +27,5 @@ fun <T> T.inlineBlock(t: T, block: T.() -> Unit) {
|
||||
// EXPRESSION: this
|
||||
// RESULT: 2: I
|
||||
|
||||
// EXPRESSION: this@c
|
||||
// RESULT: 3: I
|
||||
// EXPRESSION: this + this@a + this@b + this@c
|
||||
// RESULT: 12: I
|
||||
@@ -0,0 +1,20 @@
|
||||
// SUGGESTED_NAMES: s, getA
|
||||
// PARAM_TYPES: kotlin.String
|
||||
// PARAM_TYPES: kotlin.String, kotlin.Comparable<kotlin.String>, kotlin.CharSequence, java.io.Serializable, kotlin.Any
|
||||
// PARAM_TYPES: kotlin.String, kotlin.Comparable<kotlin.String>, kotlin.CharSequence, java.io.Serializable, kotlin.Any
|
||||
// PARAM_DESCRIPTOR: local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo.<anonymous>.<anonymous>, local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo.<anonymous>, local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo
|
||||
class Foo {
|
||||
fun foo() {
|
||||
block("a") a@ {
|
||||
block("b") b@ {
|
||||
block("c") c@ {
|
||||
val a = <selection>this@c + this@b + this@a + this + this@Foo.toString()</selection>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun <T> block(t: T, block: T.() -> Unit) {
|
||||
t.block()
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// SUGGESTED_NAMES: s, getA
|
||||
// PARAM_TYPES: kotlin.String
|
||||
// PARAM_TYPES: kotlin.String, kotlin.Comparable<kotlin.String>, kotlin.CharSequence, java.io.Serializable, kotlin.Any
|
||||
// PARAM_TYPES: kotlin.String, kotlin.Comparable<kotlin.String>, kotlin.CharSequence, java.io.Serializable, kotlin.Any
|
||||
// PARAM_DESCRIPTOR: local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo.<anonymous>.<anonymous>, local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo.<anonymous>, local final fun kotlin.String.<anonymous>(): kotlin.Unit defined in Foo.foo
|
||||
class Foo {
|
||||
fun foo() {
|
||||
block("a") a@ {
|
||||
block("b") b@ {
|
||||
block("c") c@ {
|
||||
val a = s(this@c, this@b, this@a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun s(s: String, s0: String, s1: String) = s + s0 + s1 + s + this@Foo.toString()
|
||||
}
|
||||
|
||||
private inline fun <T> block(t: T, block: T.() -> Unit) {
|
||||
t.block()
|
||||
}
|
||||
+5
@@ -996,6 +996,11 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
|
||||
runTest("idea/testData/refactoring/extractFunction/basic/refInReturn.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("sameParameterNames.kt")
|
||||
public void testSameParameterNames() throws Exception {
|
||||
runTest("idea/testData/refactoring/extractFunction/basic/sameParameterNames.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("selectedConstructor.kt")
|
||||
public void testSelectedConstructor() throws Exception {
|
||||
runTest("idea/testData/refactoring/extractFunction/basic/selectedConstructor.kt");
|
||||
|
||||
Reference in New Issue
Block a user