Skip to content
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 @@ -69,7 +69,7 @@ public int tabWidth() {
/** Returns the indentation string for the given visual column width. */
public String indentString(int indent) {
if (!useTabs()) {
return " ".repeat(indent);
return JavaOutput.spaces(indent);
}
int tabWidth = tabWidth();
return "\t".repeat(indent / tabWidth) + " ".repeat(indent % tabWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,32 @@ public final class JavaOutput extends Output {
private StringBuilder lineBuilder = new StringBuilder();
private StringBuilder spacesPending = new StringBuilder();

private static final int MAX_CACHED_SPACES = 100;
private static final String[] spaces = new String[MAX_CACHED_SPACES + 1];

/** Returns an indentation string of the given number of spaces. */
static String spaces(int indent) {
if (indent <= 0) {
return "";
}
if (indent <= MAX_CACHED_SPACES) {
String result = spaces[indent];
if (result == null) {
result = spaces[indent] = " ".repeat(indent);
}
return result;
}
return " ".repeat(indent);
}

/**
* {@code JavaOutput} constructor.
*
* @param javaInput the {@link Input}, used to match up blank lines in the output
* @param commentsHelper the {@link CommentsHelper}, used to rewrite comments
*/
public JavaOutput(String lineSeparator, Input javaInput, CommentsHelper commentsHelper) {
this(lineSeparator, javaInput, commentsHelper, indent -> " ".repeat(Math.max(0, indent)));
this(lineSeparator, javaInput, commentsHelper, JavaOutput::spaces);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,12 @@ public void styleVisualLengthAndIndentString() {
assertThat(google.indentString(0)).isEmpty();
assertThat(googleTabs.indentString(0)).isEmpty();
assertThat(aospTabs.indentString(0)).isEmpty();
assertThat(google.indentString(4)).isSameInstanceAs(google.indentString(4));
assertThat(google.indentString(104)).isEqualTo(" ".repeat(104));
assertThat(googleTabs.indentString(104)).isEqualTo("\t".repeat(52));
assertThat(JavaOutput.spaces(4)).isSameInstanceAs(JavaOutput.spaces(4));
assertThat(JavaOutput.spaces(0)).isEmpty();
assertThat(JavaOutput.spaces(104)).isEqualTo(" ".repeat(104));
}

@Test
Expand Down
Loading