Skip to content

Commit 8df44e5

Browse files
cushoncpovirk
authored andcommitted
Add flag control for javadoc formatting
Fixes #139, #149 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=279811849
1 parent 1396e60 commit 8df44e5

8 files changed

Lines changed: 81 additions & 6 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ final class CommandLineOptions {
4242
private final boolean setExitIfChanged;
4343
private final Optional<String> assumeFilename;
4444
private final boolean reflowLongStrings;
45+
private final boolean formatJavadoc;
4546

4647
CommandLineOptions(
4748
ImmutableList<String> files,
@@ -59,7 +60,8 @@ final class CommandLineOptions {
5960
boolean dryRun,
6061
boolean setExitIfChanged,
6162
Optional<String> assumeFilename,
62-
boolean reflowLongStrings) {
63+
boolean reflowLongStrings,
64+
boolean formatJavadoc) {
6365
this.files = files;
6466
this.inPlace = inPlace;
6567
this.lines = lines;
@@ -76,6 +78,7 @@ final class CommandLineOptions {
7678
this.setExitIfChanged = setExitIfChanged;
7779
this.assumeFilename = assumeFilename;
7880
this.reflowLongStrings = reflowLongStrings;
81+
this.formatJavadoc = formatJavadoc;
7982
}
8083

8184
/** The files to format. */
@@ -164,6 +167,10 @@ boolean isSelection() {
164167
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
165168
}
166169

170+
boolean formatJavadoc() {
171+
return formatJavadoc;
172+
}
173+
167174
static Builder builder() {
168175
return new Builder();
169176
}
@@ -186,6 +193,7 @@ static class Builder {
186193
private boolean setExitIfChanged = false;
187194
private Optional<String> assumeFilename = Optional.empty();
188195
private boolean reflowLongStrings = true;
196+
private boolean formatJavadoc = true;
189197

190198
ImmutableList.Builder<String> filesBuilder() {
191199
return files;
@@ -265,6 +273,11 @@ Builder reflowLongStrings(boolean reflowLongStrings) {
265273
return this;
266274
}
267275

276+
Builder formatJavadoc(boolean formatJavadoc) {
277+
this.formatJavadoc = formatJavadoc;
278+
return this;
279+
}
280+
268281
CommandLineOptions build() {
269282
return new CommandLineOptions(
270283
files.build(),
@@ -282,7 +295,8 @@ CommandLineOptions build() {
282295
dryRun,
283296
setExitIfChanged,
284297
assumeFilename,
285-
reflowLongStrings);
298+
reflowLongStrings,
299+
formatJavadoc);
286300
}
287301
}
288302
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ static CommandLineOptions parse(Iterable<String> options) {
108108
case "--skip-reflowing-long-strings":
109109
optionsBuilder.reflowLongStrings(false);
110110
break;
111+
case "--skip-javadoc-formatting":
112+
optionsBuilder.formatJavadoc(false);
113+
break;
111114
case "-":
112115
optionsBuilder.stdin(true);
113116
break;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
public final class JavaCommentsHelper implements CommentsHelper {
3131

3232
private final String lineSeparator;
33+
private final JavaFormatterOptions options;
3334

3435
public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
3536
this.lineSeparator = lineSeparator;
37+
this.options = options;
3638
}
3739

3840
@Override
@@ -41,7 +43,7 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
4143
return tok.getOriginalText();
4244
}
4345
String text = tok.getOriginalText();
44-
if (tok.isJavadocComment()) {
46+
if (tok.isJavadocComment() && options.formatJavadoc()) {
4547
text = JavadocFormatter.formatJavadoc(text, column0);
4648
}
4749
List<String> lines = new ArrayList<>();

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,22 @@ int indentationMultiplier() {
4848
}
4949

5050
private final Style style;
51+
private final boolean formatJavadoc;
5152

52-
private JavaFormatterOptions(Style style) {
53+
private JavaFormatterOptions(Style style, boolean formatJavadoc) {
5354
this.style = style;
55+
this.formatJavadoc = formatJavadoc;
5456
}
5557

5658
/** Returns the multiplier for the unit of indent. */
5759
public int indentationMultiplier() {
5860
return style.indentationMultiplier();
5961
}
6062

63+
boolean formatJavadoc() {
64+
return formatJavadoc;
65+
}
66+
6167
/** Returns the code style. */
6268
public Style style() {
6369
return style;
@@ -76,6 +82,7 @@ public static Builder builder() {
7682
/** A builder for {@link JavaFormatterOptions}. */
7783
public static class Builder {
7884
private Style style = Style.GOOGLE;
85+
private boolean formatJavadoc = true;
7986

8087
private Builder() {}
8188

@@ -84,8 +91,13 @@ public Builder style(Style style) {
8491
return this;
8592
}
8693

94+
Builder formatJavadoc(boolean formatJavadoc) {
95+
this.formatJavadoc = formatJavadoc;
96+
return this;
97+
}
98+
8799
public JavaFormatterOptions build() {
88-
return new JavaFormatterOptions(style);
100+
return new JavaFormatterOptions(style, formatJavadoc);
89101
}
90102
}
91103
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public int format(String... args) throws UsageException {
9696
}
9797

9898
JavaFormatterOptions options =
99-
JavaFormatterOptions.builder().style(parameters.aosp() ? Style.AOSP : Style.GOOGLE).build();
99+
JavaFormatterOptions.builder()
100+
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
101+
.formatJavadoc(parameters.formatJavadoc())
102+
.build();
100103

101104
if (parameters.stdin()) {
102105
return formatStdin(parameters, options);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ final class UsageException extends Exception {
4848
" Do not remove unused imports. Imports will still be sorted.",
4949
" . --skip-reflowing-long-strings",
5050
" Do not reflow string literals that exceed the column limit.",
51+
" . --skip-javadoc-formatting",
52+
" Do not reformat javadoc.",
5153
" --dry-run, -n",
5254
" Prints the paths of the files whose contents would change if the formatter were run"
5355
+ " normally.",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void defaults() {
5454
assertThat(options.dryRun()).isFalse();
5555
assertThat(options.setExitIfChanged()).isFalse();
5656
assertThat(options.reflowLongStrings()).isTrue();
57+
assertThat(options.formatJavadoc()).isTrue();
5758
}
5859

5960
@Test
@@ -195,4 +196,12 @@ public void skipReflowLongStrings() {
195196
.reflowLongStrings())
196197
.isFalse();
197198
}
199+
200+
@Test
201+
public void skipJavadocFormatting() {
202+
assertThat(
203+
CommandLineOptionsParser.parse(Arrays.asList("--skip-javadoc-formatting"))
204+
.formatJavadoc())
205+
.isFalse();
206+
}
198207
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,34 @@ public void noReflowLongStrings() throws Exception {
565565
assertThat(main.format("--skip-reflowing-long-strings", "-")).isEqualTo(0);
566566
assertThat(out.toString()).isEqualTo(joiner.join(expected));
567567
}
568+
569+
@Test
570+
public void noFormatJavadoc() throws Exception {
571+
String[] input = {
572+
"/**",
573+
" * graph",
574+
" *",
575+
" * graph",
576+
" *",
577+
" * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
578+
+ " eiusmod tempor incididunt ut labore et dolore magna aliqua",
579+
" */",
580+
"class Test {",
581+
" /**",
582+
" * creates entropy",
583+
" */",
584+
" public static void main(String... args) {}",
585+
"}",
586+
"",
587+
};
588+
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
589+
StringWriter out = new StringWriter();
590+
Main main =
591+
new Main(
592+
new PrintWriter(out, true),
593+
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
594+
in);
595+
assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0);
596+
assertThat(out.toString()).isEqualTo(joiner.join(input));
597+
}
568598
}

0 commit comments

Comments
 (0)