diff --git a/smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java b/smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java
index 028a6edc2af..0426853294d 100644
--- a/smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java
+++ b/smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java
@@ -545,7 +545,7 @@ public String toString() {
}
if (result.isEmpty()) {
- return trailingNewline ? String.valueOf(currentState.newline) : "";
+ return trailingNewline ? currentState.newline : "";
}
// This accounts for cases where the only write on the CodeWriter was
@@ -556,10 +556,10 @@ public String toString() {
if (trailingNewline) {
// Add a trailing newline if needed.
- return result.charAt(result.length() - 1) != currentState.newline ? result + currentState.newline : result;
- } else if (result.charAt(result.length() - 1) == currentState.newline) {
+ return result.endsWith(currentState.newline) ? result : result + currentState.newline;
+ } else if (result.endsWith(currentState.newline)) {
// Strip the trailing newline if present.
- return result.substring(0, result.length() - 1);
+ return result.substring(0, result.length() - currentState.newline.length());
} else {
return result;
}
@@ -663,8 +663,9 @@ public CodeWriter popState() {
// and not written to the builder of the parent state. This ensures that
// inline sections are captured inside of strings and then later written
// back into a parent state.
- popped.builder.setLength(0);
- popped.builder.append(result);
+ StringBuilder builder = popped.getBuilder();
+ builder.setLength(0);
+ builder.append(result);
} else if (!result.isEmpty()) {
// Sections can be added that are just placeholders. In those cases,
// do not write anything unless the section emitted a non-empty string.
@@ -677,16 +678,12 @@ public CodeWriter popState() {
}
private String getTrimmedPoppedStateContents(State state) {
- StringBuilder builder = state.builder;
- String result = "";
+ String result = state.toString();
// Remove the trailing newline, if present, since it gets added in the
// final call to writeOptional.
- if (builder != null && builder.length() > 0) {
- if (builder.charAt(builder.length() - 1) == currentState.newline) {
- builder.delete(builder.length() - 1, builder.length());
- }
- result = builder.toString();
+ if (result.endsWith(currentState.newline)) {
+ result = result.substring(0, result.length() - currentState.newline.length());
}
return result;
@@ -823,9 +820,8 @@ public CodeWriter enableNewlines() {
* {@link #disableNewlines()}, and does not actually change the newline
* character of the current state.
*
- *
When the provided string is not empty, then the string must contain
- * exactly one character. Setting the newline character to a non-empty
- * string also implicitly enables newlines in the current state.
+ *
Setting the newline character to a non-empty string implicitly
+ * enables newlines in the current state.
*
* @param newline Newline character to use.
* @return Returns the CodeWriter.
@@ -833,10 +829,9 @@ public CodeWriter enableNewlines() {
public final CodeWriter setNewline(String newline) {
if (newline.isEmpty()) {
return disableNewlines();
- } else if (newline.length() > 1) {
- throw new IllegalArgumentException("newline must be set to an empty string or a single character");
} else {
- return setNewline(newline.charAt(0));
+ currentState.newline = newline;
+ return enableNewlines();
}
}
@@ -851,9 +846,7 @@ public final CodeWriter setNewline(String newline) {
* @return Returns the CodeWriter.
*/
public final CodeWriter setNewline(char newline) {
- currentState.newline = newline;
- enableNewlines();
- return this;
+ return setNewline(String.valueOf(newline));
}
/**
@@ -1350,7 +1343,7 @@ private final class State {
private int indentation;
private boolean trimTrailingSpaces;
private boolean disableNewline;
- private char newline = '\n';
+ private String newline = "\n";
private char expressionStart = '$';
private transient String sectionName;
@@ -1422,41 +1415,62 @@ void putInterceptor(String section, Consumer