Skip to content

Commit

Permalink
add kdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Sep 11, 2024
1 parent ada9b8c commit d321489
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,29 @@ class KotlinAnnotationSpecBuilder internal constructor(
DelegatingBuilder<KotlinAnnotationSpecBuilder, AnnotationSpecBuilderReceiver> {

companion object {

/**
* Creates new builder.
*/
fun builder(type: ClassName): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(
delegate = AnnotationSpecBuilder.builder(type)
)

/**
* Creates new builder.
*/
fun builder(type: ParameterizedTypeName): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(
delegate = AnnotationSpecBuilder.builder(type)
)

/**
* Creates new builder.
*/
fun builder(type: KClass<out Annotation>): KotlinAnnotationSpecBuilder = builder(type.asClassName())

/**
* Creates new builder from spec.
*/
fun from(spec: KotlinAnnotationSpecSupplier) = KotlinAnnotationSpecBuilder(
delegate = spec.get().toBuilder().wrap()
)
Expand All @@ -69,28 +82,69 @@ class KotlinAnnotationSpecBuilder internal constructor(
private var multiLine = false
private val members: MutableList<CodeBlock> = mutableListOf()



/**
* If marked multiline all members become a new line.
*/
fun multiLine() = apply { multiLine = true }

/**
* Add member to annotation.
*/
fun addMember(codeBlock: CodeBlock): KotlinAnnotationSpecBuilder = apply { members.add(codeBlock) }

/**
* Add member to annotation.
*/
fun addMember(format: String, vararg args: Any): KotlinAnnotationSpecBuilder = addMember(buildCodeBlock(format, *args))

/**
* Add member to annotation.
*/
fun addNameMember(memberName: MemberName): KotlinAnnotationSpecBuilder = addMember("%M", memberName)

/**
* Add member to annotation.
*/
fun addKClassMember(name: String, value: KClass<*>) = addMember(member.kclass(name, value))

/**
* Add member to annotation.
*/
fun addKClassMembers(name: String, vararg values: KClass<*>) = addMember(member.kclasses(name, *values))

/**
* Add member to annotation.
*/
fun addStringMember(name: String, value: String) = addMember(member.string(name, value))

/**
* Add member to annotation.
*/
fun addStringMembers(name: String, vararg values: String) = addMember(member.strings(name, *values))

/**
* Add member to annotation.
*/
fun addEnumMember(name: String, value: Enum<*>): KotlinAnnotationSpecBuilder = addMember(member.enum(name, value))

/**
* Add member to annotation.
*/
fun addEnumMembers(name: String, vararg values: Enum<*>): KotlinAnnotationSpecBuilder = addMember(member.enums(name, *values))

/**
* Add member to annotation.
*/
fun addNumberMember(name: String, value: Number): KotlinAnnotationSpecBuilder = addMember(member.number(name, value))

/**
* Add member to annotation.
*/
fun addNumberMembers(name: String, vararg values: Number): KotlinAnnotationSpecBuilder = addMember(member.numbers(name, *values))

/**
* Remove all members.
*/
fun clearMembers() = apply { members.clear() }


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ class KotlinAnonymousClassSpecBuilder internal constructor(
DelegatingBuilder<KotlinAnonymousClassSpecBuilder, TypeSpecBuilderReceiver> {

companion object {
/**
* Creates new builder.
*/
fun builder(): KotlinAnonymousClassSpecBuilder = KotlinAnonymousClassSpecBuilder()
}

internal constructor() : this(delegate = TypeSpecBuilder.anonymousClassBuilder())

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 38 in kotlin-code-generation/src/main/kotlin/builder/KotlinAnonymousClassSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinAnonymousClassSpecBuilder.kt#L38

Added line #L38 was not covered by tests

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }

fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }

fun superclass(superclass: TypeName) = builder { this.superclass(superclass) }
fun superclass(superclass: KClass<*>) = builder { this.superclass(superclass) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class KotlinClassSpecBuilder internal constructor(
KotlinTypeSpecHolderBuilder<KotlinClassSpecBuilder> {

companion object {
/**
* Creates new builder.
*/
fun builder(name: String): KotlinClassSpecBuilder = builder(simpleClassName(name))

/**
* Creates new builder.
*/
fun builder(className: ClassName): KotlinClassSpecBuilder = KotlinClassSpecBuilder(className = className)
}

Expand All @@ -37,7 +44,7 @@ class KotlinClassSpecBuilder internal constructor(
internal val constructorProperties: LinkedHashMap<PropertyName, KotlinConstructorPropertySpecSupplier> = LinkedHashMap()
private var isSetPrimaryConstructor: Boolean = false

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 47 in kotlin-code-generation/src/main/kotlin/builder/KotlinClassSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinClassSpecBuilder.kt#L47

Added line #L47 was not covered by tests
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }

fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ class KotlinCompanionObjectSpecBuilder internal constructor(
KotlinTypeSpecHolderBuilder<KotlinCompanionObjectSpecBuilder> {

companion object {
/**
* Creates new builder.
*/
fun builder(name: String? = null): KotlinCompanionObjectSpecBuilder = KotlinCompanionObjectSpecBuilder(name)
}

internal constructor(name: String? = null) : this(TypeSpecBuilder.Companion.companionObjectBuilder(name))

fun addOriginatingElement(originatingElement: Element) = builder {
internal fun addOriginatingElement(originatingElement: Element) = builder {

Check warning on line 39 in kotlin-code-generation/src/main/kotlin/builder/KotlinCompanionObjectSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinCompanionObjectSpecBuilder.kt#L39

Added line #L39 was not covered by tests
delegate.addOriginatingElement(
originatingElement
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class KotlinConstructorPropertySpecBuilder internal constructor(
KotlinConstructorPropertySpecSupplier {

companion object {

/**
* Creates new builder.
*/
fun builder(name: String, type: TypeName): KotlinConstructorPropertySpecBuilder = KotlinConstructorPropertySpecBuilder(
name = name,
type = type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ class KotlinDataClassSpecBuilder internal constructor(
KotlinSuperInterfaceSupport<KotlinDataClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinDataClassSpecBuilder> {
companion object : KLogging() {
/**
* Creates new builder.
*/
fun builder(name: String): KotlinDataClassSpecBuilder = builder(simpleClassName(name))

/**
* Creates new builder.
*/
fun builder(className: ClassName): KotlinDataClassSpecBuilder = KotlinDataClassSpecBuilder(className)
}

Expand All @@ -42,7 +49,7 @@ class KotlinDataClassSpecBuilder internal constructor(

private val constructorProperties = LinkedHashMap<String, KotlinConstructorPropertySpecSupplier>()

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 52 in kotlin-code-generation/src/main/kotlin/builder/KotlinDataClassSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinDataClassSpecBuilder.kt#L52

Added line #L52 was not covered by tests

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class KotlinEnumClassSpecBuilder internal constructor(
KotlinTypeSpecHolderBuilder<KotlinEnumClassSpecBuilder> {

companion object {
/**
* Creates new builder.
*/
fun builder(name: String): KotlinEnumClassSpecBuilder = builder(simpleClassName(name))

/**
* Creates new builder.
*/
fun builder(className: ClassName): KotlinEnumClassSpecBuilder = KotlinEnumClassSpecBuilder(className)
}

Expand All @@ -39,7 +46,7 @@ class KotlinEnumClassSpecBuilder internal constructor(
fun addEnumConstant(name: String): KotlinEnumClassSpecBuilder = apply { delegate.addEnumConstant(name) }
fun addEnumConstant(name: String, typeSpec: TypeSpec = TypeSpec.anonymousClassBuilder().build()): KotlinEnumClassSpecBuilder = builder { this.addEnumConstant(name, typeSpec) }

fun addOriginatingElement(originatingElement: Element): KotlinEnumClassSpecBuilder = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element): KotlinEnumClassSpecBuilder = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 49 in kotlin-code-generation/src/main/kotlin/builder/KotlinEnumClassSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinEnumClassSpecBuilder.kt#L49

Added line #L49 was not covered by tests

fun addTypeVariable(typeVariable: TypeVariableName): KotlinEnumClassSpecBuilder = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?): KotlinEnumClassSpecBuilder = builder { this.primaryConstructor(primaryConstructor?.get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,43 @@ class KotlinFileSpecBuilder internal constructor(
KotlinMemberSpecHolderBuilder<KotlinFileSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinFileSpecBuilder> {
companion object {
/**
* Creates new builder.
*/
fun builder(className: ClassName): KotlinFileSpecBuilder {
require(className.packageName.isNotEmpty()) { "cannot build file for empty package." }
return KotlinFileSpecBuilder(delegate = FileSpecBuilder.builder(className))
}

/**
* Creates new builder.
*/
fun builder(memberName: MemberName): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.builder(memberName)
)

/**
* Creates new builder.
*/
fun builder(packageName: String, fileName: String): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.builder(packageName, fileName)
)

/**
* Creates new builder.
*/
fun scriptBuilder(fileName: String, packageName: String = ""): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.scriptBuilder(fileName, packageName)
)

/**
* Creates new builder.
*/
fun builder(spec: KotlinFileSpec) = builder(spec.get())

/**
* Creates new builder.
*/
fun builder(spec: FileSpec) = KotlinFileSpecBuilder(delegate = spec.toBuilder().wrap())
}

Expand Down Expand Up @@ -71,7 +89,6 @@ class KotlinFileSpecBuilder internal constructor(
fun nextControlFlow(controlFlow: String, vararg args: Any) = builder { this.nextControlFlow(controlFlow, *args) }
fun endControlFlow() = builder { this.endControlFlow() }


override fun build(): KotlinFileSpec {
val spec = delegate.build()
return KotlinFileSpec(spec = spec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,55 @@ class KotlinFunSpecBuilder internal constructor(
KotlinFunSpecSupplier {

companion object {
/**
* Creates new builder.
*/
fun builder(name: String): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.builder(name)
)

/**
* Creates new builder.
*/
fun builder(memberName: MemberName): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.builder(memberName)
)

/**
* Creates new builder.
*/
fun constructorBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.constructorBuilder()
)

/**
* Creates new builder.
*/
fun getterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.getterBuilder()
)

/**
* Creates new builder.
*/
fun setterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.setterBuilder()
)

/**
* Creates new builder.
*/
fun builder(spec: KotlinFunSpec) = builder(spec.get())

/**
* Creates new builder.
*/
fun builder(spec: FunSpec) = KotlinFunSpecBuilder(delegate = spec.toBuilder().wrap())
}

fun addParameter(parameter: KotlinParameterSpecSupplier) = builder { this.addParameter(parameter.get()) }

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 78 in kotlin-code-generation/src/main/kotlin/builder/KotlinFunSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinFunSpecBuilder.kt#L78

Added line #L78 was not covered by tests
fun jvmModifiers(modifiers: Iterable<Modifier>) = builder { this.jvmModifiers(modifiers) }
fun addTypeVariables(typeVariables: Iterable<TypeVariableName>) = builder { this.addTypeVariables(typeVariables) }
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ class KotlinInterfaceSpecBuilder internal constructor(
KotlinTypeSpecHolderBuilder<KotlinInterfaceSpecBuilder> {
companion object {
private const val DEFAULT_IS_FUNCTIONAL = false

/**
* Creates new builder.
*/
fun builder(name: String, isFunctionInterface: Boolean = DEFAULT_IS_FUNCTIONAL): KotlinInterfaceSpecBuilder = builder(simpleClassName(name), isFunctionInterface)

/**
* Creates new builder.
*/
fun builder(className: ClassName, isFunctionInterface: Boolean = DEFAULT_IS_FUNCTIONAL): KotlinInterfaceSpecBuilder = KotlinInterfaceSpecBuilder(className, isFunctionInterface)
}

Expand All @@ -37,7 +45,7 @@ class KotlinInterfaceSpecBuilder internal constructor(
delegate = if (funInterface) TypeSpecBuilder.funInterfaceBuilder(className) else TypeSpecBuilder.interfaceBuilder(className)
)

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }

Check warning on line 49 in kotlin-code-generation/src/main/kotlin/builder/KotlinInterfaceSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinInterfaceSpecBuilder.kt#L48-L49

Added lines #L48 - L49 were not covered by tests

fun addInitializerBlock(block: CodeBlock) = builder { this.addInitializerBlock(block) }

Check warning on line 51 in kotlin-code-generation/src/main/kotlin/builder/KotlinInterfaceSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinInterfaceSpecBuilder.kt#L51

Added line #L51 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ class KotlinObjectSpecBuilder internal constructor(
KotlinSuperInterfaceSupport<KotlinObjectSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinObjectSpecBuilder> {
companion object {

/**
* Creates new builder.
*/
fun builder(name: String): KotlinObjectSpecBuilder = builder(simpleClassName(name))

/**
* Creates new builder.
*/
fun builder(className: ClassName): KotlinObjectSpecBuilder = KotlinObjectSpecBuilder(className = className)
}

internal constructor(className: ClassName) : this(className, TypeSpecBuilder.objectBuilder(className))

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
internal fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

Check warning on line 44 in kotlin-code-generation/src/main/kotlin/builder/KotlinObjectSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinObjectSpecBuilder.kt#L44

Added line #L44 was not covered by tests

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }

Check warning on line 46 in kotlin-code-generation/src/main/kotlin/builder/KotlinObjectSpecBuilder.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/builder/KotlinObjectSpecBuilder.kt#L46

Added line #L46 was not covered by tests
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
Expand Down
Loading

0 comments on commit d321489

Please sign in to comment.