Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag for skipping javadoc formatting #149

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class CommandLineOptions {
private final boolean removeJavadocOnlyImports;
private final boolean sortImports;
private final boolean removeUnusedImports;
private final boolean skipJavadocFormatting;

CommandLineOptions(
ImmutableList<String> files,
Expand All @@ -52,7 +53,8 @@ final class CommandLineOptions {
boolean fixImportsOnly,
boolean removeJavadocOnlyImports,
boolean sortImports,
boolean removeUnusedImports) {
boolean removeUnusedImports,
boolean skipJavadocFormatting) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
Expand All @@ -66,6 +68,7 @@ final class CommandLineOptions {
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
this.sortImports = sortImports;
this.removeUnusedImports = removeUnusedImports;
this.skipJavadocFormatting = skipJavadocFormatting;
}

/** The files to format. */
Expand Down Expand Up @@ -136,6 +139,10 @@ boolean removeUnusedImports() {
return removeUnusedImports;
}

boolean skipJavadocFormatting() {
return skipJavadocFormatting;
}

/** Returns true if partial formatting was selected. */
boolean isSelection() {
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
Expand All @@ -160,6 +167,7 @@ static class Builder {
private Boolean removeJavadocOnlyImports = false;
private Boolean sortImports = true;
private Boolean removeUnusedImports = true;
private Boolean skipJavadocFormatting = false;

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

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

CommandLineOptions build() {
return new CommandLineOptions(
this.files.build(),
Expand All @@ -238,7 +251,8 @@ CommandLineOptions build() {
this.fixImportsOnly,
this.removeJavadocOnlyImports,
this.sortImports,
this.removeUnusedImports);
this.removeUnusedImports,
this.skipJavadocFormatting);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ static CommandLineOptions parse(Iterable<String> options) {
case "--skip-removing-unused-imports":
optionsBuilder.removeUnusedImports(false);
break;
case "--skip-javadoc-formatting":
optionsBuilder.skipJavadocFormatting(true);
break;
case "-":
optionsBuilder.stdin(true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
return tok.getOriginalText();
}
String text = tok.getOriginalText();
if (tok.isJavadocComment()) {
if (tok.isJavadocComment() && !options.skipJavadocFormatting()) {
text = JavadocFormatter.formatJavadoc(text, column0, options);
}
List<String> lines = new ArrayList<>();
Expand Down Expand Up @@ -162,4 +162,3 @@ private static boolean javadocShaped(List<String> lines) {
return true;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ int indentationMultiplier() {
}

private final Style style;
private final boolean skipJavadocFormatting;

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

/** Returns the maximum formatted width */
Expand All @@ -66,6 +68,11 @@ public int indentationMultiplier() {
return style.indentationMultiplier();
}

/** Returns if javadoc formatting should be skipped. */
public boolean skipJavadocFormatting() {
return skipJavadocFormatting;
}

/** Returns the default formatting options. */
public static JavaFormatterOptions defaultOptions() {
return builder().build();
Expand All @@ -79,6 +86,7 @@ public static Builder builder() {
/** A builder for {@link JavaFormatterOptions}. */
public static class Builder {
private Style style = Style.GOOGLE;
private boolean skipJavadocFormatting = false;

private Builder() {}

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

public Builder skipJavadocFormatting(boolean skipJavadocFormatting) {
this.skipJavadocFormatting = skipJavadocFormatting;
return this;
}

public JavaFormatterOptions build() {
return new JavaFormatterOptions(style);
return new JavaFormatterOptions(style, skipJavadocFormatting);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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)
.skipJavadocFormatting(parameters.skipJavadocFormatting())
.build();

if (parameters.stdin()) {
return formatStdin(parameters, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class UsageException extends Exception {
" Do not fix the import order. Unused imports will still be removed.",
" --skip-removing-unused-imports",
" Do not remove unused imports. Imports will still be sorted.",
" --skip-javadoc-formatting",
" Do not format javadoc comments. Comments will still be trimmed, indented and fixed for missing asterisks.",
" --length, -length",
" Character length to format.",
" --lines, -lines, --line, -line",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,113 @@ public void javadocCanEndAnywhere() {
doFormatTest(input, expected);
}

@Test
public void skipEmpty() {
// For skip javadoc formatting cases all we care about are that the comments are unchanged.
String[] input = {
"/***/", //
"class Test {}",
};
doSkipFormatTest(input, input);
}

@Test
public void skipJavadocFormattingStillFixesNoAsterisk() {
String[] input = {
"/**", //
" abc<p>def",
" */",
"class Test {}",
};
String[] expected = {
"/**", //
" * abc<p>def",
" */",
"class Test {}",
};
doSkipFormatTest(input, expected);
}

@Test
public void skipListHtmlButFixIndentation() {
// Skipping javadoc formatting doesn't stop fixing indentation.
String[] input = {
"/**", //
"* hi",
"*",
"* <ul>",
"* <li>",
"* <ul>",
"* <li>a</li>",
"* </ul>",
"* </li>",
"* </ul>",
"*/",
"class Test {}",
};
String[] expected = {
"/**", //
" * hi",
" *",
" * <ul>",
" * <li>",
" * <ul>",
" * <li>a</li>",
" * </ul>",
" * </li>",
" * </ul>",
" */",
"class Test {}",
};
doSkipFormatTest(input, expected);
}

@Test
public void skipInferParagraphTags() {
String[] input = {
"/**",
" *",
" *",
" * foo",
" * foo",
" *",
" *",
" * foo",
" *",
" * bar",
" *",
" * <pre>",
" *",
" * baz",
" *",
" * </pre>",
" *",
" * <ul>",
" * <li>foo",
" *",
" * bar",
" * </ul>",
" *",
" *",
" */",
"class Test {}",
};
doSkipFormatTest(input, input);
}

@Test
public void skipRemoveCloseTags() {
String[] input = {
"/**", //
" * foo</p>",
" *",
" * <p>bar</p>",
" */",
"class Test {}",
};
doSkipFormatTest(input, input);
}

private void doFormatTest(String[] input, String[] expected) {
try {
String actual = formatter.formatSource(Joiner.on('\n').join(input));
Expand All @@ -1345,6 +1452,17 @@ private void doFormatTest(String[] input, String[] expected) {
}
}

private void doSkipFormatTest(String[] input, String[] expected) {
try {
Formatter skipFormatter =
new Formatter(JavaFormatterOptions.builder().skipJavadocFormatting(true).build());
String actual = skipFormatter.formatSource(Joiner.on('\n').join(input));
assertThat(actual).isEqualTo(Joiner.on('\n').join(expected) + "\n");
} catch (FormatterException e) {
throw new AssertionError(e);
}
}

@Test
public void windowsLineSeparator() throws FormatterException {
String[] input = {
Expand Down