Skip to content

Commit

Permalink
No longer reify addParameter and addProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 13, 2023
1 parent a20703d commit dcd1b13
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 27 deletions.
2 changes: 2 additions & 0 deletions kotlinpoet/api/jvm/kotlinpoet.api
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,11 @@ public final class dev/kord/codegen/kotlinpoet/TypeSpecFactoriesKt {
}

public final class dev/kord/codegen/kotlinpoet/UtilsKt {
public static final fun addParameter (Lcom/squareup/kotlinpoet/FunSpec$Builder;Lcom/squareup/kotlinpoet/ParameterSpec;)Lcom/squareup/kotlinpoet/FunSpec$Builder;
public static final fun emptyCodeBlock ()Lcom/squareup/kotlinpoet/CodeBlock;
public static final fun indentWithSpaces (Lcom/squareup/kotlinpoet/FileSpec$Builder;I)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public static synthetic fun indentWithSpaces$default (Lcom/squareup/kotlinpoet/FileSpec$Builder;IILjava/lang/Object;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public static final fun minOf-PWzV0Is (JJ)J
public static final fun withNameAllocator (Lkotlin/jvm/functions/Function1;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@ package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String
import kotlin.collections.Iterable

public inline fun <reified R> FunSpec.Builder.`receiver`(kdoc: CodeBlock = emptyCodeBlock()):
FunSpec.Builder = `receiver`(typeNameOf<R>(), kdoc)

public inline fun <reified R> FunSpec.Builder.returns(kdoc: CodeBlock = emptyCodeBlock()):
FunSpec.Builder = returns(typeNameOf<R>(), kdoc)

public inline fun <reified T> FunSpec.Builder.addParameter(name: String, vararg
modifiers: KModifier): FunSpec.Builder = addParameter(name, typeNameOf<T>(),
modifiers = modifiers)

public inline fun <reified T> FunSpec.Builder.addParameter(name: String,
modifiers: Iterable<KModifier>): FunSpec.Builder = addParameter(name, typeNameOf<T>(),
modifiers)

public inline fun <reified A> FunSpec.Builder.addAnnotation(): FunSpec.Builder =
addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String
import kotlin.collections.Iterable

public inline fun <reified S> TypeSpec.Builder.superclass(): TypeSpec.Builder =
superclass(typeNameOf<S>())
Expand All @@ -17,14 +15,6 @@ public inline fun <reified S> TypeSpec.Builder.addSuperinterface(`delegate`: Cod
public inline fun <reified S> TypeSpec.Builder.addSuperinterface(constructorParameter: String):
TypeSpec.Builder = addSuperinterface(typeNameOf<S>(), constructorParameter)

public inline fun <reified T> TypeSpec.Builder.addProperty(name: String, vararg
modifiers: KModifier): TypeSpec.Builder = addProperty(name, typeNameOf<T>(),
modifiers = modifiers)

public inline fun <reified T> TypeSpec.Builder.addProperty(name: String,
modifiers: Iterable<KModifier>): TypeSpec.Builder = addProperty(name, typeNameOf<T>(),
modifiers)

public inline fun <reified A> TypeSpec.Builder.addAnnotation(): TypeSpec.Builder =
addAnnotation(A::class.asClassName())

Expand Down
7 changes: 7 additions & 0 deletions kotlinpoet/processor/src/main/kotlin/CodeProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.squareup.kotlinpoet.KModifier
import dev.kord.codegen.generator.builder_functions.BuilderVisitor
import dev.kord.codegen.generator.reification.ReifyingVisitor
import dev.kord.codegen.generator.reification.TypeVariableName
import dev.kord.codegen.generator.utils.accept
import dev.kord.codegen.generator.visitors.BeginEndVisitor
import dev.kord.codegen.kotlinpoet.FunSpec
import dev.kord.codegen.kotlinpoet.LambdaTypeName
import dev.kord.codegen.kotlinpoet.addCode
import dev.kord.codegen.kotlinpoet.addParameter
import java.util.*

val SymbolProcessorEnvironment.packageName: String
get() = options["package-name"]!!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.kord.codegen.generator.reification

import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSClassifierReference
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.squareup.kotlinpoet.*
import kotlin.reflect.KClass

val ignoreList = listOf("addParameter", "addProperty")

/**
* Checks whether a [KSFunctionDeclaration] is reifiable.
Expand All @@ -18,6 +20,7 @@ fun KSFunctionDeclaration.isReifiable(
includeClassName: Boolean = true,
includeTypeName: Boolean = true
): Boolean {
if (simpleName.asString() in ignoreList && parentDeclaration is KSClassDeclaration) return false
// We cannot add two receivers at this point
if (parentDeclaration is KSClassDeclaration && extensionReceiver != null) return false
return parameters.any {
Expand Down
25 changes: 22 additions & 3 deletions kotlinpoet/src/commonMain/kotlin/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.NameAllocator
import com.squareup.kotlinpoet.*
import kotlin.internal.HidesMembers

/**
* Scope for [withNameAllocator].
Expand Down Expand Up @@ -35,3 +36,21 @@ public fun FileSpec.Builder.indentWithSpaces(width: Int = 4): FileSpec.Builder =
* ```
*/
public fun withNameAllocator(block: NamingScope): Unit = NameAllocator().block()

public fun minOf(a: ULong, b: ULong): ULong {
return if (a <= b) a else b
}

@HidesMembers
public fun FunSpec.Builder.addParameter(parameterSpec: ParameterSpec): FunSpec.Builder = apply {

}

//private fun generateListOf() = FunSpec("minOf") {
// addAnn(Suppress("unused"))
// val a by addParameter<Int>()
// val b by addParameter<Int>()
//
// returns<Boolean>()
// addCode("return·if·(%N·<=·%n)·a·else·b", a, b)
//}
17 changes: 17 additions & 0 deletions kotlinpoet/src/commonTest/kotlin/ExampleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,21 @@ private fun main() {
class ExampleTest {
@Test
fun test() = main()

@Test
fun test2() {
fun generateListOf() = FunSpec("minOf") {
addAnnotation(Suppress("unused"))
val a = addParameter<Int>("a")
val b = addParameter<Int>("b")

returns<Boolean>()
addCode("return·")

}

println(generateListOf())
}
}


8 changes: 8 additions & 0 deletions ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ apiValidation {
ignoredProjects.add("processor")
}

tasks {
afterEvaluate {
named("kspTestKotlin") {
dependsOn(jar)
}
}
}

subprojects {
afterEvaluate {
mavenPublishing {
Expand Down
54 changes: 54 additions & 0 deletions ksp/processor/api/processor.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public final class dev/kord/codegen/ksp/processor/NamesKt {
public static final field NULL_IF_DEFAULT Ljava/lang/String;
public static final field PROCESSOR_ANNOTATION Ljava/lang/String;
public static final fun getARGUMENTS ()Lcom/squareup/kotlinpoet/MemberName;
public static final fun getARGUMENTS_NOT_NULL ()Lcom/squareup/kotlinpoet/MemberName;
public static final fun getGET_ANNOTATIONS_BY_TYPE ()Lcom/squareup/kotlinpoet/MemberName;
public static final fun getGET_ANNOTATION_BY_TYPE ()Lcom/squareup/kotlinpoet/MemberName;
}

public final class dev/kord/codegen/ksp/processor/ProcessorAnnotationProcessor : com/google/devtools/ksp/processing/SymbolProcessor {
public synthetic fun <init> (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun process (Lcom/google/devtools/ksp/processing/Resolver;)Ljava/util/List;
}

public final class dev/kord/codegen/ksp/processor/ProcessorAnnotationProcessor$Provider : com/google/devtools/ksp/processing/SymbolProcessorProvider {
public fun <init> ()V
public fun create (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;)Lcom/google/devtools/ksp/processing/SymbolProcessor;
}

public final class dev/kord/codegen/ksp/processor/generator/AccessorFunctionsKt {
public static final fun accessorFunction (Ldev/kord/codegen/ksp/processor/generator/ProcessingContext;)V
}

public final class dev/kord/codegen/ksp/processor/generator/DataClassRepresentationKt {
public static final fun dataClassRepresentation (Ldev/kord/codegen/ksp/processor/generator/ProcessingContext;)V
public static final fun dataClassType (Lcom/google/devtools/ksp/symbol/KSPropertyDeclaration;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;)Lcom/squareup/kotlinpoet/TypeName;
public static final fun isMappedAnnotation (Lcom/google/devtools/ksp/symbol/KSType;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;)Z
}

public final class dev/kord/codegen/ksp/processor/generator/FactoryFunctionKt {
public static final fun factoryFunction (Lcom/squareup/kotlinpoet/TypeSpec$Builder;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;)V
}

public final class dev/kord/codegen/ksp/processor/generator/GeneratorKt {
public static final fun processAnnotation (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;)V
}

public final class dev/kord/codegen/ksp/processor/generator/ProcessingContext {
public fun <init> (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;Lcom/squareup/kotlinpoet/FileSpec$Builder;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;)V
public final fun component1 ()Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;
public final fun component2 ()Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun component3 ()Lcom/google/devtools/ksp/symbol/KSClassDeclaration;
public final fun component4 ()Ljava/lang/String;
public final fun copy (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;Lcom/squareup/kotlinpoet/FileSpec$Builder;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;)Ldev/kord/codegen/ksp/processor/generator/ProcessingContext;
public static synthetic fun copy$default (Ldev/kord/codegen/ksp/processor/generator/ProcessingContext;Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;Lcom/squareup/kotlinpoet/FileSpec$Builder;Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;ILjava/lang/Object;)Ldev/kord/codegen/ksp/processor/generator/ProcessingContext;
public fun equals (Ljava/lang/Object;)Z
public final fun getDeclaration ()Lcom/google/devtools/ksp/symbol/KSClassDeclaration;
public final fun getEnvironment ()Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;
public final fun getFile ()Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun getPackageName ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

6 changes: 3 additions & 3 deletions ksp/src/test/kotlin/MockAnnotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ private val _arguments = listOf(
mockDefaultValueArgument("withDefault")
)

private fun mockValueArgumentInternal(_name: String, _value: Any?, origin: Origin = Origin.KOTLIN) =
private fun mockValueArgumentInternal(name: String, value: Any?, origin: Origin = Origin.KOTLIN) =
mockk<KSValueArgument> {
every { name } returns MockKSName(_name)
every { this@mockk.name } returns MockKSName(name)
every { this@mockk.origin } returns origin
every { value } returns _value
every { this@mockk.value } returns value
}

fun mockAnnotation() = mockk<KSAnnotation> {
Expand Down

0 comments on commit dcd1b13

Please sign in to comment.