Create actual: add delegation to secondary constructor, if needed

#KT-26518 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-11-14 19:28:41 +03:00
parent 90a2f70fd1
commit 46a5d76254
9 changed files with 39 additions and 2 deletions
@@ -187,6 +187,7 @@ interface DescriptorRendererOptions {
var normalizedVisibilities: Boolean
var renderDefaultVisibility: Boolean
var renderDefaultModality: Boolean
var renderConstructorDelegation: Boolean
var renderActualAnnotationPropertiesInPrimaryConstructor: Boolean
var uninferredTypeParameterAsName: Boolean
var overrideRenderingPolicy: OverrideRenderingPolicy
@@ -725,8 +725,8 @@ internal class DescriptorRendererImpl(
if (renderConstructorKeyword) {
builder.append(renderKeyword("constructor"))
}
val classDescriptor = constructor.containingDeclaration
if (secondaryConstructorsAsPrimary) {
val classDescriptor = constructor.containingDeclaration
if (renderConstructorKeyword) {
builder.append(" ")
}
@@ -736,6 +736,19 @@ internal class DescriptorRendererImpl(
renderValueParameters(constructor.valueParameters, constructor.hasSynthesizedParameterNames(), builder)
if (renderConstructorDelegation && !constructor.isPrimary && classDescriptor is ClassDescriptor) {
val primaryConstructor = classDescriptor.unsubstitutedPrimaryConstructor
if (primaryConstructor != null) {
val parametersWithoutDefault = primaryConstructor.valueParameters.filter {
!it.declaresDefaultValue() && it.varargElementType == null
}
if (parametersWithoutDefault.isNotEmpty()) {
builder.append(" : ").append(renderKeyword("this"))
builder.append(parametersWithoutDefault.joinToString(prefix = "(", postfix = ")", separator = ", ") { "" })
}
}
}
if (secondaryConstructorsAsPrimary) {
renderWhereSuffix(constructor.typeParameters, builder)
}
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.renderer
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.KotlinType
import java.lang.IllegalStateException
@@ -82,6 +81,7 @@ internal class DescriptorRendererOptionsImpl : DescriptorRendererOptions {
override var normalizedVisibilities by property(false)
override var renderDefaultVisibility by property(true)
override var renderDefaultModality by property(true)
override var renderConstructorDelegation by property(false)
override var renderActualAnnotationPropertiesInPrimaryConstructor by property(false)
override var uninferredTypeParameterAsName by property(false)
override var includePropertyConstant by property(false)
@@ -216,6 +216,7 @@ private val ACTUAL_RENDERER = OVERRIDE_RENDERER.withOptions {
secondaryConstructorsAsPrimary = false
renderDefaultVisibility = false
renderDefaultModality = false
renderConstructorDelegation = true
renderActualAnnotationPropertiesInPrimaryConstructor = true
}
@@ -0,0 +1,5 @@
// "Create actual class for module testModule_JVM (JVM)" "true"
expect class <caret>Constructors(s: String) {
constructor(i: Int)
}
@@ -0,0 +1,5 @@
// "Create actual class for module testModule_JVM (JVM)" "true"
expect class <caret>Constructors(s: String) {
constructor(i: Int)
}
@@ -0,0 +1 @@
// Constructors: to be implemented
@@ -0,0 +1,6 @@
// Constructors: to be implemented
actual class Constructors actual constructor(s: String) {
actual constructor(i: Int) : this() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
@@ -159,6 +159,11 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
runTest("idea/testData/multiModuleQuickFix/companionAbsence/");
}
@TestMetadata("constructorWithDelegation")
public void testConstructorWithDelegation() throws Exception {
runTest("idea/testData/multiModuleQuickFix/constructorWithDelegation/");
}
@TestMetadata("constructorWithJdk")
public void testConstructorWithJdk() throws Exception {
runTest("idea/testData/multiModuleQuickFix/constructorWithJdk/");