mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-25 19:48:45 +00:00
improvements in ksp parts
This commit is contained in:
parent
8ce7d37b72
commit
496117d517
@ -2,6 +2,14 @@
|
|||||||
|
|
||||||
## 0.22.6
|
## 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`:
|
||||||
|
@ -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.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(),
|
||||||
|
Loading…
Reference in New Issue
Block a user