Skip to content

Commit

Permalink
Add flag control for javadoc formatting
Browse files Browse the repository at this point in the history
Fixes #139, #149

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=279811849
  • Loading branch information
cushon authored and cpovirk committed Nov 12, 2019
1 parent 1396e60 commit bd17670
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class CommandLineOptions {
private final boolean setExitIfChanged;
private final Optional<String> assumeFilename;
private final boolean reflowLongStrings;
private final boolean formatJavadoc;

CommandLineOptions(
ImmutableList<String> files,
Expand All @@ -59,7 +60,8 @@ final class CommandLineOptions {
boolean dryRun,
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings) {
boolean reflowLongStrings,
boolean formatJavadoc) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
Expand All @@ -76,6 +78,7 @@ final class CommandLineOptions {
this.setExitIfChanged = setExitIfChanged;
this.assumeFilename = assumeFilename;
this.reflowLongStrings = reflowLongStrings;
this.formatJavadoc = formatJavadoc;
}

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

boolean formatJavadoc() {
return formatJavadoc;
}

static Builder builder() {
return new Builder();
}
Expand All @@ -186,6 +193,7 @@ static class Builder {
private boolean setExitIfChanged = false;
private Optional<String> assumeFilename = Optional.empty();
private boolean reflowLongStrings = true;
private boolean formatJavadoc = true;

ImmutableList.Builder<String> filesBuilder() {
return files;
Expand Down Expand Up @@ -265,6 +273,11 @@ Builder reflowLongStrings(boolean reflowLongStrings) {
return this;
}

Builder formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
return this;
}

CommandLineOptions build() {
return new CommandLineOptions(
files.build(),
Expand All @@ -282,7 +295,8 @@ CommandLineOptions build() {
dryRun,
setExitIfChanged,
assumeFilename,
reflowLongStrings);
reflowLongStrings,
formatJavadoc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ static CommandLineOptions parse(Iterable<String> options) {
case "--skip-reflowing-long-strings":
optionsBuilder.reflowLongStrings(false);
break;
case "--skip-javadoc-formatting":
optionsBuilder.formatJavadoc(false);
break;
case "-":
optionsBuilder.stdin(true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
public final class JavaCommentsHelper implements CommentsHelper {

private final String lineSeparator;
private final JavaFormatterOptions options;

public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
this.lineSeparator = lineSeparator;
this.options = options;
}

@Override
Expand All @@ -41,7 +43,7 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
return tok.getOriginalText();
}
String text = tok.getOriginalText();
if (tok.isJavadocComment()) {
if (tok.isJavadocComment() && options.formatJavadoc()) {
text = JavadocFormatter.formatJavadoc(text, column0);
}
List<String> lines = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ int indentationMultiplier() {
}

private final Style style;
private final boolean formatJavadoc;

private JavaFormatterOptions(Style style) {
private JavaFormatterOptions(Style style, boolean formatJavadoc) {
this.style = style;
this.formatJavadoc = formatJavadoc;
}

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

boolean formatJavadoc() {
return formatJavadoc;
}

/** Returns the code style. */
public Style style() {
return style;
Expand All @@ -76,6 +82,7 @@ public static Builder builder() {
/** A builder for {@link JavaFormatterOptions}. */
public static class Builder {
private Style style = Style.GOOGLE;
private boolean formatJavadoc = true;

private Builder() {}

Expand All @@ -84,8 +91,13 @@ public Builder style(Style style) {
return this;
}

Builder formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
return this;
}

public JavaFormatterOptions build() {
return new JavaFormatterOptions(style);
return new JavaFormatterOptions(style, formatJavadoc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public int format(String... args) throws UsageException {
}

JavaFormatterOptions options =
JavaFormatterOptions.builder().style(parameters.aosp() ? Style.AOSP : Style.GOOGLE).build();
JavaFormatterOptions.builder()
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
.formatJavadoc(parameters.formatJavadoc())
.build();

if (parameters.stdin()) {
return formatStdin(parameters, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ final class UsageException extends Exception {
" Do not remove unused imports. Imports will still be sorted.",
" . --skip-reflowing-long-strings",
" Do not reflow string literals that exceed the column limit.",
" . --skip-javadoc-formatting",
" Do not reformat javadoc.",
" --dry-run, -n",
" Prints the paths of the files whose contents would change if the formatter were run"
+ " normally.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void defaults() {
assertThat(options.dryRun()).isFalse();
assertThat(options.setExitIfChanged()).isFalse();
assertThat(options.reflowLongStrings()).isTrue();
assertThat(options.formatJavadoc()).isTrue();
}

@Test
Expand Down Expand Up @@ -195,4 +196,12 @@ public void skipReflowLongStrings() {
.reflowLongStrings())
.isFalse();
}

@Test
public void skipJavadocFormatting() {
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("--skip-javadoc-formatting"))
.formatJavadoc())
.isFalse();
}
}
30 changes: 30 additions & 0 deletions core/src/test/java/com/google/googlejavaformat/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,34 @@ public void noReflowLongStrings() throws Exception {
assertThat(main.format("--skip-reflowing-long-strings", "-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(joiner.join(expected));
}

@Test
public void noFormatJavadoc() throws Exception {
String[] input = {
"/**",
" * graph",
" *",
" * graph",
" *",
" * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
+ " eiusmod tempor incididunt ut labore et dolore magna aliqua",
" */",
"class Test {",
" /**",
" * creates entropy",
" */",
" public static void main(String... args) {}",
"}",
"",
};
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(joiner.join(input));
}
}

0 comments on commit bd17670

Please sign in to comment.