Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure code writer always has a StringBuilder #1128

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,6 @@ private final class State {
private CopyOnWriteRef<CodeWriterFormatterContainer> formatters;
private CopyOnWriteRef<CodeInterceptorContainer<T>> interceptors;

/** This StringBuilder, if null, will only be created lazily when needed. */
private StringBuilder builder;

/**
Expand All @@ -1777,6 +1776,7 @@ private final class State {
private boolean isInline;

State() {
builder = new StringBuilder();
isRoot = true;
CodeWriterFormatterContainer formatterContainer = new CodeWriterFormatterContainer();
DEFAULT_FORMATTERS.forEach(formatterContainer::putFormatter);
Expand Down Expand Up @@ -1810,13 +1810,10 @@ private void copyStateFrom(State copy) {

@Override
public String toString() {
return builder == null ? "" : builder.toString();
return getBuilder().toString();
}

StringBuilder getBuilder() {
if (builder == null) {
builder = new StringBuilder();
}
return builder;
}

Expand Down Expand Up @@ -1921,7 +1918,7 @@ private void makeInterceptableCodeSection(CodeSection section) {
// level is reset back to the root, and the newline prefix is removed.
// Indentation and prefixes are added automatically if/when the
// captured text is written into the parent state.
currentState.builder = null;
currentState.builder = new StringBuilder();
currentState.newlinePrefix = "";
dedent(-1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,22 @@ public void ensuresNewlineIsPresent() {

assertThat(writer.toString(), equalTo("Foo\nBar\nBaz\nBam\n"));
}

// Dynamically creating builders for a section doesn't work when pushing
// sections into captured sections that haven't written anything yet.
// It ends up swallowing the text written to sub-sections because the
// top-level section's builder wasn't written yet. This test makes sure
// that doesn't happen.
@Test
public void alwaysWritesToParentBuilders() {
SimpleCodeWriter writer = new SimpleCodeWriter();

writer.pushState("Hi");
writer.pushState();
writer.write("Hello");
writer.popState();
writer.popState();

assertThat(writer.toString(), equalTo("Hello\n"));
}
}