Skip to content

Commit

Permalink
Don't pass collectionCN and builderCN explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Aug 15, 2023
1 parent 8360c96 commit 45056fb
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 104 deletions.
6 changes: 3 additions & 3 deletions ksp-processors/src/main/kotlin/KotlinPoetDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ internal inline fun FileSpec.Builder.addFunction(name: String, builder: FunSpecB
internal inline fun TypeSpec.Builder.addClass(name: String, builder: TypeSpecBuilder) =
addType(TypeSpec.classBuilder(name).apply(builder).build())

internal inline fun TypeSpec.Builder.addClass(className: ClassName, builder: TypeSpecBuilder) =
addType(TypeSpec.classBuilder(className).apply(builder).build())

internal inline fun TypeSpec.Builder.addCompanionObject(name: String? = null, builder: TypeSpecBuilder) =
addType(TypeSpec.companionObjectBuilder(name).apply(builder).build())

Expand All @@ -81,9 +84,6 @@ internal inline fun TypeSpec.Builder.addProperty(
internal inline fun TypeSpec.Builder.primaryConstructor(builder: FunSpecBuilder) =
primaryConstructor(FunSpec.constructorBuilder().apply(builder).build())

internal inline fun TypeSpec.Builder.addConstructor(builder: FunSpecBuilder) =
addFunction(FunSpec.constructorBuilder().apply(builder).build())

internal inline fun TypeSpec.Builder.addInitializerBlock(builder: CodeBlockBuilder) =
addInitializerBlock(CodeBlock.builder().apply(builder).build())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT
import dev.kord.ksp.generation.shared.*
import kotlinx.serialization.Serializable

context(GenerationContext)
internal val BitFlags.collectionCN
get() = ClassName(entityCN.packageName, entityName + 's')

context(GenerationContext)
internal val BitFlags.builderCN
get() = collectionCN.nestedClass("Builder")

internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGenerationEntity(originatingFile) {
val collectionName = ClassName(packageName, entityName + 's')
val builderName = collectionName.nestedClass("Builder")
addClass(collectionName) {
addCollectionDoc(collectionName, builderName)
addClass(collectionCN) {
addCollectionDoc()
addAnnotation<Serializable> {
addMember("with·=·%T.Serializer::class", collectionName)
addMember("with·=·%T.Serializer::class", collectionCN)
}
primaryConstructor {
addModifiers(INTERNAL)
Expand Down Expand Up @@ -65,31 +71,31 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
}
}
addContains(parameterName = "flag", parameterType = entityCN)
addContains(parameterName = "flags", parameterType = collectionName)
addPlus(parameterName = "flag", parameterType = entityCN, collectionName)
addPlus(parameterName = "flags", parameterType = collectionName, collectionName)
addMinus(parameterName = "flag", parameterType = entityCN, collectionName)
addMinus(parameterName = "flags", parameterType = collectionName, collectionName)
addContains(parameterName = "flags", parameterType = collectionCN)
addPlus(parameterName = "flag", parameterType = entityCN)
addPlus(parameterName = "flags", parameterType = collectionCN)
addMinus(parameterName = "flag", parameterType = entityCN)
addMinus(parameterName = "flags", parameterType = collectionCN)
addFunction("copy") {
addModifiers(PUBLIC, INLINE)
addParameter("block", type = LambdaTypeName.get(receiver = builderName, returnType = UNIT))
returns(collectionName)
addParameter("block", type = LambdaTypeName.get(receiver = builderCN, returnType = UNIT))
returns(collectionCN)
addStatement("%M { callsInPlace(block, %M) }", CONTRACT, EXACTLY_ONCE)
addStatement("return %T($valueName).apply(block).build()", builderName)
addStatement("return %T($valueName).apply(block).build()", builderCN)
}
addEqualsAndHashCodeBasedOnClassAndSingleProperty(collectionName, property = valueName)
addEqualsAndHashCodeBasedOnClassAndSingleProperty(collectionCN, property = valueName)
addFunction("toString") {
addModifiers(OVERRIDE)
returns<String>()
addStatement("return \"${collectionName.simpleName}(values=\$values)\"")
addStatement("return \"${collectionCN.simpleName}(values=\$values)\"")
}
if (collectionWasDataClass) {
addDeprecatedDataClassArtifacts(collectionName)
addDeprecatedDataClassArtifacts()
}
addBuilder(collectionName)
addSerializer(collectionName)
addBuilder()
addSerializer()
}
addFactoryFunctions(collectionName, builderName)
addFactoryFunctions()
addClass(entityCN) {
// for ksp incremental processing
addOriginatingKSFile(originatingFile)
Expand Down Expand Up @@ -119,8 +125,8 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
}
}

addPlus(parameterName = "flag", parameterType = entityCN, collectionName)
addPlus(parameterName = "flags", parameterType = collectionName, collectionName)
addPlus(parameterName = "flag", parameterType = entityCN)
addPlus(parameterName = "flags", parameterType = collectionCN)
addEqualsAndHashCodeBasedOnClassAndSingleProperty(entityCN, property = "shift", FINAL)
addFunction("toString") {
addModifiers(FINAL, OVERRIDE)
Expand Down Expand Up @@ -167,50 +173,50 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
}
}

context(GenerationContext)
private fun FileSpec.Builder.addFactoryFunctions(collectionName: ClassName, builderName: ClassName) {
val factoryFunctionName = collectionName.simpleName
context(BitFlags, GenerationContext)
private fun FileSpec.Builder.addFactoryFunctions() {
val factoryFunctionName = collectionCN.simpleName

addFunction(factoryFunctionName) {
addModifiers(PUBLIC, INLINE)
addParameter("builder", type = LambdaTypeName.get(receiver = builderName, returnType = UNIT)) {
addParameter("builder", type = LambdaTypeName.get(receiver = builderCN, returnType = UNIT)) {
defaultValue("{}")
}
returns(collectionName)
returns(collectionCN)

addStatement("%M { callsInPlace(builder, %M) }", CONTRACT, EXACTLY_ONCE)
addStatement("return %T().apply(builder).build()", builderName)
addStatement("return %T().apply(builder).build()", builderCN)
}

addFunction(factoryFunctionName) {
addModifiers(PUBLIC)
addParameter("flags", entityCN, VARARG)
returns(collectionName)
returns(collectionCN)

addStatement("return $factoryFunctionName·{ flags.forEach·{ +it } }")
}

addFunction(factoryFunctionName) {
addModifiers(PUBLIC)
addParameter("flags", collectionName, VARARG)
returns(collectionName)
addParameter("flags", collectionCN, VARARG)
returns(collectionCN)

addStatement("return $factoryFunctionName·{ flags.forEach·{ +it } }")
}

addFunction(factoryFunctionName) {
addModifiers(PUBLIC)
addParameter("flags", ITERABLE.parameterizedBy(entityCN))
returns(collectionName)
returns(collectionCN)

addStatement("return $factoryFunctionName·{ flags.forEach·{ +it } }")
}

addFunction(factoryFunctionName) {
jvmName("${factoryFunctionName}0")
addModifiers(PUBLIC)
addParameter("flags", ITERABLE.parameterizedBy(collectionName))
returns(collectionName)
addParameter("flags", ITERABLE.parameterizedBy(collectionCN))
returns(collectionCN)

addStatement("return $factoryFunctionName·{ flags.forEach·{ +it } }")
}
Expand Down
17 changes: 8 additions & 9 deletions ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.ksp.generation.bitflags

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.KModifier.*
import com.squareup.kotlinpoet.TypeSpec
Expand All @@ -12,8 +11,8 @@ import dev.kord.ksp.generation.shared.EMPTY_BIT_SET
import dev.kord.ksp.generation.shared.GenerationContext

context(BitFlags, GenerationContext)
internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
addClass("Builder") {
internal fun TypeSpec.Builder.addBuilder() {
addClass(builderCN) {
primaryConstructor {
addParameter(valueName, valueCN) {
when (valueType) {
Expand All @@ -27,7 +26,7 @@ internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
initializer(valueName)
}

val builder = "Builder"
val builder = builderCN.simpleName
addFunction("unaryPlus") {
addModifiers(PUBLIC, OPERATOR)
receiver(entityCN)
Expand All @@ -41,7 +40,7 @@ internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
}
addFunction("unaryPlus") {
addModifiers(PUBLIC, OPERATOR)
receiver(collectionName)
receiver(collectionCN)

addStatement(
when (valueType) {
Expand All @@ -64,7 +63,7 @@ internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
}
addFunction("unaryMinus") {
addModifiers(PUBLIC, OPERATOR)
receiver(collectionName)
receiver(collectionCN)

addStatement(
when (valueType) {
Expand All @@ -75,8 +74,8 @@ internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
}

addFunction("build") {
returns(collectionName)
addStatement("return %T($valueName)", collectionName)
returns(collectionCN)
addStatement("return %T($valueName)", collectionCN)
}

addFunction("flags") {
Expand All @@ -89,7 +88,7 @@ internal fun TypeSpec.Builder.addBuilder(collectionName: ClassName) {
DeprecationLevel.WARNING,
)
)
returns(collectionName)
returns(collectionCN)
addStatement("return build()")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.ksp.generation.bitflags

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier.OPERATOR
import com.squareup.kotlinpoet.KModifier.PUBLIC
import com.squareup.kotlinpoet.TypeName
Expand All @@ -9,48 +8,46 @@ import dev.kord.ksp.addFunction
import dev.kord.ksp.generation.GenerationEntity.BitFlags
import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET
import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT
import dev.kord.ksp.generation.shared.GenerationContext
import dev.kord.ksp.returns

context(BitFlags)
internal fun TypeSpec.Builder.addContains(parameterName: String, parameterType: TypeName) =
addFunction("contains") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns<Boolean>()
addStatement(
when (valueType) {
INT -> "return this.$valueName·and·$parameterName.$valueName·==·$parameterName.$valueName"
BIT_SET -> "return $parameterName.$valueName·in·this.$valueName"
}
)
}
internal fun TypeSpec.Builder.addContains(parameterName: String, parameterType: TypeName) = addFunction("contains") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns<Boolean>()
addStatement(
when (valueType) {
INT -> "return this.$valueName·and·$parameterName.$valueName·==·$parameterName.$valueName"
BIT_SET -> "return $parameterName.$valueName·in·this.$valueName"
}
)
}

context(BitFlags)
internal fun TypeSpec.Builder.addPlus(parameterName: String, parameterType: TypeName, collectionName: ClassName) =
addFunction("plus") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns(collectionName)
addStatement(
when (valueType) {
INT -> "return %T(this.$valueName·or·$parameterName.$valueName)"
BIT_SET -> "return %T(this.$valueName·+·$parameterName.$valueName)"
},
collectionName,
)
}
context(BitFlags, GenerationContext)
internal fun TypeSpec.Builder.addPlus(parameterName: String, parameterType: TypeName) = addFunction("plus") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns(collectionCN)
addStatement(
when (valueType) {
INT -> "return %T(this.$valueName·or·$parameterName.$valueName)"
BIT_SET -> "return %T(this.$valueName·+·$parameterName.$valueName)"
},
collectionCN,
)
}

context(BitFlags)
internal fun TypeSpec.Builder.addMinus(parameterName: String, parameterType: TypeName, collectionName: ClassName) =
addFunction("minus") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns(collectionName)
addStatement(
when (valueType) {
INT -> "return %T(this.$valueName·and·$parameterName.$valueName.inv())"
BIT_SET -> "return %T(this.$valueName·-·$parameterName.$valueName)"
},
collectionName,
)
}
context(BitFlags, GenerationContext)
internal fun TypeSpec.Builder.addMinus(parameterName: String, parameterType: TypeName) = addFunction("minus") {
addModifiers(PUBLIC, OPERATOR)
addParameter(parameterName, parameterType)
returns(collectionCN)
addStatement(
when (valueType) {
INT -> "return %T(this.$valueName·and·$parameterName.$valueName.inv())"
BIT_SET -> "return %T(this.$valueName·-·$parameterName.$valueName)"
},
collectionCN,
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.ksp.generation.bitflags

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.KModifier.OPERATOR
import com.squareup.kotlinpoet.KModifier.PUBLIC
Expand All @@ -16,12 +15,12 @@ private val LEVEL = DeprecationLevel.WARNING

context(BitFlags, GenerationContext)
@OptIn(DelicateKotlinPoetApi::class)
internal fun TypeSpec.Builder.addDeprecatedDataClassArtifacts(collectionName: ClassName) {
internal fun TypeSpec.Builder.addDeprecatedDataClassArtifacts() {
addFunction("component1") {
addKdoc("@suppress")
addAnnotation(
Deprecated(
"${collectionName.simpleName} is no longer a data class.",
"${collectionCN.simpleName} is no longer a data class.",
ReplaceWith("this.$valueName", imports = emptyArray()),
LEVEL,
)
Expand All @@ -35,14 +34,14 @@ internal fun TypeSpec.Builder.addDeprecatedDataClassArtifacts(collectionName: Cl
addAnnotation(Suppress("DeprecatedCallableAddReplaceWith"))
addAnnotation(
Deprecated(
"${collectionName.simpleName} is no longer a data class. Deprecated without a replacement.",
"${collectionCN.simpleName} is no longer a data class. Deprecated without a replacement.",
level = LEVEL,
)
)
addParameter(valueName, valueCN) {
defaultValue("this.$valueName")
}
returns(collectionName)
addStatement("return %T($valueName)", collectionName)
returns(collectionCN)
addStatement("return %T($valueName)", collectionCN)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.ksp.generation.bitflags

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.TypeSpec
import dev.kord.ksp.generation.GenerationEntity.BitFlags
Expand Down Expand Up @@ -79,7 +78,7 @@ private val docString = """
""".trimIndent()

context(BitFlags, GenerationContext)
internal fun TypeSpec.Builder.addCollectionDoc(collectionName: ClassName, builderName: ClassName) {
internal fun TypeSpec.Builder.addCollectionDoc() {
val possibleValues = entries.map { entityCN.nestedClass(it.name) }
val unknown = entityCN.nestedClass("Unknown")
val withReplacedVariables = docString
Expand All @@ -90,14 +89,14 @@ internal fun TypeSpec.Builder.addCollectionDoc(collectionName: ClassName, builde
addKdoc(
CodeBlock.of(
withReplacedVariables,
collectionName.simpleName, // %1L
collectionCN.simpleName, // %1L
entityCN, // %2T
collectionName, // %3T
collectionCN, // %3T
possibleValues.getSafe(0), // %4T
possibleValues.getSafe(1), // %5T
possibleValues.getSafe(1), // %6T
unknown, // %7T
builderName, // %8T
builderCN, // %8T
)
)
}
Expand Down
Loading

0 comments on commit 45056fb

Please sign in to comment.