Skip to content

Commit 6b57fbf

Browse files
committed
Reflow string literals that exceed the column limit
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237903057
1 parent f8d4e8d commit 6b57fbf

File tree

9 files changed

+840
-3
lines changed

9 files changed

+840
-3
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ final class CommandLineOptions {
4141
private final boolean dryRun;
4242
private final boolean setExitIfChanged;
4343
private final Optional<String> assumeFilename;
44+
private final boolean reflowLongStrings;
4445

4546
CommandLineOptions(
4647
ImmutableList<String> files,
@@ -57,7 +58,8 @@ final class CommandLineOptions {
5758
boolean removeUnusedImports,
5859
boolean dryRun,
5960
boolean setExitIfChanged,
60-
Optional<String> assumeFilename) {
61+
Optional<String> assumeFilename,
62+
boolean reflowLongStrings) {
6163
this.files = files;
6264
this.inPlace = inPlace;
6365
this.lines = lines;
@@ -73,6 +75,7 @@ final class CommandLineOptions {
7375
this.dryRun = dryRun;
7476
this.setExitIfChanged = setExitIfChanged;
7577
this.assumeFilename = assumeFilename;
78+
this.reflowLongStrings = reflowLongStrings;
7679
}
7780

7881
/** The files to format. */
@@ -152,6 +155,10 @@ Optional<String> assumeFilename() {
152155
return assumeFilename;
153156
}
154157

158+
boolean reflowLongStrings() {
159+
return reflowLongStrings;
160+
}
161+
155162
/** Returns true if partial formatting was selected. */
156163
boolean isSelection() {
157164
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
@@ -178,6 +185,7 @@ static class Builder {
178185
private boolean dryRun = false;
179186
private boolean setExitIfChanged = false;
180187
private Optional<String> assumeFilename = Optional.empty();
188+
private boolean reflowLongStrings = true;
181189

182190
ImmutableList.Builder<String> filesBuilder() {
183191
return files;
@@ -252,6 +260,11 @@ Builder assumeFilename(String assumeFilename) {
252260
return this;
253261
}
254262

263+
Builder reflowLongStrings(boolean reflowLongStrings) {
264+
this.reflowLongStrings = reflowLongStrings;
265+
return this;
266+
}
267+
255268
CommandLineOptions build() {
256269
return new CommandLineOptions(
257270
files.build(),
@@ -268,7 +281,8 @@ CommandLineOptions build() {
268281
removeUnusedImports,
269282
dryRun,
270283
setExitIfChanged,
271-
assumeFilename);
284+
assumeFilename,
285+
reflowLongStrings);
272286
}
273287
}
274288
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ static CommandLineOptions parse(Iterable<String> options) {
105105
case "--skip-removing-unused-imports":
106106
optionsBuilder.removeUnusedImports(false);
107107
break;
108+
case "--skip-reflowing-long-strings":
109+
optionsBuilder.reflowLongStrings(false);
110+
break;
108111
case "-":
109112
optionsBuilder.stdin(true);
110113
break;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public String call() throws FormatterException {
4444
String formatted =
4545
new Formatter(options).formatSource(input, characterRanges(input).asRanges());
4646
formatted = fixImports(formatted);
47+
if (parameters.reflowLongStrings()) {
48+
formatted = StringWrapper.wrap(options.maxLineLength(), formatted);
49+
}
4750
return formatted;
4851
}
4952

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ public String formatSource(String input) throws FormatterException {
217217
public String formatSourceAndFixImports(String input) throws FormatterException {
218218
input = ImportOrderer.reorderImports(input);
219219
input = RemoveUnusedImports.removeUnusedImports(input);
220-
return formatSource(input);
220+
String formatted = formatSource(input);
221+
formatted = StringWrapper.wrap(formatted);
222+
return formatted;
221223
}
222224

223225
/**

0 commit comments

Comments
 (0)