Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
theEvilReaper committed Jan 16, 2024
1 parent 4cc6944 commit fb5b875
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ class ClassBuilder internal constructor(
this.constantStack += constants
}

/**
* Add a [TypeDefSpec] to the spec.
* @param typeDefSpec the typedef to add
*/
fun typedef(typeDefSpec: TypeDefSpec) = apply {
this.typedefs += typeDefSpec
}

/**
* Add an array of [TypeDefSpec] to the spec.
* @param typeDefSpec the typedefs to add
*/
fun typedef(vararg typeDefSpec: TypeDefSpec) = apply {
this.typedefs += typeDefSpec
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.theevilreaper.dartpoet.util.toImmutableSet
* A [ClassBuilder] describes the actual content of the class.
* The content includes functions, typedefs, const values etc.
* Partly some things are also only allowed to be set on certain classes.
* @param builder the [ClassBuilder] instance to retrieve data from it
* @since 1.0.0
* @author theEvilReaper
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ import net.theevilreaper.dartpoet.code.joinToCode
import net.theevilreaper.dartpoet.util.ANNOTATION_CHAR
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.NEW_LINE
import net.theevilreaper.dartpoet.util.ROUND_CLOSE
import net.theevilreaper.dartpoet.util.ROUND_OPEN
import net.theevilreaper.dartpoet.util.toImmutableList

/**
* The [AnnotationWriter] is responsible for writing the data from an [AnnotationSpec] into valid code for dart code.
* @since 1.0.0
* @author theEvilReaper
*/
class AnnotationWriter {
internal class AnnotationWriter {

/**
* Writes the data which are stored in a [AnnotationSpec] to a given instance from a [CodeWriter].
* @param spec the spec to write
* @param writer the writer to write the spec to
* @param inline if the spec should be written inline
*/
fun emit(spec: AnnotationSpec, writer: CodeWriter, inline: Boolean) {
writer.emit(ANNOTATION_CHAR)
writer.emitCode("%T", spec.typeName)
Expand All @@ -26,17 +34,30 @@ class AnnotationWriter {
val memberSeparator = if (inline) ", " else ",\n"
val memberSuffix = if (!inline && spec.content.size > 1) "," else EMPTY_STRING

writer.emit("(")
writer.emit(ROUND_OPEN)
if (spec.hasMultipleContentParts) writer.emit(whitespace).indent()
writer.emitCode(
codeBlock = mapCodeBlocks(spec, inline, memberSeparator, memberSuffix),
isConstantContext = true,
)
if (spec.hasMultipleContentParts) writer.unindent().emit(whitespace)
writer.emit(")")
writer.emit(ROUND_CLOSE)
}

private fun mapCodeBlocks(spec: AnnotationSpec, inline: Boolean, memberSeparator: String, memberSuffix: String): CodeBlock {
/**
* Maps the content from an [AnnotationSpec] into a [CodeBlock] reference.
* @param spec the spec to map
* @param inline if the spec should be written inline
* @param memberSeparator the separator for the members
* @param memberSuffix the suffix for the members
* @return the created [CodeBlock] reference
*/
private fun mapCodeBlocks(
spec: AnnotationSpec,
inline: Boolean,
memberSeparator: String,
memberSuffix: String
): CodeBlock {
return spec.content.toImmutableList()
.map { if (inline) it.replaceAll("[⇥|⇤]", EMPTY_STRING) else it }
.joinToCode(separator = memberSeparator, suffix = memberSuffix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.util.SEMICOLON
import net.theevilreaper.dartpoet.util.SPACE

internal class ConstantPropertyWriter : Writeable<ConstantPropertySpec>, InitializerAppender<PropertyWriter>, VariableAppender {
/**
* The [ConstantPropertyWriter] is responsible for writing the data of the [ConstantPropertySpec] to a [CodeWriter].
* @since 1.0.0
* @author theEvilReaper
*/
internal class ConstantPropertyWriter : Writeable<ConstantPropertySpec>, InitializerAppender<PropertyWriter>,
VariableAppender {

/**
* Writes the data from a provided [ConstantPropertySpec] to the given [CodeWriter] instance.
*
* @param spec the ConstantPropertySpec which should be written
* @param writer the instance from the [CodeWriter] to append the data
**/
override fun write(spec: ConstantPropertySpec, writer: CodeWriter) {
val modifiersAsString = spec.modifiers.joinToString(separator = SPACE, postfix = SPACE) { it.identifier }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import net.theevilreaper.dartpoet.util.SPACE
* @author theEvilReaper
* @since 1.0.0
*/
internal class PropertyWriter : Writeable<PropertySpec>, VariableAppender, DocumentationAppender, InitializerAppender<PropertySpec> {
internal class PropertyWriter : Writeable<PropertySpec>, VariableAppender, DocumentationAppender,
InitializerAppender<PropertySpec> {

/**
* Writes the given [PropertySpec] into the [CodeWriter].
Expand Down

0 comments on commit fb5b875

Please sign in to comment.