Skip to content

Commit 0e485a8

Browse files
kluevergoogle-java-format Team
authored andcommitted
Prevent leading blank lines in list items when formatting block tags.
* Update `writeTableOpen`, `writePreOpen`, and `writeBlockquoteOpenOrClose` in `JavadocWriter` to check `if (wroteAnythingSignificant && !atStartOfLine)` before requesting a blank line. This matches existing logic in `writeMarkdownTable` (`line 378`) and `writeMarkdownFencedCodeBlock` (`line 346`), ensuring that block elements opening immediately at the start of a list item (`<li>` or `- `) do not get preceded by an unnecessary blank line. * Fix a `NullPointerException` in `MarkdownPositions.visitListItem` when a Markdown list item has a non-`Paragraph` first child (such as `- <table>...</table>`), safely falling through when `firstChild` is not an instance of `Paragraph`. * Update unit tests in `JavadocFormattingTest` (`tableInHtmlListItem` and `tableInMarkdownListItem`) to verify correct formatting and verify no `NullPointerException` is thrown when `- <table>` is lexed. PiperOrigin-RevId: 945755137
1 parent 77bee2d commit 0e485a8

3 files changed

Lines changed: 35 additions & 21 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void writeParagraphOpen(Token token) {
260260
}
261261

262262
void writeBlockquoteOpenOrClose(Token token) {
263-
if (wroteAnythingSignificant) {
263+
if (wroteAnythingSignificant && !atStartOfLine) {
264264
requestBlankLine();
265265
}
266266

@@ -270,7 +270,7 @@ void writeBlockquoteOpenOrClose(Token token) {
270270
}
271271

272272
void writePreOpen(PreOpenTag token) {
273-
if (wroteAnythingSignificant) {
273+
if (wroteAnythingSignificant && !atStartOfLine) {
274274
requestBlankLine();
275275
}
276276

@@ -292,7 +292,7 @@ void writeCodeClose(CodeCloseTag token) {
292292
}
293293

294294
void writeTableOpen(TableOpenTag token) {
295-
if (wroteAnythingSignificant) {
295+
if (wroteAnythingSignificant && !atStartOfLine) {
296296
requestBlankLine();
297297
}
298298

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,16 @@ private boolean visitListItem(ListItem listItem) {
129129
verify(matcher.lookingAt());
130130
ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1));
131131
addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
132-
return switch (listItem.getFirstChild()) {
133-
case Paragraph paragraph -> {
134-
// A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
135-
// because that would lead us to introduce a line break after the list introduction
136-
// (the `-` or whatever). So we visit the children and siblings of the Paragraph instead.
137-
visitNodeList(paragraph.getFirstChild());
138-
visitNodeList(paragraph.getNext());
139-
yield true;
140-
}
141-
default -> false;
142-
};
132+
Node firstChild = listItem.getFirstChild();
133+
if (firstChild instanceof Paragraph paragraph) {
134+
// A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
135+
// because that would lead us to introduce a line break after the list introduction
136+
// (the `-` or whatever). So we visit the children and siblings of the Paragraph instead.
137+
visitNodeList(paragraph.getFirstChild());
138+
visitNodeList(paragraph.getNext());
139+
return true;
140+
}
141+
return false;
143142
}
144143

145144
private void visitHeading(Heading heading) {

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,14 +2216,10 @@ public void tableInHtmlListItem() {
22162216
/// </ul>
22172217
class Test {}
22182218
""";
2219-
// requestBlankLine() in writeTableOpen() inserts a blank line after <li> before <table> when
2220-
// inside a list item.
22212219
String expected =
22222220
"""
22232221
/// <ul>
2224-
/// <li>
2225-
///
2226-
/// <table>
2222+
/// <li><table>
22272223
/// <tr><td>Foo</td></tr>
22282224
/// </table>
22292225
///
@@ -2233,6 +2229,27 @@ class Test {}
22332229
doFormatTest(input, expected);
22342230
}
22352231

2232+
@Test
2233+
public void tableInMarkdownListItem() {
2234+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2235+
String input =
2236+
"""
2237+
/// - <table>
2238+
/// <tr><td>Foo</td></tr>
2239+
/// </table>
2240+
class Test {}
2241+
""";
2242+
String expected =
2243+
"""
2244+
///
2245+
/// - <table>
2246+
/// <tr><td>Foo</td></tr>
2247+
/// </table>
2248+
class Test {}
2249+
""";
2250+
doFormatTest(input, expected);
2251+
}
2252+
22362253
@Test
22372254
public void tableInsideMarkdownListItemAfterText() {
22382255
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
@@ -2244,8 +2261,6 @@ public void tableInsideMarkdownListItemAfterText() {
22442261
/// </table>
22452262
class Test {}
22462263
""";
2247-
// requestBlankLine() in writeTableOpen() inserts a blank line after item text before <table>
2248-
// inside the list item.
22492264
String expected =
22502265
"""
22512266
///

0 commit comments

Comments
 (0)