From 8ce7d37b72ee0b1ffa370ecdd996110fa5432cb4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 17 Oct 2024 20:52:45 +0600 Subject: [PATCH 1/8] start 0.22.6 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c22980ce0b..606f65786c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.22.6 + ## 0.22.5 * `Versions`: diff --git a/gradle.properties b/gradle.properties index aa47a95740e..e78b60bf1e5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 From 496117d517e4fa725d5a6fc394977fa084c0dbd7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 17 Oct 2024 21:04:41 +0600 Subject: [PATCH 2/8] improvements in ksp parts --- CHANGELOG.md | 8 ++++++++ .../kotlin/KSClassDeclarationBuildSubFileName.kt | 13 +++++++++++++ .../src/main/kotlin/KSClassDeclarationCompanion.kt | 8 ++++++++ ksp/generator/src/main/kotlin/ResolveSubclasses.kt | 11 +++++++++++ ksp/sealed/generator/src/main/kotlin/Processor.kt | 11 +++++++++-- 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt create mode 100644 ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt create mode 100644 ksp/generator/src/main/kotlin/ResolveSubclasses.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 606f65786c2..6af660b0f2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`: diff --git a/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt b/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt new file mode 100644 index 00000000000..5f854a166f2 --- /dev/null +++ b/ksp/generator/src/main/kotlin/KSClassDeclarationBuildSubFileName.kt @@ -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 + } diff --git a/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt b/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt new file mode 100644 index 00000000000..2f7f859736d --- /dev/null +++ b/ksp/generator/src/main/kotlin/KSClassDeclarationCompanion.kt @@ -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 } + } diff --git a/ksp/generator/src/main/kotlin/ResolveSubclasses.kt b/ksp/generator/src/main/kotlin/ResolveSubclasses.kt new file mode 100644 index 00000000000..ca6acc8c9b5 --- /dev/null +++ b/ksp/generator/src/main/kotlin/ResolveSubclasses.kt @@ -0,0 +1,11 @@ +package dev.inmo.micro_ksp.generator + +import com.google.devtools.ksp.symbol.KSClassDeclaration + +fun KSClassDeclaration.resolveSubclasses(): List { + return (getSealedSubclasses().flatMap { + it.resolveSubclasses() + }.ifEmpty { + sequenceOf(this) + }).toList() +} diff --git a/ksp/sealed/generator/src/main/kotlin/Processor.kt b/ksp/sealed/generator/src/main/kotlin/Processor.kt index 6eca44452a7..99196279576 100644 --- a/ksp/sealed/generator/src/main/kotlin/Processor.kt +++ b/ksp/sealed/generator/src/main/kotlin/Processor.kt @@ -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 { (resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance().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(), From b9a26530663fd5b34268aa47f46977aa65278508 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 19 Oct 2024 13:41:25 +0600 Subject: [PATCH 3/8] start 0.22.7 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6af660b0f2e..4dbb9e081da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.22.7 + ## 0.22.6 * `KSP`: diff --git a/gradle.properties b/gradle.properties index e78b60bf1e5..aaa620af696 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,5 +15,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.22.6 -android_code_version=272 +version=0.22.7 +android_code_version=273 From 4625dfb85713d47f0a51f13f186df2dfe5ff0e30 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 19 Oct 2024 13:44:37 +0600 Subject: [PATCH 4/8] replace GenerateSealedWorkaround --- CHANGELOG.md | 4 ++++ ksp/sealed/generator/src/main/kotlin/Processor.kt | 2 +- ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt | 2 +- .../src/commonMain/kotlin/GenerateSealedWorkaround.kt | 2 +- .../src/commonMain/kotlin/OldGenerateSealedWorkaround.kt | 6 ++++++ 5 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 ksp/sealed/src/commonMain/kotlin/OldGenerateSealedWorkaround.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dbb9e081da..09ecd3d7af9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.22.7 +* `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`: diff --git a/ksp/sealed/generator/src/main/kotlin/Processor.kt b/ksp/sealed/generator/src/main/kotlin/Processor.kt index 99196279576..bf716e02840 100644 --- a/ksp/sealed/generator/src/main/kotlin/Processor.kt +++ b/ksp/sealed/generator/src/main/kotlin/Processor.kt @@ -19,7 +19,7 @@ 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 +import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround import java.io.File class Processor( diff --git a/ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt b/ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt index b88dc45e563..01c9e8e90b4 100644 --- a/ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt +++ b/ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt @@ -1,6 +1,6 @@ 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 sealed interface Test { diff --git a/ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt b/ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt index c98eed90d8e..2146dff9346 100644 --- a/ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt +++ b/ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt @@ -1,4 +1,4 @@ -package dev.inmo.microutils.kps.sealed +package dev.inmo.micro_utils.ksp.sealed @Retention(AnnotationRetention.BINARY) @Target(AnnotationTarget.CLASS) diff --git a/ksp/sealed/src/commonMain/kotlin/OldGenerateSealedWorkaround.kt b/ksp/sealed/src/commonMain/kotlin/OldGenerateSealedWorkaround.kt new file mode 100644 index 00000000000..2a317c5a476 --- /dev/null +++ b/ksp/sealed/src/commonMain/kotlin/OldGenerateSealedWorkaround.kt @@ -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 \ No newline at end of file From ee2521cb017e6377ce2a4bedec280ef7e495fac7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 19 Oct 2024 15:21:00 +0600 Subject: [PATCH 5/8] update dependencies --- CHANGELOG.md | 3 +++ gradle/libs.versions.toml | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ecd3d7af9..9581a6a2102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 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` diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9fafdeb524d..d4e846e8f23 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,12 +1,12 @@ [versions] -kt = "2.0.20" +kt = "2.0.21" kt-serialization = "1.7.3" kt-coroutines = "1.9.0" kslog = "1.3.6" -jb-compose = "1.7.0-rc01" +jb-compose = "1.7.0" jb-exposed = "0.55.0" jb-dokka = "1.9.20" @@ -23,7 +23,7 @@ koin = "4.0.0" okio = "3.9.1" -ksp = "2.0.20-1.0.25" +ksp = "2.0.21-1.0.25" kotlin-poet = "1.18.1" versions = "0.51.0" From c5dbd103355601c5eaf80fbc312f70d3edda2c5b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 19 Oct 2024 15:39:00 +0600 Subject: [PATCH 6/8] hotfix due to deprecated annotation --- ...eclarationGetGenerateSealedWorkaroundAnnotation.kt | 11 +++++++++++ ksp/sealed/generator/src/main/kotlin/Processor.kt | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 ksp/sealed/generator/src/main/kotlin/KSClassDeclarationGetGenerateSealedWorkaroundAnnotation.kt diff --git a/ksp/sealed/generator/src/main/kotlin/KSClassDeclarationGetGenerateSealedWorkaroundAnnotation.kt b/ksp/sealed/generator/src/main/kotlin/KSClassDeclarationGetGenerateSealedWorkaroundAnnotation.kt new file mode 100644 index 00000000000..aab653b1349 --- /dev/null +++ b/ksp/sealed/generator/src/main/kotlin/KSClassDeclarationGetGenerateSealedWorkaroundAnnotation.kt @@ -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()) diff --git a/ksp/sealed/generator/src/main/kotlin/Processor.kt b/ksp/sealed/generator/src/main/kotlin/Processor.kt index bf716e02840..d2c7e1f522e 100644 --- a/ksp/sealed/generator/src/main/kotlin/Processor.kt +++ b/ksp/sealed/generator/src/main/kotlin/Processor.kt @@ -53,10 +53,10 @@ class Processor( ksClassDeclaration: KSClassDeclaration, resolver: Resolver ) { - val annotation = ksClassDeclaration.getAnnotationsByType(GenerateSealedWorkaround::class).first() + val annotation = ksClassDeclaration.getGenerateSealedWorkaroundAnnotation val subClasses = ksClassDeclaration.resolveSubclasses( searchIn = resolver.getAllFiles(), - allowNonSealed = annotation.includeNonSealedSubTypes + allowNonSealed = annotation ?.includeNonSealedSubTypes ?: false ).distinct() val subClassesNames = subClasses.filter { when (it.classKind) { @@ -112,7 +112,7 @@ class Processor( @OptIn(KspExperimental::class) override fun process(resolver: Resolver): List { (resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance().forEach { - val prefix = it.getAnnotationsByType(GenerateSealedWorkaround::class).first().prefix.takeIf { + val prefix = (it.getGenerateSealedWorkaroundAnnotation) ?.prefix ?.takeIf { it.isNotEmpty() } ?: it.buildSubFileName.replaceFirst(it.simpleName.asString(), "") it.writeFile(prefix = prefix, suffix = "SealedWorkaround") { From 54cc353bcc7a4108280e2f892cf1ab08fd0e1d3d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 24 Oct 2024 15:26:02 +0600 Subject: [PATCH 7/8] start 0.22.8 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9581a6a2102..032047c5da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.22.8 + ## 0.22.7 * `Versions`: diff --git a/gradle.properties b/gradle.properties index aaa620af696..b3d107e2b47 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,5 +15,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.22.7 -android_code_version=273 +version=0.22.8 +android_code_version=274 From 6245b36bdbec7dcd89c2c117a724890c11ebee65 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 24 Oct 2024 15:30:10 +0600 Subject: [PATCH 8/8] add pads and breakAsPairs extensions --- CHANGELOG.md | 4 +++ .../inmo/micro_utils/common/BreakAsPairs.kt | 13 ++++++++ .../inmo/micro_utils/common/PadExtensions.kt | 32 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 032047c5da6..045d5ef16d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 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`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt new file mode 100644 index 00000000000..d1e262e6188 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt @@ -0,0 +1,13 @@ +package dev.inmo.micro_utils.common + +fun List.breakAsPairs(): List> { + val result = mutableListOf>() + + for (i in 0 until size - 1) { + val first = get(i) + val second = get(i + 1) + result.add(first to second) + } + + return result +} diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt new file mode 100644 index 00000000000..2bc5621d2ae --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt @@ -0,0 +1,32 @@ +package dev.inmo.micro_utils.common + +inline fun Sequence.padWith(size: Int, inserter: (Sequence) -> Sequence): Sequence { + var result = this + while (result.count() < size) { + result = inserter(result) + } + return result +} + +inline fun Sequence.padEnd(size: Int, padBlock: (Int) -> T): Sequence = padWith(size) { it + padBlock(it.count()) } + +inline fun Sequence.padEnd(size: Int, o: T) = padEnd(size) { o } + +inline fun List.padWith(size: Int, inserter: (List) -> List): List { + var result = this + while (result.size < size) { + result = inserter(result) + } + return result +} +inline fun List.padEnd(size: Int, padBlock: (Int) -> T): List = asSequence().padEnd(size, padBlock).toList() + +inline fun List.padEnd(size: Int, o: T): List = asSequence().padEnd(size, o).toList() + +inline fun Sequence.padStart(size: Int, padBlock: (Int) -> T): Sequence = padWith(size) { sequenceOf(padBlock(it.count())) + it } + +inline fun Sequence.padStart(size: Int, o: T) = padStart(size) { o } + +inline fun List.padStart(size: Int, padBlock: (Int) -> T): List = asSequence().padStart(size, padBlock).toList() + +inline fun List.padStart(size: Int, o: T): List = asSequence().padStart(size, o).toList()