diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt index 1a6003402..35cc397ce 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/Codegen.kt @@ -52,15 +52,19 @@ class CodegenScope : MutableList by mutableListOf() { @RegionCodegenDsl operator fun RegionCodegen.invoke(ktTypes: Collection) { - 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 by mutableListOf() { annotation class CodegenDsl } +internal fun String.mapLine(mapper: (String) -> CharSequence) = this.lines().joinToString("\n", transform = mapper) + @DslMarker annotation class RegionCodegenDsl