Skip to content

Commit

Permalink
Improve readability of generated Swift by fixing issues with new line…
Browse files Browse the repository at this point in the history
…s in SwiftPoet.
  • Loading branch information
FilipDolnik committed Nov 10, 2023
1 parent 73cf506 commit ddca706
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -242,7 +249,7 @@ internal class CodeWriter(
statementLine = -1
}

"%W" -> out.wrappingSpace(indentLevel + 2)
"%W" -> emitWrappingSpace()

else -> emit(part)
}
Expand All @@ -251,6 +258,7 @@ internal class CodeWriter(

fun emitWrappingSpace() = apply {
out.wrappingSpace(indentLevel + 2)
trailingNewline = false
}

private fun emitLiteral(o: Any?) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -403,7 +411,7 @@ internal class CodeWriter(
}

out.append(line)
trailingNewline = false
trailingNewline = line.trimEnd { it.isWhitespace() && it != '\n' }.endsWith('\n')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ class FileSpec private constructor(
}
}

internal const val DEFAULT_INDENT = " "
internal const val DEFAULT_INDENT = " "
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,12 @@ internal fun List<ParameterSpec>.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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -142,7 +136,6 @@ class TypeSpec private constructor(

// Types.
if (typeSpecs.isNotEmpty()) {
firstMember = false
typeSpecs.forEach { typeSpec ->
codeWriter.emit("\n")
typeSpec.emit(codeWriter)
Expand All @@ -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
}
Expand Down

0 comments on commit ddca706

Please sign in to comment.