-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b865c3f
commit 7a061dc
Showing
8 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.kord.codegen.ksp.annotations | ||
|
||
@Repeatable | ||
@MustBeDocumented | ||
@Target(AnnotationTarget.FILE) | ||
@Retention(AnnotationRetention.SOURCE) | ||
@ProcessorAnnotation("dev.kord.codegen.generator.annotator") | ||
public annotation class Annotator( | ||
val fromPackage: String, | ||
val ignore: Array<String> = [] | ||
) |
85 changes: 85 additions & 0 deletions
85
...erationSource/generationSourceMain/kotlin/dev/kord/codegen/kotlinpoet/js/JsAnnotations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package dev.kord.codegen.kotlinpoet.js | ||
|
||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import dev.kord.codegen.kotlinpoet.addAnnotation | ||
import kotlin.String | ||
|
||
/** | ||
* Adds `JsName` to this [FunSpec] | ||
*/ | ||
public fun FunSpec.Builder.jsName(name: String) { | ||
addAnnotation(ClassName("kotlin.js", "JsName")) { | ||
addMember("%S", name) | ||
} | ||
} | ||
|
||
/** | ||
* Adds `JsName` to this [TypeSpec] | ||
*/ | ||
public fun TypeSpec.Builder.jsName(name: String) { | ||
addAnnotation(ClassName("kotlin.js", "JsName")) { | ||
addMember("%S", name) | ||
} | ||
} | ||
|
||
/** | ||
* Adds `JsName` to this [PropertySpec] | ||
*/ | ||
public fun PropertySpec.Builder.jsName(name: String) { | ||
addAnnotation(ClassName("kotlin.js", "JsName")) { | ||
addMember("%S", name) | ||
} | ||
} | ||
|
||
/** | ||
* Adds `JsExport` to this [FileSpec] | ||
*/ | ||
public fun FileSpec.Builder.jsExport() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport")) | ||
} | ||
|
||
/** | ||
* Adds `JsExport` to this [FunSpec] | ||
*/ | ||
public fun FunSpec.Builder.jsExport() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport")) | ||
} | ||
|
||
/** | ||
* Adds `JsExport` to this [TypeSpec] | ||
*/ | ||
public fun TypeSpec.Builder.jsExport() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport")) | ||
} | ||
|
||
/** | ||
* Adds `JsExport` to this [PropertySpec] | ||
*/ | ||
public fun PropertySpec.Builder.jsExport() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport")) | ||
} | ||
|
||
/** | ||
* Adds `Ignore` to this [FunSpec] | ||
*/ | ||
public fun FunSpec.Builder.ignore() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport", "Ignore")) | ||
} | ||
|
||
/** | ||
* Adds `Ignore` to this [TypeSpec] | ||
*/ | ||
public fun TypeSpec.Builder.ignore() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport", "Ignore")) | ||
} | ||
|
||
/** | ||
* Adds `Ignore` to this [PropertySpec] | ||
*/ | ||
public fun PropertySpec.Builder.ignore() { | ||
addAnnotation(ClassName("kotlin.js", "JsExport", "Ignore")) | ||
} |
33 changes: 33 additions & 0 deletions
33
kotlinpoet/processor/src/main/kotlin/AnnotatorProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.kord.codegen.generator | ||
|
||
import com.google.devtools.ksp.containingFile | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSFile | ||
import dev.kord.codegen.generator.annotator.getAnnotators | ||
import dev.kord.codegen.generator.annotator.processAnnotators | ||
import dev.kord.codegen.ksp.annotations.Annotator | ||
import dev.kord.codegen.ksp.annotations.InlineConstructor | ||
import dev.kord.codegen.ksp.getSymbolsWithAnnotation | ||
|
||
/** | ||
* Please refer to the documentation of [InlineConstructor]. | ||
*/ | ||
class AnnotatorProcessor private constructor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor { | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
resolver.getSymbolsWithAnnotation<Annotator>() | ||
.forEach { | ||
val annotators = it.getAnnotators() | ||
environment.processAnnotators(resolver, it as KSFile, annotators) | ||
} | ||
|
||
return emptyList() | ||
} | ||
|
||
class Provider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = AnnotatorProcessor(environment) | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
kotlinpoet/processor/src/main/kotlin/annotator/Annotator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
@file:JvmName("AnnotatorGenerator") | ||
|
||
package dev.kord.codegen.generator.annotator | ||
|
||
import com.google.devtools.ksp.KspExperimental | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSFile | ||
import com.squareup.kotlinpoet.* | ||
import com.squareup.kotlinpoet.ksp.addOriginatingKSFile | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
import dev.kord.codegen.generator.utils.ADD_ANNOTATION | ||
import dev.kord.codegen.generator.utils.mapToValueParameterList | ||
import dev.kord.codegen.kotlinpoet.CodeBlock | ||
import dev.kord.codegen.kotlinpoet.FileSpec | ||
import dev.kord.codegen.kotlinpoet.addFunction | ||
import dev.kord.codegen.kotlinpoet.withControlFlow | ||
import dev.kord.codegen.ksp.annotations.AnnotationArguments.Companion.arguments | ||
import dev.kord.codegen.ksp.getAnnotationByType | ||
|
||
@OptIn(KspExperimental::class) | ||
fun SymbolProcessorEnvironment.processAnnotators(resolver: Resolver, origin: KSFile, annotators: Sequence<Annotator>) { | ||
val fileSpec = FileSpec(origin.packageName.asString(), origin.fileName.dropLast(3)) { | ||
annotators.forEach { annotator -> | ||
resolver.getDeclarationsFromPackage(annotator.fromPackage) | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { it.classKind == ClassKind.ANNOTATION_CLASS } | ||
.filter { it.simpleName.asString() !in annotator.ignore } | ||
.forEach { | ||
val targetAnnotation = it.getAnnotationByType<Target>() | ||
val targetsRaw = targetAnnotation.arguments<Target>() | ||
val targets = targetsRaw[Target::allowedTargets]!! | ||
it.generateAnnotator(origin, this) | ||
} | ||
} | ||
} | ||
|
||
fileSpec.writeTo(codeGenerator, false) | ||
} | ||
|
||
private fun KSClassDeclaration.generateAnnotator(origin: KSFile, builder: FileSpec.Builder) { | ||
receiversByTarget.forEach { | ||
builder.addFunction(simpleName.asString().replaceFirstChar { it.lowercase() }) { | ||
addKdoc("Adds `%N` to this [%T]", toClassName().simpleName, it.enclosingClassName()!!) | ||
addOriginatingKSFile(origin) | ||
receiver(it) | ||
primaryConstructor!!.parameters.forEach { | ||
addParameter(it.name!!.asString(), it.type.toTypeName()) | ||
} | ||
|
||
val type = toClassName() | ||
|
||
if (parameters.isEmpty()) { | ||
addCode( | ||
"%M(%L)", | ||
ADD_ANNOTATION, | ||
type.toLiteral() | ||
) | ||
} else { | ||
withControlFlow( | ||
"%M(%L)", ADD_ANNOTATION, | ||
type.toLiteral() | ||
) { | ||
val placeHolders = parameters.map { | ||
if (it.type == STRING) { | ||
CodeBlock.of("%%S") | ||
} else { | ||
CodeBlock.of("%%L") | ||
} | ||
}.joinToCode(", ") | ||
|
||
addStatement("addMember(%S, %L)", placeHolders, parameters.mapToValueParameterList()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun ClassName.toLiteral() = CodeBlock { | ||
add("%T(", ClassName::class.asClassName()) | ||
val parameters = (listOf(packageName) + simpleNames).map { | ||
CodeBlock.of("%S", it) | ||
}.joinToCode(", ") | ||
add("%L", parameters) | ||
add(")") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dev.kord.codegen.generator.CodeProcessor$Provider | ||
dev.kord.codegen.generator.ConstructorInliner$Provider | ||
dev.kord.codegen.generator.AnnotatorProcessor$Provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@file:Annotator( | ||
"kotlin.js", | ||
ignore = ["ExperimentalJsExport"] | ||
) | ||
|
||
package dev.kord.codegen.kotlinpoet.js | ||
|
||
import dev.kord.codegen.ksp.annotations.Annotator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters