From a664ee9c5ff0da6c7c5415006e707a5fcf7739f6 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Tue, 24 Nov 2026 13:01:56 +0300 Subject: [PATCH] Keep the blank line before a Javadoc
instead of inferring a

Where a blank line separates two words of a Javadoc comment, the lexer infers a

in front of the second. When that word is a

start tag, the

comes out empty: a

cannot hold a

, so the
closes it, and doclint warns "empty

tag". The blank line now stays without the

, as google-java-format#1534 proposes.

and
count, does not, and a

written in the comment stays. Inside a list item or a block tag the writer turns the blank lines that other tokens ask for into newlines. A newline before the

would be plain whitespace the next time the output is formatted, and the
would join the line before it, so this blank line is written there too. The upstream change writes a newline, and so changes its own output on a second pass. Among the 128k files of JDK 21's src.zip and the Gradle cache's sources jars, 17 have a Javadoc line that starts with a
. In 7 of them an inferred

goes, and nothing else changes; formatting the output again changes nothing. Fixes #268. --- .../java/javadoc/JavadocFormatter.java | 1 + .../javaformat/java/javadoc/JavadocLexer.java | 18 +++++- .../java/javadoc/JavadocWriter.java | 15 ++++- .../javaformat/java/javadoc/Token.java | 6 ++ .../java/JavadocFormattingTest.java | 63 +++++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocFormatter.java b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocFormatter.java index 62695849d..26913095b 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocFormatter.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocFormatter.java @@ -83,6 +83,7 @@ private String render(List input, int blockIndent) { case HTML_COMMENT -> output.writeHtmlComment(token); case BR_TAG -> output.writeBr(standardizeBrToken(token)); case WHITESPACE -> output.requestWhitespace(); + case BLANK_LINE -> output.requestForcedBlankLine(); case FORCED_NEWLINE -> output.writeLineBreakNoAutoIndent(); case LITERAL, INLINE_TAG_OPEN, INLINE_TAG_CLOSE -> output.writeLiteral(token); case PARAGRAPH_CLOSE_TAG, LIST_ITEM_CLOSE_TAG, OPTIONAL_LINE_BREAK -> {} diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java index 37ed1884f..53b02c731 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocLexer.java @@ -19,6 +19,7 @@ import static com.google.common.base.Verify.verify; import static com.google.common.collect.Iterators.peekingIterator; import static com.palantir.javaformat.java.javadoc.Token.Type.BEGIN_JAVADOC; +import static com.palantir.javaformat.java.javadoc.Token.Type.BLANK_LINE; import static com.palantir.javaformat.java.javadoc.Token.Type.BLOCKQUOTE_CLOSE_TAG; import static com.palantir.javaformat.java.javadoc.Token.Type.BLOCKQUOTE_OPEN_TAG; import static com.palantir.javaformat.java.javadoc.Token.Type.BR_TAG; @@ -422,6 +423,8 @@ private static boolean isSuspendedHyphenConjunction(String s) { /** * Where the input has two consecutive line breaks between literals, insert a {@code

} tag between the literals. + * Where the second literal starts a {@code

}, which a {@code

} cannot hold, turn the line breaks into a + * {@link Token.Type#BLANK_LINE} instead. * *

This method must be called after {@link #joinAdjacentLiteralsAndAdjacentWhitespace}, as it assumes that * adjacent whitespace tokens have already been joined. @@ -435,9 +438,15 @@ private static List inferParagraphTags(List input) { if (tokens.peek().getType() == WHITESPACE && hasMultipleNewlines(tokens.peek().getValue())) { - output.add(tokens.next()); - - if (tokens.peek().getType() == LITERAL) { + var whitespace = tokens.next(); + var next = tokens.peek(); + + if (next.getType() != LITERAL) { + output.add(whitespace); + } else if (DIV_OPEN_PATTERN.matcher(next.getValue()).find()) { + output.add(new Token(BLANK_LINE, whitespace.getValue())); + } else { + output.add(whitespace); output.add(new Token(PARAGRAPH_OPEN_TAG, "

")); } } @@ -645,6 +654,9 @@ private static boolean hasMultipleNewlines(String s) { private static final Pattern BLOCKQUOTE_OPEN_PATTERN = openTagPattern("blockquote"); private static final Pattern BLOCKQUOTE_CLOSE_PATTERN = closeTagPattern("blockquote"); private static final Pattern BR_PATTERN = openTagPattern("br"); + // Tried on a literal, which ends at whitespace: ". + private static final Pattern DIV_OPEN_PATTERN = compile("^]|$)", CASE_INSENSITIVE); private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile("^[{]@snippet\\b"); private static final Pattern INLINE_TAG_OPEN_PATTERN = compile("^[{]@\\w*"); /** diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocWriter.java b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocWriter.java index 0767ce813..00cd1c953 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocWriter.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/JavadocWriter.java @@ -18,6 +18,7 @@ import static com.palantir.javaformat.java.javadoc.JavadocWriter.AutoIndent.AUTO_INDENT; import static com.palantir.javaformat.java.javadoc.JavadocWriter.AutoIndent.NO_AUTO_INDENT; import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.BLANK_LINE; +import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.FORCED_BLANK_LINE; import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.NEWLINE; import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.NONE; import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.WHITESPACE; @@ -69,6 +70,16 @@ void requestWhitespace() { requestWhitespace(WHITESPACE); } + /** + * Requests a blank line that, unlike the ones other tokens request, is written inside lists and footer tags too. + * The lexer turns the blank line before a {@code

} into this request instead of inferring a {@code

}: a + * newline there would leave nothing to keep the {@code

} off the line before it when the output is formatted + * again. + */ + void requestForcedBlankLine() { + requestWhitespace(FORCED_BLANK_LINE); + } + void writeBeginJavadoc() { /* * JavaCommentsHelper will make sure this is indented right. But it seems sensible enough that, @@ -295,6 +306,8 @@ enum RequestedWhitespace { WHITESPACE, NEWLINE, BLANK_LINE, + /** A {@link #BLANK_LINE} that is written inside lists and footer tags too. */ + FORCED_BLANK_LINE, ; } @@ -317,7 +330,7 @@ private void writeToken(Token token) { requestedWhitespace = NEWLINE; } - if (requestedWhitespace == BLANK_LINE) { + if (requestedWhitespace == BLANK_LINE || requestedWhitespace == FORCED_BLANK_LINE) { writeBlankLine(); requestedWhitespace = NONE; } else if (requestedWhitespace == NEWLINE) { diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/Token.java b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/Token.java index 2ebc42e66..fc927e914 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/Token.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/javadoc/Token.java @@ -69,6 +69,12 @@ enum Type { * asterisks) or spaces. */ WHITESPACE, + /** + * Whitespace with a blank line in it, before a {@code
}. {@link JavadocLexer} infers no {@code

} there, + * since a {@code

} cannot hold a {@code

}, so {@link JavadocWriter} keeps the blank line instead, even + * in a list or footer tag. + */ + BLANK_LINE, /** * A newline in a {@code
} or {@code } section. We preserve user formatting in these sections,
          * including newlines.
diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java
index 320e3d5d2..8ee411c51 100644
--- a/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java
+++ b/open-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java
@@ -1149,6 +1149,69 @@ void inferParagraphTags() {
         doFormatTest(input, expected);
     }
 
+    @Test
+    void noParagraphTagInferredBeforeDiv() {
+        // A 

cannot hold a

, so a blank line before one stays a blank line. + String[] input = { + "/**", + " * Intro.", + " *", + " * Another paragraph.", + " *", + " *
Block content.
", + " *", + " * Following paragraph.", + " *", + " *
", + " *", + " * Custom element.", + " *", + " *

Explicit paragraph.
", + " *", + " *
    ", + " *
  • Item.", + " *", + " *
    In an item.
    ", + " *
", + " *", + " * @param x Parameter.", + " *", + " *
In a block tag.
", + " */", + "class Test {}", + }; + String[] expected = { + "/**", + " * Intro.", + " *", + " *

Another paragraph.", + " *", + " *

Block content.
", + " *", + " *

Following paragraph.", + " *", + " *

", + " *", + " *

Custom element.", + " *", + " *

Explicit paragraph.
", + " *", + " *
    ", + " *
  • Item.", + " *", + " *
    In an item.
    ", + " *
", + " *", + " * @param x Parameter.", + " *", + " *
In a block tag.
", + " */", + "class Test {}", + }; + doFormatTest(input, expected); + doFormatTest(expected, expected); + } + @Test void paragraphTagNewlines() throws Exception { String input = new String(