Keep a javadoc {@snippet} as it is written - #75
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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
{@snippet ...}. google/google-java-format#1141 by cpovirk (Chris Povirk), commit authored by them:{@snippetthat 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 testsblankLinesAroundSnippetAndNoManglingandnotASnippetUnlessOuterTagcome with it.INLINE_TAG_OPENandINLINE_TAG_CLOSEtokens (Keep javadoc inline tags like {@link ...} together palantir/palantir-java-format#125): a{@snippetinside 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// @highlightmarkup comment stays exactly as written.Checked
./gradlew :open-java-format:teston JDK 21: 1500 tests, all green.JavaFormatterOptionsasks for it, and the lexer only runs then, so the default output and the JDK corpus are unchanged by construction.