Skip to content

Keep a javadoc {@snippet} as it is written - #75

Merged
abashev merged 1 commit into
mainfrom
javadoc-snippet
Sep 25, 2026
Merged

abashev merged 1 commit into
mainfrom
javadoc-snippet

Conversation

@abashev

@abashev abashev commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Brings over google/google-java-format#1141, a fix from google-java-format 1.24.

The bug

With javadoc formatting on (JavaFormatterOptions.formatJavadoc(true)), the lines of a {@snippet ...} tag were reflowed like prose:

/**
 * Example usage:
 *
 * <p>{@snippet : int x = 1; foo(x, y); // keep this }
 *
 * <p>Done.
 */

The code inside came out on one line, <p> in front of it, and a // comment in the snippet swallowed the closing brace. The lexer treated the snippet as any other inline tag, whose contents are joined and wrapped.

From upstream, with the author kept

  • Avoid mangling {@snippet ...}. google/google-java-format#1141 by cpovirk (Chris Povirk), commit authored by them: {@snippet that starts an inline tag is its own token, everything up to its closing brace keeps its newlines and spaces as inside <pre>, and the writer puts a blank line before and after the snippet. Nothing inside the snippet is reformatted. The tests blankLinesAroundSnippetAndNoMangling and notASnippetUnlessOuterTag come with it.
  • Adapted to this lexer, which keeps inline tags as INLINE_TAG_OPEN and INLINE_TAG_CLOSE tokens (Keep javadoc inline tags like {@link ...} together palantir/palantir-java-format#125): a {@snippet inside another inline tag is an ordinary inline tag here, as upstream's is an ordinary literal.

Added here

  • snippetKeepsCommentsAndIndentation: a snippet with indented code and a // @highlight markup comment stays exactly as written.

Checked

  • The three tests fail on main and pass with the change.
  • ./gradlew :open-java-format:test on JDK 21: 1500 tests, all green.
  • Javadoc formatting is off unless JavaFormatterOptions asks for it, and the lexer only runs then, so the default output and the JDK corpus are unchanged by construction.

With javadoc formatting on, the lines of a {@snippet ...} tag were
reflowed like prose: the code inside came out on one line, "<p>" in
front of it, and a "//" comment in the snippet swallowed the closing
brace. The lexer treated the snippet as any other inline tag, whose
contents are joined and wrapped.

This ports google/google-java-format#1141 by cpovirk: "{@snippet"
that starts an inline tag is its own token, everything up to its
closing brace keeps its newlines and spaces as inside <pre>, and the
writer puts a blank line before and after the snippet. Nothing inside
the snippet is reformatted. The two tests come from upstream; a third
one pins a snippet with indentation and a // markup comment.

Adapted to this lexer, which keeps inline tags as INLINE_TAG_OPEN and
INLINE_TAG_CLOSE tokens: a "{@snippet" inside another inline tag is an
ordinary inline tag, as upstream's is an ordinary literal.

Javadoc formatting is off unless JavaFormatterOptions asks for it, so
the default output is unchanged.
@abashev
abashev merged commit 58947f8 into main Sep 25, 2026
15 checks passed
@abashev
abashev deleted the javadoc-snippet branch September 25, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants