improvements in ksp parts

This commit is contained in:
InsanusMokrassar 2024-10-17 21:04:41 +06:00
parent 8ce7d37b72
commit 496117d517
5 changed files with 49 additions and 2 deletions

View File

@ -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`:

View File

@ -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
}

View File

@ -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 }
}

View File

@ -0,0 +1,11 @@
package dev.inmo.micro_ksp.generator
import com.google.devtools.ksp.symbol.KSClassDeclaration
fun KSClassDeclaration.resolveSubclasses(): List<KSClassDeclaration> {
return (getSealedSubclasses().flatMap {
it.resolveSubclasses()
}.ifEmpty {
sequenceOf(this)
}).toList()
}

View File

@ -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<KSAnnotated> {
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().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(),