From e4cef58267456f586b7263d8073f480fd7348383 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 10 Jul 2026 12:38:09 -0700 Subject: [PATCH] Improve interactions between `wroteAnythingSignificant` and whitespace requests. Mainly, make all whitespace requests into no-ops if we haven't written anything significant yet. This lets us remove various existing special cases. But also, start setting `wroteAnythingSignificant` in some locations where we write to `output` without using `writeToken` (which would set `wroteAnythingSignificant` automatically). PiperOrigin-RevId: 945842813 --- .../java/javadoc/JavadocWriter.java | 35 ++++++++----------- .../java/JavadocFormattingTest.java | 11 ++---- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java index fbe6e84d2..3c4b7135e 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java @@ -145,9 +145,7 @@ void writeFooterJavadocTagStart(FooterJavadocTagStart token) { */ postWriteModifiedContinuingListStack.reset(); - if (!wroteAnythingSignificant) { - // Javadoc consists solely of tags. This is frowned upon in general but OK for @Overrides. - } else if (!continuingFooterTag) { + if (!continuingFooterTag) { // First footer tag after a body tag. requestBlankLine(); } else { @@ -234,9 +232,7 @@ void writeListItemOpen(ListItemOpenTag token) { } void writeHeaderOpen(HeaderOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLine(); writeToken(token); } @@ -262,9 +258,7 @@ void writeParagraphOpen(Token token) { } void writeBlockquoteOpen(BlockquoteOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLine(); writeToken(token); @@ -272,9 +266,7 @@ void writeBlockquoteOpen(BlockquoteOpenTag token) { } void writeBlockquoteClose(BlockquoteCloseTag token) { - if (wroteAnythingSignificant) { - requestNewline(); - } + requestNewline(); writeToken(token); @@ -282,9 +274,7 @@ void writeBlockquoteClose(BlockquoteCloseTag token) { } void writePreOpen(PreOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLine(); writeToken(token); } @@ -304,9 +294,7 @@ void writeCodeClose(CodeCloseTag token) { } void writeTableOpen(TableOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLine(); writeToken(token); } @@ -355,7 +343,7 @@ void writeLiteral(Literal token) { } void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) { - if (wroteAnythingSignificant && !atStartOfLine) { + if (!atStartOfLine) { // A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken. requestBlankLine(); } @@ -371,11 +359,12 @@ void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) { }); writeNewline(); output.append(token.end()); + wroteAnythingSignificant = true; requestBlankLine(); } void writeMarkdownTable(MarkdownTable token) { - if (wroteAnythingSignificant && !atStartOfLine) { + if (!atStartOfLine) { requestBlankLine(); } flushWhitespace(); @@ -385,6 +374,7 @@ void writeMarkdownTable(MarkdownTable token) { writeNewline(AutoIndent.NO_AUTO_INDENT); output.append(line); } + wroteAnythingSignificant = true; requestBlankLine(); } @@ -419,6 +409,10 @@ private void flushWhitespace() { requestNewline(); } + if (!wroteAnythingSignificant) { + requestedWhitespace = NONE; + } + if (classicJavadoc && requestedWhitespace == BLANK_LINE && (!postWriteModifiedContinuingListStack.isEmpty() || continuingFooterTag)) { @@ -466,6 +460,7 @@ private void writeToken(Token token) { output.append(requestedMoeBeginStripComment.value()); requestedMoeBeginStripComment = null; indentForMoeEndStripComment = innerIndent(); + wroteAnythingSignificant = true; requestNewline(); writeToken(token); return; 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 eec7f43fb..8caab7187 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -284,7 +284,6 @@ class Test {}\ String expected = """ /** - * * * Foo. * @@ -1995,11 +1994,9 @@ public void markdownLooseLists() { /// - item 2 class Test {} """; - // TODO: the line break between items should be preserved, and there should not be a blank line - // before the list. + // TODO: the line break between items should be preserved. String expected = """ - /// /// - item 1 /// - item 2 class Test {} @@ -2259,7 +2256,8 @@ public void tableInHtmlListItem() { class Test {} """; // requestBlankLine() in writeTableOpen() inserts a blank line after
  • before when - // inside a list item. + // inside a list item. Also, we might prefer to eliminate the blank lines around the table + // element and to indent the table contents and close tag at least as far as the open tag. String expected = """ ///
    class Test {} """; - // requestBlankLine() in writeTableOpen() inserts a blank line after item text before - // inside the list item. String expected = """ - /// /// - item text /// ///