mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 08:13:49 +00:00
Merge branch 'master' into 0.23.0
This commit is contained in:
commit
7ef4c5d282
25
CHANGELOG.md
25
CHANGELOG.md
@ -2,6 +2,31 @@
|
|||||||
|
|
||||||
## 0.23.0
|
## 0.23.0
|
||||||
|
|
||||||
|
## 0.22.8
|
||||||
|
|
||||||
|
* `Common`:
|
||||||
|
* Add `List.breakAsPairs` extension
|
||||||
|
* Add `Sequence.padWith`/`Sequence.padStart`/`Sequence.padEnd` and `List.padWith`/`List.padStart`/`List.padEnd` extensions
|
||||||
|
|
||||||
|
## 0.22.7
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Kotlin`: `2.0.20` -> `2.0.21`
|
||||||
|
* `Compose`: `1.7.0-rc01` -> `1.7.0`
|
||||||
|
* `KSP`:
|
||||||
|
* `Sealed`:
|
||||||
|
* Change package of `GenerateSealedWorkaround`. Migration: replace `dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround` -> `dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround`
|
||||||
|
|
||||||
|
## 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_utils.common
|
||||||
|
|
||||||
|
fun <T> List<T>.breakAsPairs(): List<Pair<T, T>> {
|
||||||
|
val result = mutableListOf<Pair<T, T>>()
|
||||||
|
|
||||||
|
for (i in 0 until size - 1) {
|
||||||
|
val first = get(i)
|
||||||
|
val second = get(i + 1)
|
||||||
|
result.add(first to second)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
inline fun <T> Sequence<T>.padWith(size: Int, inserter: (Sequence<T>) -> Sequence<T>): Sequence<T> {
|
||||||
|
var result = this
|
||||||
|
while (result.count() < size) {
|
||||||
|
result = inserter(result)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> Sequence<T>.padEnd(size: Int, padBlock: (Int) -> T): Sequence<T> = padWith(size) { it + padBlock(it.count()) }
|
||||||
|
|
||||||
|
inline fun <T> Sequence<T>.padEnd(size: Int, o: T) = padEnd(size) { o }
|
||||||
|
|
||||||
|
inline fun <T> List<T>.padWith(size: Int, inserter: (List<T>) -> List<T>): List<T> {
|
||||||
|
var result = this
|
||||||
|
while (result.size < size) {
|
||||||
|
result = inserter(result)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
inline fun <T> List<T>.padEnd(size: Int, padBlock: (Int) -> T): List<T> = asSequence().padEnd(size, padBlock).toList()
|
||||||
|
|
||||||
|
inline fun <T> List<T>.padEnd(size: Int, o: T): List<T> = asSequence().padEnd(size, o).toList()
|
||||||
|
|
||||||
|
inline fun <T> Sequence<T>.padStart(size: Int, padBlock: (Int) -> T): Sequence<T> = padWith(size) { sequenceOf(padBlock(it.count())) + it }
|
||||||
|
|
||||||
|
inline fun <T> Sequence<T>.padStart(size: Int, o: T) = padStart(size) { o }
|
||||||
|
|
||||||
|
inline fun <T> List<T>.padStart(size: Int, padBlock: (Int) -> T): List<T> = asSequence().padStart(size, padBlock).toList()
|
||||||
|
|
||||||
|
inline fun <T> List<T>.padStart(size: Int, o: T): List<T> = asSequence().padStart(size, o).toList()
|
@ -16,4 +16,4 @@ crypto_js_version=4.1.1
|
|||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.23.0
|
version=0.23.0
|
||||||
android_code_version=273
|
android_code_version=275
|
||||||
|
@ -6,7 +6,7 @@ kt-coroutines = "1.9.0"
|
|||||||
|
|
||||||
kslog = "1.3.6"
|
kslog = "1.3.6"
|
||||||
|
|
||||||
jb-compose = "1.7.0-rc01"
|
jb-compose = "1.7.0"
|
||||||
jb-exposed = "0.55.0"
|
jb-exposed = "0.55.0"
|
||||||
jb-dokka = "1.9.20"
|
jb-dokka = "1.9.20"
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package dev.inmo.micro_utils.ksp.sealed.generator
|
||||||
|
|
||||||
|
import com.google.devtools.ksp.KspExperimental
|
||||||
|
import com.google.devtools.ksp.getAnnotationsByType
|
||||||
|
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
||||||
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
|
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround as OldGenerateSealedWorkaround
|
||||||
|
|
||||||
|
@OptIn(KspExperimental::class)
|
||||||
|
val KSClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
||||||
|
get() = (getAnnotationsByType(GenerateSealedWorkaround::class).firstOrNull() ?: getAnnotationsByType(OldGenerateSealedWorkaround::class).firstOrNull())
|
@ -15,9 +15,11 @@ 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.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class Processor(
|
class Processor(
|
||||||
@ -51,10 +53,10 @@ class Processor(
|
|||||||
ksClassDeclaration: KSClassDeclaration,
|
ksClassDeclaration: KSClassDeclaration,
|
||||||
resolver: Resolver
|
resolver: Resolver
|
||||||
) {
|
) {
|
||||||
val annotation = ksClassDeclaration.getAnnotationsByType(GenerateSealedWorkaround::class).first()
|
val annotation = ksClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
||||||
val subClasses = ksClassDeclaration.resolveSubclasses(
|
val subClasses = ksClassDeclaration.resolveSubclasses(
|
||||||
searchIn = resolver.getAllFiles(),
|
searchIn = resolver.getAllFiles(),
|
||||||
allowNonSealed = annotation.includeNonSealedSubTypes
|
allowNonSealed = annotation ?.includeNonSealedSubTypes ?: false
|
||||||
).distinct()
|
).distinct()
|
||||||
val subClassesNames = subClasses.filter {
|
val subClassesNames = subClasses.filter {
|
||||||
when (it.classKind) {
|
when (it.classKind) {
|
||||||
@ -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.getGenerateSealedWorkaroundAnnotation) ?.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(),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
||||||
|
|
||||||
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
@GenerateSealedWorkaround
|
@GenerateSealedWorkaround
|
||||||
sealed interface Test {
|
sealed interface Test {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package dev.inmo.microutils.kps.sealed
|
package dev.inmo.micro_utils.ksp.sealed
|
||||||
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
@Retention(AnnotationRetention.BINARY)
|
||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
package dev.inmo.microutils.kps.sealed
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("GenerateSealedWorkaround", "dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround"))
|
||||||
|
typealias GenerateSealedWorkaround = GenerateSealedWorkaround
|
Loading…
Reference in New Issue
Block a user