Skip to content

Commit c439a9f

Browse files
committed
Fix: javadoc MAX_LINE_LENGTH follows style, rather than always 100 (#44)
Javadoc follows the line length configured in the [JavaFormatterOptions.Style](https://github.com/palantir/palantir-java-format/blob/05df9d3b5c10f2bdd434181b6f046309a1040a37/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java#L32-L57).
1 parent 9e25a93 commit c439a9f

17 files changed

Lines changed: 50 additions & 47 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
.apt_generated/
1010

1111
target/
12-
/build/
13-
*/build
14-
*/*/build/
12+
build/
1513

1614
bin/
1715
out/

‎changelog/@unreleased/pr-44.v2.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Javadoc follows the line length configured in the [JavaFormatterOptions.Style](internal-github
4+
links:
5+
- internal-github

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public final class JavaCommentsHelper implements CommentsHelper {
3131

3232
private final String lineSeparator;
3333
private final JavaFormatterOptions options;
34+
private final JavadocFormatter javadocFormatter;
3435

3536
public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
3637
this.lineSeparator = lineSeparator;
3738
this.options = options;
39+
this.javadocFormatter = new JavadocFormatter(options.maxLineLength());
3840
}
3941

4042
@Override
@@ -44,7 +46,7 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
4446
}
4547
String text = tok.getOriginalText();
4648
if (tok.isJavadocComment()) {
47-
text = JavadocFormatter.formatJavadoc(text, column0);
49+
text = javadocFormatter.formatJavadoc(text, column0);
4850
}
4951
List<String> lines = new ArrayList<>();
5052
Iterator<String> it = Newlines.lineIterator(text);

‎core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@
3636
*/
3737
public final class JavadocFormatter {
3838

39-
static final int MAX_LINE_LENGTH = 100;
39+
private final int maxLineLength;
40+
41+
public JavadocFormatter(int maxLineLength) {
42+
this.maxLineLength = maxLineLength;
43+
}
4044

4145
/**
4246
* Formats the given Javadoc comment, which must start with ∕✱✱ and end with ✱∕. The output will
4347
* start and end with the same characters.
4448
*/
45-
public static String formatJavadoc(String input, int blockIndent) {
49+
public String formatJavadoc(String input, int blockIndent) {
4650
ImmutableList<Token> tokens;
4751
try {
4852
tokens = lex(input);
@@ -53,8 +57,8 @@ public static String formatJavadoc(String input, int blockIndent) {
5357
return makeSingleLineIfPossible(blockIndent, result);
5458
}
5559

56-
private static String render(List<Token> input, int blockIndent) {
57-
JavadocWriter output = new JavadocWriter(blockIndent);
60+
private String render(List<Token> input, int blockIndent) {
61+
JavadocWriter output = new JavadocWriter(blockIndent, maxLineLength);
5862
for (Token token : input) {
5963
switch (token.getType()) {
6064
case BEGIN_JAVADOC:
@@ -165,8 +169,8 @@ private static Token standardize(Token token, Token standardToken) {
165169
* Returns the given string or a one-line version of it (e.g., "∕✱✱ Tests for foos. ✱∕") if it
166170
* fits on one line.
167171
*/
168-
private static String makeSingleLineIfPossible(int blockIndent, String input) {
169-
int oneLinerContentLength = MAX_LINE_LENGTH - "/** */".length() - blockIndent;
172+
private String makeSingleLineIfPossible(int blockIndent, String input) {
173+
int oneLinerContentLength = maxLineLength - "/** */".length() - blockIndent;
170174
Matcher matcher = ONE_CONTENT_LINE_PATTERN.matcher(input);
171175
if (matcher.matches() && matcher.group(1).isEmpty()) {
172176
return "/** */";
@@ -175,6 +179,4 @@ private static String makeSingleLineIfPossible(int blockIndent, String input) {
175179
}
176180
return input;
177181
}
178-
179-
private JavadocFormatter() {}
180182
}

‎core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
final class JavadocWriter {
4242
private final int blockIndent;
43+
private final int maxLineLength;
4344
private final StringBuilder output = new StringBuilder();
4445
/**
4546
* Whether we are inside an {@code <li>} element, excluding the case in which the {@code <li>}
@@ -59,8 +60,9 @@ final class JavadocWriter {
5960
private int indentForMoeEndStripComment;
6061
private boolean wroteAnythingSignificant;
6162

62-
JavadocWriter(int blockIndent) {
63+
JavadocWriter(int blockIndent, int maxLineLength) {
6364
this.blockIndent = blockIndent;
65+
this.maxLineLength = maxLineLength;
6466
}
6567

6668
/**
@@ -373,7 +375,7 @@ private void writeNewline(AutoIndent autoIndent) {
373375
appendSpaces(blockIndent + 1);
374376
output.append("*");
375377
appendSpaces(1);
376-
remainingOnLine = JavadocFormatter.MAX_LINE_LENGTH - blockIndent - 3;
378+
remainingOnLine = maxLineLength - blockIndent - 3;
377379
if (autoIndent == AUTO_INDENT) {
378380
appendSpaces(innerIndent());
379381
remainingOnLine -= innerIndent();

‎core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
@RunWith(JUnit4.class)
2929
public final class JavadocFormattingTest {
3030

31-
private final Formatter formatter = new Formatter();
31+
private final Formatter formatter =
32+
new Formatter(
33+
JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.GOOGLE).build());
3234

3335
@Test
3436
public void notJavadoc() {

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/A.output‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.google.googlejavaformat.java.test;
22

33
/**
4-
* Tests for AbstractTypeDeclarations, AnnotationTypeDeclarations, AnnotationTypeMemberDeclarations,
5-
* Annotations, AnonymousClassDeclarations, ArrayAccesses, ArrayCreations, ArrayInitializers,
6-
* ArrayTypes, AssertStatements, and Assignments.
4+
* Tests for AbstractTypeDeclarations, AnnotationTypeDeclarations, AnnotationTypeMemberDeclarations, Annotations,
5+
* AnonymousClassDeclarations, ArrayAccesses, ArrayCreations, ArrayInitializers, ArrayTypes, AssertStatements, and
6+
* Assignments.
77
*/
88
class A {
99
public @interface X {

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/B30223986.output‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
22
* Base class for {@link Foo}.
33
*
4-
* <p>The subclass should implement {@link #bar()}, with the implementation returning a new instance
5-
* of the foo relevant to that baz.
4+
* <p>The subclass should implement {@link #bar()}, with the implementation returning a new instance of the foo relevant
5+
* to that baz.
66
*
77
* <p>Example: <code>
88
* @Override
99
* protected Foo bar() {
1010
* return new Foo();
1111
* }
12-
* </code> The subclass should call {@link #get()} to get the foo object, and should not cache it in
13-
* the subclass. Example: <code>
12+
* </code> The subclass should call {@link #get()} to get the foo object, and should not cache it in the subclass.
13+
* Example: <code>
1414
* @Annotation("something")
1515
* public void thing() {
1616
* }

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/C.output‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.google.googlejavaformat.java.test;
22

33
/**
4-
* Tests for CastExpressions, CatchClauses, CharacterLiterals, ClassInstanceCreations,
5-
* CommentHelper, ConditionalExpressions, ConstructorInvocations, ContinueStatements, and
6-
* CreationReferences.
4+
* Tests for CastExpressions, CatchClauses, CharacterLiterals, ClassInstanceCreations, CommentHelper,
5+
* ConditionalExpressions, ConstructorInvocations, ContinueStatements, and CreationReferences.
76
*/
87
class C<T> {
98
// TODO(jdd): Test higher-language-level constructs.

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/I.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.google.googlejavaformat.java.test;
33
import java.util.List;
44

55
/**
6-
* Tests for IfStatements, ImportDeclarations, InfixExpressions, Initializers,
7-
* InstanceofExpressions, and IntersectionTypes.
6+
* Tests for IfStatements, ImportDeclarations, InfixExpressions, Initializers, InstanceofExpressions, and
7+
* IntersectionTypes.
88
*/
99
class I {
1010
interface I0 {}

0 commit comments

Comments
 (0)