mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-24 17:47:15 +00:00
commit
fcaa327660
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,5 +1,15 @@
|
||||
# Changelog
|
||||
|
||||
## 0.22.6
|
||||
|
||||
* `KSP`:
|
||||
* `Generator`:
|
||||
* Add extension `KSClassDeclaration.buildSubFileName`
|
||||
* Add extension `KSClassDeclaration.companion`
|
||||
* Add extension `KSClassDeclaration.resolveSubclasses`
|
||||
* `Sealed`:
|
||||
* Improvements
|
||||
|
||||
## 0.22.5
|
||||
|
||||
* `Versions`:
|
||||
|
@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.22.5
|
||||
android_code_version=271
|
||||
version=0.22.6
|
||||
android_code_version=272
|
||||
|
@ -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
|
||||
}
|
@ -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 }
|
||||
}
|
11
ksp/generator/src/main/kotlin/ResolveSubclasses.kt
Normal file
11
ksp/generator/src/main/kotlin/ResolveSubclasses.kt
Normal 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()
|
||||
}
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user