Skip to content

Commit

Permalink
Remove trailing tabs from comments to make behavior idempotent.
Browse files Browse the repository at this point in the history
Fixes #422, fixes #423

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=288364171
  • Loading branch information
Eckaaaaaat authored and netdpb committed Jan 7, 2020
1 parent 1288c85 commit 9016086
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
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");
}
}

0 comments on commit 9016086

Please sign in to comment.