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

MOE Sync 2020-01-07 #432

Merged
merged 1 commit into from
Jan 7, 2020
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 @@ -18,6 +18,7 @@

import com.google.common.base.CharMatcher;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
Expand Down Expand Up @@ -55,9 +56,9 @@ public final class JavaOutput extends Output {
private final int kN; // The number of tokens or comments in the input, excluding the EOF.
private int iLine = 0; // Closest corresponding line number on input.
private int lastK = -1; // Last {@link Tok} index output.
private int spacesPending = 0;
private int newlinesPending = 0;
private StringBuilder lineBuilder = new StringBuilder();
private StringBuilder spacesPending = new StringBuilder();

/**
* {@code JavaOutput} constructor.
Expand Down Expand Up @@ -121,23 +122,26 @@ public void append(String text, Range<Integer> range) {
if (newlinesPending == 0) {
++newlinesPending;
}
spacesPending = 0;
spacesPending = new StringBuilder();
} else {
boolean rangesSet = false;
int textN = text.length();
for (int i = 0; i < textN; i++) {
char c = text.charAt(i);
switch (c) {
case ' ':
++spacesPending;
spacesPending.append(' ');
break;
case '\t':
spacesPending.append('\t');
break;
case '\r':
if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
i++;
}
// falls through
case '\n':
spacesPending = 0;
spacesPending = new StringBuilder();
++newlinesPending;
break;
default:
Expand All @@ -150,9 +154,9 @@ public void append(String text, Range<Integer> range) {
rangesSet = false;
--newlinesPending;
}
while (spacesPending > 0) {
lineBuilder.append(' ');
--spacesPending;
if (spacesPending.length() > 0) {
lineBuilder.append(spacesPending);
spacesPending = new StringBuilder();
}
lineBuilder.append(c);
if (!range.isEmpty()) {
Expand All @@ -174,7 +178,7 @@ public void append(String text, Range<Integer> range) {

@Override
public void indent(int indent) {
spacesPending = indent;
spacesPending.append(Strings.repeat(" ", indent));
}

/** Flush any incomplete last line, then add the EOF token into our data structures. */
Expand Down Expand Up @@ -387,7 +391,7 @@ public String toString() {
return MoreObjects.toStringHelper(this)
.add("iLine", iLine)
.add("lastK", lastK)
.add("spacesPending", spacesPending)
.add("spacesPending", spacesPending.toString().replace("\t", "\\t"))
.add("newlinesPending", newlinesPending)
.add("blankLines", blankLines)
.add("super", super.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,24 @@ public void dontWrapMoeLineComments() throws Exception {
+ " chance to interrupt;\n"
+ "}\n");
}

@Test
public void removeTrailingTabsInComments() throws Exception {
assertThat(
new Formatter()
.formatSource(
"class Foo {\n"
+ " void f() {\n"
+ " int x = 0; // comment\t\t\t\n"
+ " return;\n"
+ " }\n"
+ "}\n"))
.isEqualTo(
"class Foo {\n"
+ " void f() {\n"
+ " int x = 0; // comment\n"
+ " return;\n"
+ " }\n"
+ "}\n");
}
}