diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/CodeWriter.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/CodeWriter.kt index b0b82120..f22cfa74 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/CodeWriter.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/CodeWriter.kt @@ -49,13 +49,20 @@ internal class CodeWriter( */ var statementLine = -1 - fun indent(levels: Int = 1) = apply { + fun indent(levels: Int = 1): CodeWriter = apply { indentLevel += levels + if (!trailingNewline) { + emit("\n") + } } - fun unindent(levels: Int = 1) = apply { + fun unindent(levels: Int = 1): CodeWriter = apply { require(indentLevel - levels >= 0) { "cannot unindent $levels from $indentLevel" } indentLevel -= levels + + if (!trailingNewline) { + emit("\n") + } } val currentModule: String get() = this.moduleStack.last() @@ -242,7 +249,7 @@ internal class CodeWriter( statementLine = -1 } - "%W" -> out.wrappingSpace(indentLevel + 2) + "%W" -> emitWrappingSpace() else -> emit(part) } @@ -251,6 +258,7 @@ internal class CodeWriter( fun emitWrappingSpace() = apply { out.wrappingSpace(indentLevel + 2) + trailingNewline = false } private fun emitLiteral(o: Any?) { @@ -370,7 +378,7 @@ internal class CodeWriter( * [CodeWriter.out] does it through here, since we emit indentation lazily in order to avoid * unnecessary trailing whitespace. */ - fun emit(s: String) = apply { + fun emit(s: String): CodeWriter = apply { var first = true for (line in s.split('\n')) { // Emit a newline character. Make sure blank lines in doc & comments look good. @@ -403,7 +411,7 @@ internal class CodeWriter( } out.append(line) - trailingNewline = false + trailingNewline = line.trimEnd { it.isWhitespace() && it != '\n' }.endsWith('\n') } } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ExtensionSpec.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ExtensionSpec.kt index 43589b9d..0bf5426f 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ExtensionSpec.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ExtensionSpec.kt @@ -109,11 +109,7 @@ class ExtensionSpec private constructor( codeWriter.popType() codeWriter.popModule() - if (typeSpecs.isNotEmpty() || propertySpecs.isNotEmpty() || funSpecs.isNotEmpty()) { - codeWriter.emit("\n") - } - - codeWriter.emit("}\n") + codeWriter.emit("\n}\n") } finally { codeWriter.statementLine = previousStatementLine } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/FileSpec.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/FileSpec.kt index d0f68609..a61656b9 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/FileSpec.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/FileSpec.kt @@ -202,4 +202,4 @@ class FileSpec private constructor( } } -internal const val DEFAULT_INDENT = " " +internal const val DEFAULT_INDENT = " " diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ParameterSpec.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ParameterSpec.kt index 13d0fd43..d98d6106 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ParameterSpec.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/ParameterSpec.kt @@ -146,14 +146,12 @@ internal fun List.emit( emit("(") when { size > 2 || forceNewLines -> { - emit("\n") indent(1) forEachIndexed { index, parameter -> if (index > 0) emit(",\n") emitParameter(parameter) } unindent(1) - emit("\n") } size == 0 -> emit("") size == 1 -> emitParameter(params[0]) diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/TypeSpec.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/TypeSpec.kt index 3b3006ee..4c44e84e 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/TypeSpec.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/io/outfoxx/swiftpoet/TypeSpec.kt @@ -85,11 +85,9 @@ class TypeSpec private constructor( codeWriter.pushType(this) codeWriter.indent() - var firstMember = true if (associatedTypes.isNotEmpty()) { codeWriter.emit("\n") - firstMember = false for (associatedType in associatedTypes) { codeWriter.emit("associatedtype ") associatedType.emit(codeWriter) @@ -100,7 +98,6 @@ class TypeSpec private constructor( if (enumCases.isNotEmpty()) { codeWriter.emit("\n") - firstMember = false val i = enumCases.iterator() while (i.hasNext()) { i.next().emit(codeWriter) @@ -115,13 +112,11 @@ class TypeSpec private constructor( propertySpec.emit(codeWriter, kind.implicitPropertyModifiers) codeWriter.emit("\n") } - firstMember = false } // Constructors. val constructors = funSpecs.filter { it.isConstructor } if (constructors.isNotEmpty()) { - firstMember = false constructors.forEachIndexed { index, funSpec -> codeWriter.emit("\n") funSpec.emit(codeWriter, name, kind.implicitFunctionModifiers) @@ -132,7 +127,6 @@ class TypeSpec private constructor( // Functions. val functions = funSpecs.filterNot { it.isConstructor } if (functions.isNotEmpty()) { - firstMember = false functions.forEachIndexed { index, funSpec -> codeWriter.emit("\n") funSpec.emit(codeWriter, name, kind.implicitFunctionModifiers) @@ -142,7 +136,6 @@ class TypeSpec private constructor( // Types. if (typeSpecs.isNotEmpty()) { - firstMember = false typeSpecs.forEach { typeSpec -> codeWriter.emit("\n") typeSpec.emit(codeWriter) @@ -152,11 +145,7 @@ class TypeSpec private constructor( codeWriter.unindent() codeWriter.popType() - if (!firstMember) { - codeWriter.emit("\n") - } - - codeWriter.emit("}\n") + codeWriter.emit("\n}\n") } finally { codeWriter.statementLine = previousStatementLine }