-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise typedef handling and generation (#77)
* Outsource typedefs into an own structure * Add new extension method to write the typedef objects * Add structure to manage or write typedef objects * Integrate the updated typedef structure into other spec objects, writers * Add test classes for the typedef structure * Update given tests to support typedef creation * Remove typedef as valid function modifier * Remove empty line * Remove unused code * Improve code layout * Remove out commented code * Improve new line write * Add support for multiple cast arguments * Add test for the multiple cast arguments * Remove unused import
- Loading branch information
1 parent
abc2577
commit a03c810
Showing
18 changed files
with
455 additions
and
60 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
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
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
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
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
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
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
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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/net/theevilreaper/dartpoet/code/writer/TypeDefWriter.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,31 @@ | ||
package net.theevilreaper.dartpoet.code.writer | ||
|
||
import net.theevilreaper.dartpoet.DartModifier | ||
import net.theevilreaper.dartpoet.code.CodeWriter | ||
import net.theevilreaper.dartpoet.code.Writeable | ||
import net.theevilreaper.dartpoet.code.emitParameters | ||
import net.theevilreaper.dartpoet.function.typedef.TypeDefSpec | ||
import net.theevilreaper.dartpoet.util.SEMICOLON | ||
|
||
class TypeDefWriter : Writeable<TypeDefSpec> { | ||
override fun write(spec: TypeDefSpec, writer: CodeWriter) { | ||
writer.emit("${DartModifier.TYPEDEF.identifier}·${spec.typeDefName}") | ||
if (spec.typeCasts.isNotEmpty()) { | ||
val typesAsString = spec.typeCasts.joinToString(separator = ",·") { it.toString() } | ||
writer.emitCode("<%L>", typesAsString) | ||
} | ||
writer.emit("·=·") | ||
writer.emitCode("%T", spec.returnType) | ||
|
||
if (spec.name != null) { | ||
writer.emitCode("·%L", spec.name) | ||
} | ||
|
||
if (spec.hasParameters) { | ||
writer.emit("(") | ||
spec.parameters.emitParameters(writer, forceNewLines = false, emitBrackets = false, emitSpace = spec.parameters.size > 1) | ||
writer.emit(")") | ||
} | ||
writer.emit(SEMICOLON) | ||
} | ||
} |
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
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
103 changes: 103 additions & 0 deletions
103
src/main/kotlin/net/theevilreaper/dartpoet/function/typedef/TypeDefBuilder.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,103 @@ | ||
package net.theevilreaper.dartpoet.function.typedef | ||
|
||
import net.theevilreaper.dartpoet.parameter.ParameterSpec | ||
import net.theevilreaper.dartpoet.type.ClassName | ||
import net.theevilreaper.dartpoet.type.TypeName | ||
import net.theevilreaper.dartpoet.type.asTypeName | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* The builder is used to create a type definition with a specific name and optional type cast. | ||
* After the construction the builder maps the data into a [TypeDefSpec] object. | ||
* | ||
* @property typeDefName the name of the type definition. | ||
* @property typeCasts optional array of type-cast for the type definition. | ||
*/ | ||
class TypeDefBuilder internal constructor( | ||
val typeDefName: String, | ||
vararg val typeCasts: TypeName? = emptyArray() | ||
) { | ||
/** | ||
* The name of the type definition. | ||
*/ | ||
var name: String? = null | ||
|
||
/** | ||
* The return type of the type definition. | ||
*/ | ||
var returnType: TypeName? = null | ||
|
||
/** | ||
* List of parameters associated with the type definition. | ||
*/ | ||
val parameters: MutableList<ParameterSpec> = mutableListOf() | ||
|
||
/** | ||
* Sets the name of the type definition. | ||
* | ||
* @param name the name of the type definition. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun name(name: String) = apply { | ||
this.name = name | ||
} | ||
|
||
/** | ||
* Adds a parameter to the list of parameters. | ||
* | ||
* @param parameterSpec the parameter specification. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun parameter(parameterSpec: ParameterSpec) = apply { | ||
this.parameters += parameterSpec | ||
} | ||
|
||
/** | ||
* Adds multiple parameters to the list of parameters. | ||
* | ||
* @param parameterSpecs the parameter specifications. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun parameters(vararg parameterSpecs: ParameterSpec) = apply { | ||
this.parameters += parameterSpecs | ||
} | ||
|
||
/** | ||
* Sets the return type of the type definition. | ||
* | ||
* @param typeName the return type as a [TypeName]. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun returns(typeName: TypeName) = apply { | ||
this.returnType = typeName | ||
} | ||
|
||
/** | ||
* Sets the return type of the type definition. | ||
* | ||
* @param typeName the return type as a [ClassName]. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun returns(typeName: ClassName) = apply { | ||
this.returnType = typeName | ||
} | ||
|
||
/** | ||
* Sets the return type of the type definition using a [KClass]. | ||
* | ||
* @param typeName the return type as a [KClass]. | ||
* @return the current instance of [TypeDefBuilder]. | ||
*/ | ||
fun returns(typeName: KClass<*>) = apply { | ||
this.returnType = typeName.asTypeName() | ||
} | ||
|
||
/** | ||
* Builds and returns an instance of [TypeDefSpec] based on the configuration. | ||
* | ||
* @return an instance of [TypeDefSpec]. | ||
*/ | ||
fun build(): TypeDefSpec { | ||
return TypeDefSpec(this) | ||
} | ||
} |
Oops, something went wrong.