Skip to content

Commit

Permalink
Use codegen.kt instead of KSPUtils.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann authored and DRSchlaubi committed Oct 22, 2024
1 parent 663410e commit cca4a66
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 53 deletions.
43 changes: 0 additions & 43 deletions ksp-processors/src/main/kotlin/KSPUtils.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
package dev.kord.ksp

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.findActualType
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.isDefault
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*
import kotlin.reflect.KProperty1

internal inline fun <reified A : Annotation> Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) =
getSymbolsWithAnnotation(A::class.qualifiedName!!, inDepth)

internal fun Resolver.getNewClasses() = getNewFiles().flatMap { it.declarations.filterIsInstance<KSClassDeclaration>() }

internal inline fun <reified A : Annotation> KSAnnotation.isOfType() = isOfType(A::class.qualifiedName!!)

internal fun KSAnnotation.isOfType(qualifiedName: String) = annotationType.resolve()
.declaration.let { if (it is KSTypeAlias) it.findActualType() else it }
.qualifiedName?.asString() == qualifiedName

@OptIn(KspExperimental::class)
internal inline fun <reified A : Annotation> KSAnnotated.getAnnotationsByType() = getAnnotationsByType(A::class)

internal class AnnotationArguments<A : Annotation> private constructor(
private val arguments: Map<String, KSValueArgument>,
) {
private fun getArgument(parameter: KProperty1<A, Any>) = arguments.getValue(parameter.name)
private val KProperty1<A, Any>.value get() = getArgument(this).value

fun isDefault(parameter: KProperty1<A, Any>) = getArgument(parameter).isDefault()

// can't return non-nullable values because of https://github.com/google/ksp/issues/885
operator fun get(parameter: KProperty1<A, Annotation>) = parameter.value as KSAnnotation?
operator fun get(parameter: KProperty1<A, Array<out Annotation>>) =
@Suppress("UNCHECKED_CAST") (parameter.value as List<KSAnnotation>?)

companion object {
fun <A : Annotation> KSAnnotation.arguments() =
AnnotationArguments<A>(arguments.associateBy { it.name!!.asString() })
}
}

@Suppress("RecursivePropertyAccessor")
internal val KSReferenceElement.isClassifierReference: Boolean
get() = when (this) {
is KSDynamicReference, is KSCallableReference -> false
is KSClassifierReference -> true
is KSDefNonNullReference -> enclosedType.isClassifierReference
is KSParenthesizedReference -> element.isClassifierReference
else -> error("Unexpected KSReferenceElement: $this")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.kord.ksp.generation

import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSAnnotation
import dev.kord.ksp.AnnotationArguments.Companion.arguments
import dev.kord.codegen.ksp.annotations.AnnotationArguments.Companion.arguments
import dev.kord.ksp.Generate
import dev.kord.ksp.Generate.EntityType
import dev.kord.ksp.Generate.EntityType.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSFile
import com.squareup.kotlinpoet.ksp.writeTo
import dev.kord.codegen.ksp.getSymbolsWithAnnotation
import dev.kord.codegen.ksp.isOfType
import dev.kord.ksp.Generate
import dev.kord.ksp.generation.bitflags.generateFileSpec
import dev.kord.ksp.generation.kordenum.generateFileSpec
import dev.kord.ksp.getAnnotationsByType
import dev.kord.ksp.getSymbolsWithAnnotation
import dev.kord.ksp.isOfType

/** [SymbolProcessorProvider] for [GenerationProcessor]. */
class GenerationProcessorProvider : SymbolProcessorProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.KModifier.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.ksp.addOriginatingKSFile
import dev.kord.ksp.*
import dev.kord.codegen.kotlinpoet.addAnnotation
import dev.kord.codegen.kotlinpoet.addClass
import dev.kord.codegen.kotlinpoet.addCompanionObject
import dev.kord.codegen.kotlinpoet.addFunction
import dev.kord.codegen.kotlinpoet.addInitializerBlock
import dev.kord.codegen.kotlinpoet.addParameter
import dev.kord.codegen.kotlinpoet.addProperty
import dev.kord.codegen.kotlinpoet.getter
import dev.kord.codegen.kotlinpoet.primaryConstructor
import dev.kord.codegen.kotlinpoet.returns
import dev.kord.codegen.kotlinpoet.withControlFlow
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.*
import kotlinx.serialization.Serializable
import com.squareup.kotlinpoet.INT as INT_CN

context(GenerationContext)
internal val BitFlags.collectionCN
Expand Down Expand Up @@ -104,6 +115,7 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
addStatement("shift++")
}
}

BIT_SET -> withControlFlow("for·(shift·in·0..<$valueName.size)") {
addStatement("if·($valueName[shift])·add(%T.fromShift(shift))", entityCN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

package dev.kord.ksp.generation.bitflags

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.KModifier.*
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.UNIT
import dev.kord.ksp.addFunction
import dev.kord.codegen.kotlinpoet.addAnnotation
import dev.kord.codegen.kotlinpoet.addFunction
import dev.kord.codegen.kotlinpoet.returns
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.CONTRACT
import dev.kord.ksp.generation.shared.EXACTLY_ONCE
import dev.kord.ksp.generation.shared.GenerationContext
import dev.kord.ksp.returns

context(BitFlags, GenerationContext)
internal fun TypeSpec.Builder.addContains(parameterName: String, parameterType: TypeName) = addFunction("contains") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.jvm.jvmField
import dev.kord.ksp.*
import dev.kord.codegen.kotlinpoet.addAnnotation
import dev.kord.codegen.kotlinpoet.addClass
import dev.kord.codegen.kotlinpoet.addCompanionObject
import dev.kord.codegen.kotlinpoet.addFunction
import dev.kord.codegen.kotlinpoet.addProperty
import dev.kord.codegen.kotlinpoet.primaryConstructor
import dev.kord.ksp.generation.GenerationEntity.BitFlags
import dev.kord.ksp.generation.shared.GenerationContext
import dev.kord.ksp.generation.shared.K_SERIALIZER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.KModifier.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.jvm.jvmName
import dev.kord.ksp.addFunction
import dev.kord.ksp.addParameter
import dev.kord.codegen.kotlinpoet.addAnnotation
import dev.kord.codegen.kotlinpoet.addFunction
import dev.kord.codegen.kotlinpoet.addParameter
import dev.kord.codegen.kotlinpoet.withControlFlow
import dev.kord.ksp.generation.GenerationEntity.BitFlags
import dev.kord.ksp.generation.shared.CONTRACT
import dev.kord.ksp.generation.shared.EXACTLY_ONCE
import dev.kord.ksp.generation.shared.GenerationContext
import dev.kord.ksp.withControlFlow

context(GenerationContext)
private val BitFlags.factoryFunctionName
Expand Down

0 comments on commit cca4a66

Please sign in to comment.