Skip to content

Commit d0e7707

Browse files
committed
Make handling of whitespace in reflowed lines consistent
We always want it at the beginning of lines, not at the end. This fixes an interaction with the logic for preserving existing line breaks at non-whitespace boundaries. MOE_MIGRATED_REVID=241641348
1 parent 2d65366 commit d0e7707

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,36 +161,40 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
161161
private static ImmutableList<String> stringComponents(
162162
String input, JCTree.JCCompilationUnit unit, List<Tree> flat) {
163163
ImmutableList.Builder<String> result = ImmutableList.builder();
164+
StringBuilder piece = new StringBuilder();
164165
for (Tree tree : flat) {
165166
// adjust for leading and trailing double quotes
166167
String text = input.substring(getStartPosition(tree) + 1, getEndPosition(unit, tree) - 1);
167168
int start = 0;
168169
for (int idx = 0; idx < text.length(); idx++) {
169170
if (CharMatcher.whitespace().matches(text.charAt(idx))) {
170-
result.add(text.substring(start, idx));
171-
start = idx;
172-
continue;
173-
}
174-
if (hasEscapedWhitespaceAt(text, idx) != -1) {
175-
result.add(text.substring(start, idx));
176-
start = idx;
177-
continue;
178-
}
179-
180-
if (hasEscapedNewlineAt(text, idx) != -1) {
171+
// continue below
172+
} else if (hasEscapedWhitespaceAt(text, idx) != -1) {
173+
// continue below
174+
} else if (hasEscapedNewlineAt(text, idx) != -1) {
181175
int length;
182176
while ((length = hasEscapedNewlineAt(text, idx)) != -1) {
183177
idx += length;
184178
}
185-
result.add(text.substring(start, idx));
186-
start = idx;
179+
} else {
187180
continue;
188181
}
182+
piece.append(text, start, idx);
183+
result.add(piece.toString());
184+
piece = new StringBuilder();
185+
start = idx;
186+
}
187+
if (piece.length() > 0) {
188+
result.add(piece.toString());
189+
piece = new StringBuilder();
189190
}
190191
if (start < text.length()) {
191-
result.add(text.substring(start));
192+
piece.append(text, start, text.length());
192193
}
193194
}
195+
if (piece.length() > 0) {
196+
result.add(piece.toString());
197+
}
194198
return result.build();
195199
}
196200

core/src/test/java/com/google/googlejavaformat/java/StringWrapperIntegrationTest.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,16 @@ public static Collection<Object[]> parameters() {
139139
{
140140
{
141141
"class T {", //
142-
" String s = \"onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto interrupt\";",
142+
" String s ="
143+
+ " \"onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto"
144+
+ " interrupt\";",
143145
"}"
144146
},
145147
{
146148
"class T {",
147149
" String s =",
148-
" \"onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto\"",
150+
" "
151+
+ " \"onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto\"",
149152
" + \" interrupt\";",
150153
"}",
151154
}
@@ -272,7 +275,8 @@ public static Collection<Object[]> parameters() {
272275
{
273276
"class T {",
274277
" String s =",
275-
" \"__ onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto\"",
278+
" \"__"
279+
+ " onelongincrediblyunbrokensentencemovingfromtopictotopicsothatnoonehadachanceto\"",
276280
" + \" interrupt\";",
277281
"}",
278282
}
@@ -306,6 +310,44 @@ public static Collection<Object[]> parameters() {
306310
"}",
307311
}
308312
},
313+
{
314+
{
315+
"class T {", //
316+
" String s =",
317+
" \"aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccc"
318+
+ " dddddddddddddddddd\";",
319+
"}"
320+
},
321+
{
322+
"class T {",
323+
" String s =",
324+
" \"aaaaaaaaaaaaaaaaaaaaaaaa\"",
325+
" + \" bbbbbbbbbbbbbbbbbb\"",
326+
" + \" ccccccccccccccccccccccccccc\"",
327+
" + \" dddddddddddddddddd\";",
328+
"}",
329+
}
330+
},
331+
{
332+
{
333+
"class T {", //
334+
" String s =",
335+
" \"aaaaaaaaaaaaaaaaaaaaaaaa \"",
336+
" + \"bbbbbbbbbbbbbbbbbb \"",
337+
" + \"ccccccccccccccccccccccccccc \"",
338+
" + \"dddddddddddddddddd\";",
339+
"}"
340+
},
341+
{
342+
"class T {",
343+
" String s =",
344+
" \"aaaaaaaaaaaaaaaaaaaaaaaa\"",
345+
" + \" bbbbbbbbbbbbbbbbbb\"",
346+
" + \" ccccccccccccccccccccccccccc\"",
347+
" + \" dddddddddddddddddd\";",
348+
"}",
349+
}
350+
},
309351
};
310352
return Arrays.stream(inputsAndOutputs)
311353
.map(

0 commit comments

Comments
 (0)