Skip to content

Commit

Permalink
Update writer handling (#119)
Browse files Browse the repository at this point in the history
* Update writer call

* Add helper class which holds the instances of each writer
  • Loading branch information
theEvilReaper authored May 26, 2024
1 parent 63a1d46 commit 70a72b6
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 28 deletions.
17 changes: 2 additions & 15 deletions src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package net.theevilreaper.dartpoet
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.clazz.ClassSpec
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.DartFileWriter
import net.theevilreaper.dartpoet.directive.DartDirective
import net.theevilreaper.dartpoet.directive.ExportDirective
import net.theevilreaper.dartpoet.directive.LibraryDirective
import net.theevilreaper.dartpoet.directive.PartDirective
import net.theevilreaper.dartpoet.directive.RelativeDirective
import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.function.FunctionSpec
import net.theevilreaper.dartpoet.util.*
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.util.DART_FILE_ENDING
Expand Down Expand Up @@ -58,23 +57,11 @@ class DartFile internal constructor(
}

internal fun write(codeWriter: CodeWriter) {
DartFileWriter().write(this, codeWriter)
WriterHelper.fileWriter.write(this, codeWriter)
}

override fun toString() = buildCodeString { write(this) }

internal val callEmit: (Any, CodeWriter) -> Unit = { o: Any, c: CodeWriter ->
emitInternal(o, c)
}

private fun emitInternal(o: Any, c: CodeWriter) {
when (o::class) {
FunctionSpec::class -> {
callEmit(o, c)
}
}
}

/**
* Writes the content from a [DartFile] to the given [Appendable].
* @param out the [Appendable] where the content should be written
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.theevilreaper.dartpoet.annotation

import net.theevilreaper.dartpoet.code.*
import net.theevilreaper.dartpoet.code.writer.AnnotationWriter
import net.theevilreaper.dartpoet.type.ClassName
import net.theevilreaper.dartpoet.type.TypeName
import net.theevilreaper.dartpoet.type.asClassName
Expand Down Expand Up @@ -41,7 +40,7 @@ class AnnotationSpec internal constructor(
codeWriter: CodeWriter,
inline: Boolean = true,
) {
AnnotationWriter().emit(this, codeWriter, inline = inline)
WriterHelper.annotationWriter.emit(this, codeWriter, inline = inline)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.theevilreaper.dartpoet.DartModifier.WITH
import net.theevilreaper.dartpoet.InheritKeyword
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ClassWriter
import net.theevilreaper.dartpoet.enum.EnumPropertySpec
Expand Down Expand Up @@ -75,7 +76,7 @@ class ClassSpec internal constructor(
* @param codeWriter the [CodeWriter] instance to apply the data
*/
internal fun write(codeWriter: CodeWriter) {
ClassWriter().write(this, codeWriter)
WriterHelper.classWriter.write(this, codeWriter)
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/net/theevilreaper/dartpoet/code/WriterHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.theevilreaper.dartpoet.code

import net.theevilreaper.dartpoet.code.writer.*
import org.jetbrains.annotations.ApiStatus.Internal
import org.jetbrains.annotations.ApiStatus.NonExtendable

/**
* A helper class that holds references to writer implementations.
* Maintaining a single instance of each writer improves runtime performance
* by avoiding the overhead of creating new instances for each write operation.
*
* Since writer classes do not store any state, creating new instances is unnecessary.
*
* **Note**: Update this class only when adding or removing writer implementations.
*
* @since 1.0.0
* author theEvilReaper
*/
@Internal
@NonExtendable
internal object WriterHelper {

internal val annotationWriter by lazy { AnnotationWriter() }
internal val classWriter by lazy { ClassWriter() }
internal val constantPropertyWriter by lazy { ConstantPropertyWriter() }
internal val constructorWriter by lazy { ConstructorWriter() }
internal val fileWriter by lazy { DartFileWriter() }
internal val enumPropertyWriter by lazy { EnumPropertyWriter() }
internal val extensionWriter by lazy { ExtensionWriter() }
internal val factoryWriter by lazy { FactoryWriter() }
internal val functionWriter by lazy { FunctionWriter() }
internal val parameterWriter by lazy { ParameterWriter() }
internal val propertyWriter by lazy { PropertyWriter() }
internal val typeDefWriter by lazy { TypeDefWriter() }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.theevilreaper.dartpoet.constructor

import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ConstructorWriter
import net.theevilreaper.dartpoet.util.toImmutableList
Expand Down Expand Up @@ -37,7 +38,7 @@ class ConstructorSpec internal constructor(
* @param codeWriter the instance to a [CodeWriter] to append the data
*/
internal fun write(codeWriter: CodeWriter) {
ConstructorWriter().write(this, codeWriter)
WriterHelper.constructorWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.theevilreaper.dartpoet.constructor.factory
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.code.CodeBlock
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.FactoryWriter
import net.theevilreaper.dartpoet.constructor.ConstructorBase
Expand Down Expand Up @@ -54,7 +55,7 @@ class FactorySpec(
* @see FactoryWriter
*/
internal fun write(codeWriter: CodeWriter) {
FactoryWriter().write(this, codeWriter)
WriterHelper.factoryWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.theevilreaper.dartpoet.enum

import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.EnumPropertyWriter
import net.theevilreaper.dartpoet.util.toImmutableList
Expand Down Expand Up @@ -31,7 +32,7 @@ class EnumPropertySpec internal constructor(


internal fun write(codeWriter: CodeWriter) {
EnumPropertyWriter().write(this, codeWriter)
WriterHelper.enumPropertyWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.extension

import net.theevilreaper.dartpoet.code.CodeBlock
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ExtensionWriter
import net.theevilreaper.dartpoet.function.FunctionSpec
Expand Down Expand Up @@ -68,7 +69,7 @@ class ExtensionSpec internal constructor(
* @param codeWriter the writer instance to apply the data as code
*/
internal fun write(codeWriter: CodeWriter) {
ExtensionWriter().write(this, codeWriter)
WriterHelper.extensionWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.code.CodeBlock
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.writer.FunctionWriter
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.parameter.ParameterSpec
Expand Down Expand Up @@ -73,7 +74,7 @@ class FunctionSpec internal constructor(
* @param codeWriter the writer instance
*/
internal fun write(codeWriter: CodeWriter) {
FunctionWriter().write(this, codeWriter)
WriterHelper.functionWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.theevilreaper.dartpoet.function.typedef

import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.FunctionWriter
import net.theevilreaper.dartpoet.code.writer.TypeDefWriter
import net.theevilreaper.dartpoet.type.ClassName
import net.theevilreaper.dartpoet.type.TypeName
import net.theevilreaper.dartpoet.type.asTypeName
Expand Down Expand Up @@ -50,7 +50,7 @@ class TypeDefSpec(
* @param codeWriter the writer instance
*/
internal fun write(codeWriter: CodeWriter) {
TypeDefWriter().write(this, codeWriter)
WriterHelper.typeDefWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.parameter

import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ParameterWriter
import net.theevilreaper.dartpoet.type.ClassName
Expand Down Expand Up @@ -51,7 +52,7 @@ class ParameterSpec internal constructor(
* @param codeWriter the [CodeWriter] to which the parameter should be written
*/
internal fun write(codeWriter: CodeWriter) {
ParameterWriter().write(this, codeWriter)
WriterHelper.parameterWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.theevilreaper.dartpoet.property
import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.writer.PropertyWriter
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.type.ClassName
Expand Down Expand Up @@ -60,7 +61,7 @@ class PropertySpec internal constructor(
* @param codeWriter the [CodeWriter] to apply the content from the spec
*/
internal fun write(codeWriter: CodeWriter) {
PropertyWriter().write(this, codeWriter)
WriterHelper.propertyWriter.write(this, codeWriter)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.property.consts

import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.WriterHelper
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ConstantPropertyWriter
import net.theevilreaper.dartpoet.property.PropertySpec
Expand Down Expand Up @@ -44,7 +45,7 @@ class ConstantPropertySpec(
* @param codeWriter the [CodeWriter] to apply the content from the spec
*/
internal fun write(codeWriter: CodeWriter) {
ConstantPropertyWriter().write(this, codeWriter)
WriterHelper.constantPropertyWriter.write(this, codeWriter)
}

/**
Expand Down

0 comments on commit 70a72b6

Please sign in to comment.