Skip to content

Commit afe8ac5

Browse files
committed
Support windows line endings in string reflowing
MOE_MIGRATED_REVID=241798204
1 parent d0e7707 commit afe8ac5

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static String wrap(final int columnLimit, final String input) throws FormatterEx
7575
}
7676

7777
JCTree.JCCompilationUnit unit = parse(input, /* allowStringFolding= */ false);
78+
String separator = Newlines.guessLineSeparator(input);
7879

7980
// Paths to string literals that extend past the column limit.
8081
List<TreePath> toFix = new ArrayList<>();
@@ -133,7 +134,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
133134
ImmutableList<String> components = stringComponents(input, unit, flat);
134135
replacements.put(
135136
Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(unit, getLast(flat))),
136-
reflow(columnLimit, startColumn, trailing, components, first.get()));
137+
reflow(separator, columnLimit, startColumn, trailing, components, first.get()));
137138
}
138139
String result = applyReplacements(input, replacements);
139140

@@ -207,7 +208,7 @@ static int hasEscapedWhitespaceAt(String input, int idx) {
207208
}
208209

209210
static int hasEscapedNewlineAt(String input, int idx) {
210-
return Stream.of("\\n\\r", "\\r", "\\n")
211+
return Stream.of("\\r\\n", "\\r", "\\n")
211212
.mapToInt(x -> input.startsWith(x, idx) ? x.length() : -1)
212213
.filter(x -> x != -1)
213214
.findFirst()
@@ -217,13 +218,15 @@ static int hasEscapedNewlineAt(String input, int idx) {
217218
/**
218219
* Reflows the given source text, trying to split on word boundaries.
219220
*
221+
* @param separator the line separator
220222
* @param columnLimit the number of columns to wrap at
221223
* @param startColumn the column position of the beginning of the original text
222224
* @param trailing extra space to leave after the last line
223225
* @param components the text to reflow
224226
* @param first0 true if the text includes the beginning of its enclosing concat chain, i.e. a
225227
*/
226228
private static String reflow(
229+
String separator,
227230
int columnLimit,
228231
int startColumn,
229232
int trailing,
@@ -263,7 +266,7 @@ private static String reflow(
263266
return lines.stream()
264267
.collect(
265268
joining(
266-
"\"\n" + Strings.repeat(" ", startColumn + (first0 ? 4 : -2)) + "+ \"",
269+
"\"" + separator + Strings.repeat(" ", startColumn + (first0 ? 4 : -2)) + "+ \"",
267270
"\"",
268271
"\""));
269272
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public static Collection<Object[]> parameters() {
190190
"class T {", //
191191
" String s = \"\\n\\n\\none\\n\\nlong\\n\\nincredibly\\n\\nunbroken\\n\\nsentence\\n\\n"
192192
+ "moving\\n\\nfrom\\n\\n topic\\n\\nto\\n\\n topic\\n\\nso\\n\\nthat\\n\\nno-one"
193-
+ "\\n\\nhad\\n\\na\\n\\nchance\\n\\nto\\n\\ninterrupt\\n\\n\\n\";",
193+
+ "\\n\\nhad\\n\\na\\n\\nchance\\n\\nto\\n\\ninterrupt\\n\\n\";",
194194
"}"
195195
},
196196
{
@@ -214,7 +214,7 @@ public static Collection<Object[]> parameters() {
214214
" + \"a\\n\\n\"",
215215
" + \"chance\\n\\n\"",
216216
" + \"to\\n\\n\"",
217-
" + \"interrupt\\n\\n\\n\";",
217+
" + \"interrupt\\n\\n\";",
218218
"}",
219219
},
220220
},
@@ -374,6 +374,18 @@ public void test() throws Exception {
374374
assertThat(StringWrapper.wrap(40, new Formatter().formatSource(input))).isEqualTo(output);
375375
}
376376

377+
@Test
378+
public void testCR() throws Exception {
379+
assertThat(StringWrapper.wrap(40, new Formatter().formatSource(input.replace("\n", "\r"))))
380+
.isEqualTo(output.replace("\n", "\r"));
381+
}
382+
383+
@Test
384+
public void testCRLF() throws Exception {
385+
assertThat(StringWrapper.wrap(40, new Formatter().formatSource(input.replace("\n", "\r\n"))))
386+
.isEqualTo(output.replace("\n", "\r\n"));
387+
}
388+
377389
@Test
378390
public void idempotent() throws Exception {
379391
String wrap = StringWrapper.wrap(40, new Formatter().formatSource(input));

0 commit comments

Comments
 (0)