diff --git a/compiler/src/main/kotlin/com/r0adkll/kimchi/processors/MergeComponentSymbolProcessor.kt b/compiler/src/main/kotlin/com/r0adkll/kimchi/processors/MergeComponentSymbolProcessor.kt index 4059650..075b7fc 100644 --- a/compiler/src/main/kotlin/com/r0adkll/kimchi/processors/MergeComponentSymbolProcessor.kt +++ b/compiler/src/main/kotlin/com/r0adkll/kimchi/processors/MergeComponentSymbolProcessor.kt @@ -53,6 +53,9 @@ internal class MergeComponentSymbolProcessor( } } + private val isGenerateCompanionExtensionsEnabled: Boolean + get() = env.options["me.tatarka.inject.generateCompanionExtensions"] == "true" + private var deferred: MutableList = mutableListOf() override fun process(resolver: Resolver): List { @@ -109,12 +112,17 @@ internal class MergeComponentSymbolProcessor( // Create Companion extension method on original element to create this component val constructorParameters = getConstructorParameters(element) + val createFunction = if (isGenerateCompanionExtensionsEnabled) { + "%T.create" + } else { + "%T::class.create" + } addFunction( FunSpec.builder("create$classSimpleName") .receiver(element.toClassName().nestedClass("Companion")) .addParameters(constructorParameters) .addStatement( - "return %T.create(${constructorParameters.joinToString { "%L" }})", + "return $createFunction(${constructorParameters.joinToString { "%L" }})", className, *constructorParameters.map { it.name }.toTypedArray(), ) @@ -253,7 +261,7 @@ internal class MergeComponentSymbolProcessor( addSuperinterface(subcomponent.factoryClass.toClassName()) // Generate the factory creation function overload to generate this subcomponent - addFunction(subcomponent.createFactoryFunctionOverload()) + addFunction(subcomponent.createFactoryFunctionOverload(isGenerateCompanionExtensionsEnabled)) // Generate the Subcomponent addType( diff --git a/compiler/src/main/kotlin/com/r0adkll/kimchi/util/ksp/SubcomponentDeclaration.kt b/compiler/src/main/kotlin/com/r0adkll/kimchi/util/ksp/SubcomponentDeclaration.kt index 4f31d41..bc1e7dd 100644 --- a/compiler/src/main/kotlin/com/r0adkll/kimchi/util/ksp/SubcomponentDeclaration.kt +++ b/compiler/src/main/kotlin/com/r0adkll/kimchi/util/ksp/SubcomponentDeclaration.kt @@ -40,7 +40,9 @@ class SubcomponentDeclaration( ) } - fun createFactoryFunctionOverload(): FunSpec = with(factoryClass) { + fun createFactoryFunctionOverload( + isGenerateCompanionExtensionsEnabled: Boolean, + ): FunSpec = with(factoryClass) { return FunSpec.buildFun(factoryFunction.simpleName.asString()) { addModifiers(KModifier.OVERRIDE) @@ -51,8 +53,13 @@ class SubcomponentDeclaration( // Build the return statement constructing the expected merged subcomponent, including // parent. + val componentCreationFunction = if (isGenerateCompanionExtensionsEnabled) { + "%L.create" + } else { + "%L::class.create" + } addStatement( - "return %L.create(${factoryParameters.joinToString { "%L" }}" + + "return $componentCreationFunction(${factoryParameters.joinToString { "%L" }}" + "${if (factoryParameters.isNotEmpty()) ", " else ""}this)", subcomponentSimpleName, *factoryParameters.map { it.name }.toTypedArray(), diff --git a/sample/shared/build.gradle.kts b/sample/shared/build.gradle.kts index 2fc04b5..f0baa5a 100644 --- a/sample/shared/build.gradle.kts +++ b/sample/shared/build.gradle.kts @@ -63,10 +63,6 @@ composeCompiler { android { namespace = "com.r0adkll.kimchi.restaurant" } -ksp { - arg("me.tatarka.inject.generateCompanionExtensions", "true") -} - private fun Project.addKspDependencyForAllTargets(dependencyNotation: Any) { val kmpExtension = extensions.getByType() dependencies {