mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Support indenting for codegen
This commit is contained in:
parent
6b4124895f
commit
9f0465c91b
@ -52,15 +52,19 @@ class CodegenScope : MutableList<Replacer> by mutableListOf() {
|
||||
|
||||
@RegionCodegenDsl
|
||||
operator fun RegionCodegen.invoke(ktTypes: Collection<KtType>) {
|
||||
add(Replacer {
|
||||
it.replace(Regex("""//// region $regionName CODEGEN ////([\s\S]*?)//// endregion $regionName CODEGEN ////""")) {
|
||||
val code = CodegenScope().apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) }.applyTo("")
|
||||
add(Replacer { content ->
|
||||
content.replace(Regex("""//// region $regionName CODEGEN ////([\s\S]*?)( *)//// endregion $regionName CODEGEN ////""")) { result ->
|
||||
val indent = result.groups[2]!!.value
|
||||
val indentedCode = CodegenScope()
|
||||
.apply { (this@invoke as Codegen).invoke(*ktTypes.toTypedArray()) }
|
||||
.applyTo("")
|
||||
.mapLine { "${indent}$it" }
|
||||
"""
|
||||
|//// region $regionName CODEGEN ////
|
||||
|
|
||||
|$code
|
||||
|$indentedCode
|
||||
|
|
||||
|//// endregion $regionName CODEGEN ////
|
||||
|${indent}//// endregion $regionName CODEGEN ////
|
||||
""".trimMargin()
|
||||
}
|
||||
})
|
||||
@ -70,6 +74,8 @@ class CodegenScope : MutableList<Replacer> by mutableListOf() {
|
||||
annotation class CodegenDsl
|
||||
}
|
||||
|
||||
internal fun String.mapLine(mapper: (String) -> CharSequence) = this.lines().joinToString("\n", transform = mapper)
|
||||
|
||||
@DslMarker
|
||||
annotation class RegionCodegenDsl
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user