diff --git a/CHANGELOG.md b/CHANGELOG.md index 606f65786c2..6af660b0f2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 0.22.6 +* `KSP`: + * `Generator`: + * Add extension `KSClassDeclaration.buildSubFileName` + * Add extension `KSClassDeclaration.companion` + * Add extension `KSClassDeclaration.resolveSubclasses` + * `Sealed`: + * Improvements + ## 0.22.5 * `Versions`: diff --git a/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt b/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt new file mode 100644 index 00000000000..5f854a166f2 --- /dev/null +++ b/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt @@ -0,0 +1,13 @@ +package dev.inmo.micro_ksp.generator + +import com.google.devtools.ksp.symbol.KSClassDeclaration + +val KSClassDeclaration.buildSubFileName: String + get() { + val parentDeclarationCaptured = parentDeclaration + val simpleNameString = simpleName.asString() + return when (parentDeclarationCaptured) { + is KSClassDeclaration -> parentDeclarationCaptured.buildSubFileName + else -> "" + } + simpleNameString + } diff --git a/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt b/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt new file mode 100644 index 00000000000..2f7f859736d --- /dev/null +++ b/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt @@ -0,0 +1,8 @@ +package dev.inmo.micro_ksp.generator + +import com.google.devtools.ksp.symbol.KSClassDeclaration + +val KSClassDeclaration.companion + get() = declarations.firstNotNullOfOrNull { + (it as? KSClassDeclaration)?.takeIf { it.isCompanionObject } + } diff --git a/ksp/generator/src/main/kotlin/ResolveSubclasses.kt b/ksp/generator/src/main/kotlin/ResolveSubclasses.kt new file mode 100644 index 00000000000..ca6acc8c9b5 --- /dev/null +++ b/ksp/generator/src/main/kotlin/ResolveSubclasses.kt @@ -0,0 +1,11 @@ +package dev.inmo.micro_ksp.generator + +import com.google.devtools.ksp.symbol.KSClassDeclaration + +fun KSClassDeclaration.resolveSubclasses(): List { + return (getSealedSubclasses().flatMap { + it.resolveSubclasses() + }.ifEmpty { + sequenceOf(this) + }).toList() +} diff --git a/ksp/sealed/generator/src/main/kotlin/Processor.kt b/ksp/sealed/generator/src/main/kotlin/Processor.kt index 6eca44452a7..99196279576 100644 --- a/ksp/sealed/generator/src/main/kotlin/Processor.kt +++ b/ksp/sealed/generator/src/main/kotlin/Processor.kt @@ -15,6 +15,8 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.asTypeName import com.squareup.kotlinpoet.ksp.toClassName +import dev.inmo.micro_ksp.generator.buildSubFileName +import dev.inmo.micro_ksp.generator.companion import dev.inmo.micro_ksp.generator.findSubClasses import dev.inmo.micro_ksp.generator.writeFile import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround @@ -93,7 +95,10 @@ class Processor( ) addFunction( FunSpec.builder("values").apply { - receiver(ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion")) + val companion = ksClassDeclaration.takeIf { it.isCompanionObject } ?.toClassName() + ?: ksClassDeclaration.companion ?.toClassName() + ?: ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion") + receiver(companion) returns(setType) addCode( CodeBlock.of( @@ -107,7 +112,9 @@ class Processor( @OptIn(KspExperimental::class) override fun process(resolver: Resolver): List { (resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance().forEach { - val prefix = it.getAnnotationsByType(GenerateSealedWorkaround::class).first().prefix + val prefix = it.getAnnotationsByType(GenerateSealedWorkaround::class).first().prefix.takeIf { + it.isNotEmpty() + } ?: it.buildSubFileName.replaceFirst(it.simpleName.asString(), "") it.writeFile(prefix = prefix, suffix = "SealedWorkaround") { FileSpec.builder( it.packageName.asString(),