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 option to format javadoc #921

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -57,8 +57,11 @@ public int maxLineLength() {

private final Style style;

private JavaFormatterOptions(Style style) {
private final boolean formatJavadoc;

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

/** Returns the multiplier for the unit of indent. */
Expand All @@ -70,6 +73,10 @@ public int maxLineLength() {
return style.maxLineLength();
}

public boolean formatJavadoc() {
return formatJavadoc;
}

/** Returns the code style. */
public Style style() {
return style;
Expand All @@ -90,15 +97,22 @@ public static final class Builder {
// default is still GOOGLE just because lots of hand-rolled tests rely on this behaviour
private Style style = Style.GOOGLE;

private boolean formatJavadoc = false;

private Builder() {}

public Builder style(Style style) {
this.style = style;
return this;
}

public 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 @@ -91,26 +91,20 @@ public final class Formatter {

private final JavaFormatterOptions options;
private final boolean debugMode;
private final JavaFormatterInternalOptions internalOptions;

@VisibleForTesting
Formatter(JavaFormatterOptions options, boolean debugMode, JavaFormatterInternalOptions internalOptions) {
Formatter(JavaFormatterOptions options, boolean debugMode) {
this.options = options;
this.debugMode = debugMode;
this.internalOptions = internalOptions;
}

/** A new Formatter instance with default options. */
public static Formatter create() {
return new Formatter(
JavaFormatterOptions.defaultOptions(),
false,
JavaFormatterInternalOptions.builder().build());
return new Formatter(JavaFormatterOptions.defaultOptions(), false);
}

public static Formatter createFormatter(JavaFormatterOptions options) {
return new Formatter(
options, false, JavaFormatterInternalOptions.builder().build());
return new Formatter(options, false);
}

/**
Expand Down Expand Up @@ -300,8 +294,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
// 'de-linting' changes (e.g. import ordering).
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);

JavaCommentsHelper commentsHelper =
new JavaCommentsHelper(javaInput.getLineSeparator(), options, internalOptions);
JavaCommentsHelper commentsHelper = new JavaCommentsHelper(javaInput.getLineSeparator(), options);
JavaOutput javaOutput;
try {
javaOutput = format(javaInput, options, commentsHelper, debugMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ public final class JavaCommentsHelper implements CommentsHelper {
private final JavaFormatterOptions options;
private final JavadocFormatter javadocFormatter;

public JavaCommentsHelper(
String lineSeparator, JavaFormatterOptions options, JavaFormatterInternalOptions internalOptions) {
public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
this.lineSeparator = lineSeparator;
this.options = options;
this.javadocFormatter =
internalOptions.reformatJavadoc() ? new JavadocFormatter(options.maxLineLength()) : null;
this.javadocFormatter = options.formatJavadoc() ? new JavadocFormatter(options.maxLineLength()) : null;
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private static Formatter createFormatter() {
JavaFormatterOptions.builder()
.style(JavaFormatterOptions.Style.PALANTIR)
.build(),
isDebugMode(),
JavaFormatterInternalOptions.builder().build());
isDebugMode());
}

@TestTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public final class JavadocFormattingTest {
private final Formatter formatter = new Formatter(
JavaFormatterOptions.builder()
.style(JavaFormatterOptions.Style.GOOGLE)
.formatJavadoc(true)
.build(),
false,
JavaFormatterInternalOptions.builder().reformatJavadoc(true).build());
false);

@Test
public void notJavadoc() {
Expand Down