Skip to content

Commit

Permalink
Add option to format javadoc (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinychen authored Sep 5, 2023
1 parent df30041 commit 83277dd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 56 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-921.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Add option to format javadoc
links:
- https://github.com/palantir/palantir-java-format/pull/921
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

0 comments on commit 83277dd

Please sign in to comment.