Skip to content

Commit e4cef58

Browse files
cpovirkgoogle-java-format Team
authored andcommitted
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
1 parent adf9252 commit e4cef58

2 files changed

Lines changed: 18 additions & 28 deletions

File tree

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ void writeFooterJavadocTagStart(FooterJavadocTagStart token) {
145145
*/
146146
postWriteModifiedContinuingListStack.reset();
147147

148-
if (!wroteAnythingSignificant) {
149-
// Javadoc consists solely of tags. This is frowned upon in general but OK for @Overrides.
150-
} else if (!continuingFooterTag) {
148+
if (!continuingFooterTag) {
151149
// First footer tag after a body tag.
152150
requestBlankLine();
153151
} else {
@@ -234,9 +232,7 @@ void writeListItemOpen(ListItemOpenTag token) {
234232
}
235233

236234
void writeHeaderOpen(HeaderOpenTag token) {
237-
if (wroteAnythingSignificant) {
238-
requestBlankLine();
239-
}
235+
requestBlankLine();
240236

241237
writeToken(token);
242238
}
@@ -262,29 +258,23 @@ void writeParagraphOpen(Token token) {
262258
}
263259

264260
void writeBlockquoteOpen(BlockquoteOpenTag token) {
265-
if (wroteAnythingSignificant) {
266-
requestBlankLine();
267-
}
261+
requestBlankLine();
268262

269263
writeToken(token);
270264

271265
requestNewline();
272266
}
273267

274268
void writeBlockquoteClose(BlockquoteCloseTag token) {
275-
if (wroteAnythingSignificant) {
276-
requestNewline();
277-
}
269+
requestNewline();
278270

279271
writeToken(token);
280272

281273
requestBlankLine();
282274
}
283275

284276
void writePreOpen(PreOpenTag token) {
285-
if (wroteAnythingSignificant) {
286-
requestBlankLine();
287-
}
277+
requestBlankLine();
288278

289279
writeToken(token);
290280
}
@@ -304,9 +294,7 @@ void writeCodeClose(CodeCloseTag token) {
304294
}
305295

306296
void writeTableOpen(TableOpenTag token) {
307-
if (wroteAnythingSignificant) {
308-
requestBlankLine();
309-
}
297+
requestBlankLine();
310298

311299
writeToken(token);
312300
}
@@ -355,7 +343,7 @@ void writeLiteral(Literal token) {
355343
}
356344

357345
void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
358-
if (wroteAnythingSignificant && !atStartOfLine) {
346+
if (!atStartOfLine) {
359347
// A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken.
360348
requestBlankLine();
361349
}
@@ -371,11 +359,12 @@ void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
371359
});
372360
writeNewline();
373361
output.append(token.end());
362+
wroteAnythingSignificant = true;
374363
requestBlankLine();
375364
}
376365

377366
void writeMarkdownTable(MarkdownTable token) {
378-
if (wroteAnythingSignificant && !atStartOfLine) {
367+
if (!atStartOfLine) {
379368
requestBlankLine();
380369
}
381370
flushWhitespace();
@@ -385,6 +374,7 @@ void writeMarkdownTable(MarkdownTable token) {
385374
writeNewline(AutoIndent.NO_AUTO_INDENT);
386375
output.append(line);
387376
}
377+
wroteAnythingSignificant = true;
388378
requestBlankLine();
389379
}
390380

@@ -419,6 +409,10 @@ private void flushWhitespace() {
419409
requestNewline();
420410
}
421411

412+
if (!wroteAnythingSignificant) {
413+
requestedWhitespace = NONE;
414+
}
415+
422416
if (classicJavadoc
423417
&& requestedWhitespace == BLANK_LINE
424418
&& (!postWriteModifiedContinuingListStack.isEmpty() || continuingFooterTag)) {
@@ -466,6 +460,7 @@ private void writeToken(Token token) {
466460
output.append(requestedMoeBeginStripComment.value());
467461
requestedMoeBeginStripComment = null;
468462
indentForMoeEndStripComment = innerIndent();
463+
wroteAnythingSignificant = true;
469464
requestNewline();
470465
writeToken(token);
471466
return;

core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ class Test {}\
284284
String expected =
285285
"""
286286
/**
287-
*
288287
* <!-- moe:begin_intracomment_strip -->
289288
* Foo.
290289
* <!-- moe:end_intracomment_strip -->
@@ -1995,11 +1994,9 @@ public void markdownLooseLists() {
19951994
/// - item 2
19961995
class Test {}
19971996
""";
1998-
// TODO: the line break between items should be preserved, and there should not be a blank line
1999-
// before the list.
1997+
// TODO: the line break between items should be preserved.
20001998
String expected =
20011999
"""
2002-
///
20032000
/// - item 1
20042001
/// - item 2
20052002
class Test {}
@@ -2259,7 +2256,8 @@ public void tableInHtmlListItem() {
22592256
class Test {}
22602257
""";
22612258
// requestBlankLine() in writeTableOpen() inserts a blank line after <li> before <table> when
2262-
// inside a list item.
2259+
// inside a list item. Also, we might prefer to eliminate the blank lines around the table
2260+
// element and to indent the table contents and close tag at least as far as the open tag.
22632261
String expected =
22642262
"""
22652263
/// <ul>
@@ -2286,11 +2284,8 @@ public void tableInsideMarkdownListItemAfterText() {
22862284
/// </table>
22872285
class Test {}
22882286
""";
2289-
// requestBlankLine() in writeTableOpen() inserts a blank line after item text before <table>
2290-
// inside the list item.
22912287
String expected =
22922288
"""
2293-
///
22942289
/// - item text
22952290
///
22962291
/// <table>

0 commit comments

Comments
 (0)