diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java index 71e64b804..5446d98fb 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java @@ -186,9 +186,34 @@ private ImmutableList generateTokens() throws LexException { } result = optionalizeSpacesAfterLinks(result); result = deindentPreCodeBlocks(result); + if (classicJavadoc) { + result = removeTrailingEmptyParagraphTag(result); + } return result; } + private static ImmutableList removeTrailingEmptyParagraphTag(List input) { + int end = input.size() - 1; + + if (end >= 0 && input.get(end) instanceof EndJavadoc) { + end--; + } + + while (end >= 0 && input.get(end) instanceof Whitespace) { + end--; + } + + if (end < 0 || !(input.get(end) instanceof ParagraphOpenTag)) { + return ImmutableList.copyOf(input); + } + + ImmutableList.Builder result = ImmutableList.builder(); + result.addAll(input.subList(0, end)); + result.addAll(input.subList(input.size() - 1, input.size())); + + return result.build(); + } + private Token processMarkdownToken(Token markdownToken) { // For `...`, we switch to MARKDOWN_CODE_CONTEXT for the duration of the span, and we change the // start or end token to a Literal so it will get joined to adjacent Literal tokens. That diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java index 92f65be6b..9230da4ac 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -1252,6 +1252,25 @@ class Test {} doFormatTest(input, expected); } + @Test + public void removeTrailingEmptyParagraphTag() { + String input = + """ + /** + * The client's first name. + * + *

+ */ + class Test {}\ + """; + String expected = + """ + /** The client's first name. */ + class Test {} + """; + doFormatTest(input, expected); + } + @Test public void inferParagraphTags() { String input =