Primary constructor to secondary: always try to build constructor body #KT-14475 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-10-24 20:33:24 +03:00
parent d6eee65d72
commit a74f010e0f
4 changed files with 52 additions and 21 deletions
@@ -89,7 +89,6 @@ class ConvertPrimaryConstructorToSecondaryIntention : SelfTargetingIntention<KtP
superTypeEntry.replace(factory.createSuperTypeEntry(superTypeEntry.typeReference!!.text)) superTypeEntry.replace(factory.createSuperTypeEntry(superTypeEntry.typeReference!!.text))
} }
} }
if (element.valueParameters.firstOrNull { it.hasValOrVar() } != null || initializerMap.isNotEmpty()) {
val valueParameterInitializers = element.valueParameters.filter { it.hasValOrVar() }.joinToString(separator = "\n") { val valueParameterInitializers = element.valueParameters.filter { it.hasValOrVar() }.joinToString(separator = "\n") {
val name = it.name!! val name = it.name!!
"this.$name = $name" "this.$name = $name"
@@ -113,8 +112,9 @@ class ConvertPrimaryConstructorToSecondaryIntention : SelfTargetingIntention<KtP
} ?: "" } ?: ""
} }
} }
blockBody(listOf(valueParameterInitializers, classBodyInitializers) val allInitializers = listOf(valueParameterInitializers, classBodyInitializers).filter(String::isNotEmpty)
.filter(String::isNotEmpty).joinToString(separator = "\n")) if (allInitializers.isNotEmpty()) {
blockBody(allInitializers.joinToString(separator = "\n"))
} }
}.asString() }.asString()
) )
@@ -0,0 +1,12 @@
fun println(arg: Int) = arg
class My<caret>() {
val x = 1
val y = 2
init {
println(x)
println(y)
}
}
@@ -0,0 +1,13 @@
fun println(arg: Int) = arg
class My {
constructor() {
println(x)
println(y)
}
val x: Int = 1
val y: Int = 2
}
@@ -4537,6 +4537,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("initWithoutAssignments.kt")
public void testInitWithoutAssignments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPrimaryConstructorToSecondary/initWithoutAssignments.kt");
doTest(fileName);
}
@TestMetadata("paramsAndProperties.kt") @TestMetadata("paramsAndProperties.kt")
public void testParamsAndProperties() throws Exception { public void testParamsAndProperties() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPrimaryConstructorToSecondary/paramsAndProperties.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPrimaryConstructorToSecondary/paramsAndProperties.kt");