Merge pull request #501 from InsanusMokrassar/0.22.6

0.22.6
This commit is contained in:
InsanusMokrassar 2024-10-17 22:40:53 +06:00 committed by GitHub
commit fcaa327660
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 53 additions and 4 deletions

View File

@ -1,5 +1,15 @@
# Changelog # Changelog
## 0.22.6
* `KSP`:
* `Generator`:
* Add extension `KSClassDeclaration.buildSubFileName`
* Add extension `KSClassDeclaration.companion`
* Add extension `KSClassDeclaration.resolveSubclasses`
* `Sealed`:
* Improvements
## 0.22.5 ## 0.22.5
* `Versions`: * `Versions`:

View File

@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.22.5 version=0.22.6
android_code_version=271 android_code_version=272

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.PropertySpec
import com.squareup.kotlinpoet.asTypeName import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.ksp.toClassName 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.findSubClasses
import dev.inmo.micro_ksp.generator.writeFile import dev.inmo.micro_ksp.generator.writeFile
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
@ -93,7 +95,10 @@ class Processor(
) )
addFunction( addFunction(
FunSpec.builder("values").apply { 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) returns(setType)
addCode( addCode(
CodeBlock.of( CodeBlock.of(
@ -107,7 +112,9 @@ class Processor(
@OptIn(KspExperimental::class) @OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> { override fun process(resolver: Resolver): List<KSAnnotated> {
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach { (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") { it.writeFile(prefix = prefix, suffix = "SealedWorkaround") {
FileSpec.builder( FileSpec.builder(
it.packageName.asString(), it.packageName.asString(),