Skip to content

Commit 05f57ec

Browse files
Eckaaaaaatnetdpb
authored andcommitted
Remove trailing tabs from comments to make behavior idempotent.
Fixes #422, fixes #423 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=288364171
1 parent 1288c85 commit 05f57ec

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.base.CharMatcher;
2020
import com.google.common.base.MoreObjects;
21+
import com.google.common.base.Strings;
2122
import com.google.common.collect.DiscreteDomain;
2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.collect.Range;
@@ -55,9 +56,9 @@ public final class JavaOutput extends Output {
5556
private final int kN; // The number of tokens or comments in the input, excluding the EOF.
5657
private int iLine = 0; // Closest corresponding line number on input.
5758
private int lastK = -1; // Last {@link Tok} index output.
58-
private int spacesPending = 0;
5959
private int newlinesPending = 0;
6060
private StringBuilder lineBuilder = new StringBuilder();
61+
private StringBuilder spacesPending = new StringBuilder();
6162

6263
/**
6364
* {@code JavaOutput} constructor.
@@ -121,23 +122,26 @@ public void append(String text, Range<Integer> range) {
121122
if (newlinesPending == 0) {
122123
++newlinesPending;
123124
}
124-
spacesPending = 0;
125+
spacesPending = new StringBuilder();
125126
} else {
126127
boolean rangesSet = false;
127128
int textN = text.length();
128129
for (int i = 0; i < textN; i++) {
129130
char c = text.charAt(i);
130131
switch (c) {
131132
case ' ':
132-
++spacesPending;
133+
spacesPending.append(' ');
134+
break;
135+
case '\t':
136+
spacesPending.append('\t');
133137
break;
134138
case '\r':
135139
if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
136140
i++;
137141
}
138142
// falls through
139143
case '\n':
140-
spacesPending = 0;
144+
spacesPending = new StringBuilder();
141145
++newlinesPending;
142146
break;
143147
default:
@@ -150,9 +154,9 @@ public void append(String text, Range<Integer> range) {
150154
rangesSet = false;
151155
--newlinesPending;
152156
}
153-
while (spacesPending > 0) {
154-
lineBuilder.append(' ');
155-
--spacesPending;
157+
if (spacesPending.length() > 0) {
158+
lineBuilder.append(spacesPending);
159+
spacesPending = new StringBuilder();
156160
}
157161
lineBuilder.append(c);
158162
if (!range.isEmpty()) {
@@ -174,7 +178,7 @@ public void append(String text, Range<Integer> range) {
174178

175179
@Override
176180
public void indent(int indent) {
177-
spacesPending = indent;
181+
spacesPending.append(Strings.repeat(" ", indent));
178182
}
179183

180184
/** Flush any incomplete last line, then add the EOF token into our data structures. */
@@ -387,7 +391,7 @@ public String toString() {
387391
return MoreObjects.toStringHelper(this)
388392
.add("iLine", iLine)
389393
.add("lastK", lastK)
390-
.add("spacesPending", spacesPending)
394+
.add("spacesPending", spacesPending.toString().replace("\t", "\\t"))
391395
.add("newlinesPending", newlinesPending)
392396
.add("blankLines", blankLines)
393397
.add("super", super.toString())

‎core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,24 @@ public void dontWrapMoeLineComments() throws Exception {
472472
+ " chance to interrupt;\n"
473473
+ "}\n");
474474
}
475+
476+
@Test
477+
public void removeTrailingTabsInComments() throws Exception {
478+
assertThat(
479+
new Formatter()
480+
.formatSource(
481+
"class Foo {\n"
482+
+ " void f() {\n"
483+
+ " int x = 0; // comment\t\t\t\n"
484+
+ " return;\n"
485+
+ " }\n"
486+
+ "}\n"))
487+
.isEqualTo(
488+
"class Foo {\n"
489+
+ " void f() {\n"
490+
+ " int x = 0; // comment\n"
491+
+ " return;\n"
492+
+ " }\n"
493+
+ "}\n");
494+
}
475495
}

0 commit comments

Comments
 (0)