Skip to content

Commit 2a2ea68

Browse files
kluevergoogle-java-format Team
authored andcommitted
Guard requestBlankLine in writeTableOpen, writePreOpen, and writeBlockquoteOpenOrClose of JavadocWriter.
* Prevent inserting leading blank lines (`///`) before `<table>`, `<pre>`, and `<blockquote>` when they appear as the very first token in a Javadoc comment by checking `if (wroteAnythingSignificant)` before requesting a blank line. * Add `tableAtStartOfComment()`, `preAtStartOfComment()`, and `blockquoteAtStartOfComment()` unit tests in `JavadocFormattingTest`. * Add `tableInHtmlListItem()` and `tableInsideMarkdownListItemAfterText()` unit tests in `JavadocFormattingTest` to document existing list-item table spacing behavior (Thread 4 discussion). PiperOrigin-RevId: 945714394
1 parent d3f17cd commit 2a2ea68

2 files changed

Lines changed: 96 additions & 4 deletions

File tree

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

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

262262
void writeBlockquoteOpenOrClose(Token token) {
263-
requestBlankLine();
263+
if (wroteAnythingSignificant) {
264+
requestBlankLine();
265+
}
264266

265267
writeToken(token);
266268

267269
requestBlankLine();
268270
}
269271

270272
void writePreOpen(PreOpenTag token) {
271-
requestBlankLine();
273+
if (wroteAnythingSignificant) {
274+
requestBlankLine();
275+
}
272276

273277
writeToken(token);
274278
}
@@ -288,7 +292,9 @@ void writeCodeClose(CodeCloseTag token) {
288292
}
289293

290294
void writeTableOpen(TableOpenTag token) {
291-
requestBlankLine();
295+
if (wroteAnythingSignificant) {
296+
requestBlankLine();
297+
}
292298

293299
writeToken(token);
294300
}

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,14 +2160,100 @@ public void tableAtStartOfComment() {
21602160
/// </table>
21612161
class Test {}
21622162
""";
2163-
// TODO(kak): we shouldn't spew out these 2 leading blank lines
2163+
String expected = input;
2164+
doFormatTest(input, expected);
2165+
}
2166+
2167+
@Test
2168+
public void blockquoteAtStartOfComment() {
2169+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2170+
String input =
2171+
"""
2172+
/// <blockquote>
2173+
/// line 1
2174+
/// line 2
2175+
/// </blockquote>
2176+
class Test {}
2177+
""";
21642178
String expected =
21652179
"""
2180+
/// <blockquote>
21662181
///
2182+
/// line 1 line 2
21672183
///
2184+
/// </blockquote>
2185+
class Test {}
2186+
""";
2187+
doFormatTest(input, expected);
2188+
}
2189+
2190+
@Test
2191+
public void preAtStartOfComment() {
2192+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2193+
String input =
2194+
"""
2195+
/// <pre>
2196+
/// line 1
2197+
/// line 2
2198+
/// </pre>
2199+
class Test {}
2200+
""";
2201+
String expected = input;
2202+
doFormatTest(input, expected);
2203+
}
2204+
2205+
@Test
2206+
public void tableInHtmlListItem() {
2207+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2208+
String input =
2209+
"""
2210+
/// <ul>
2211+
/// <li>
21682212
/// <table>
21692213
/// <tr><td>Foo</td></tr>
21702214
/// </table>
2215+
/// </li>
2216+
/// </ul>
2217+
class Test {}
2218+
""";
2219+
// requestBlankLine() in writeTableOpen() inserts a blank line after <li> before <table> when
2220+
// inside a list item.
2221+
String expected =
2222+
"""
2223+
/// <ul>
2224+
/// <li>
2225+
///
2226+
/// <table>
2227+
/// <tr><td>Foo</td></tr>
2228+
/// </table>
2229+
///
2230+
/// </ul>
2231+
class Test {}
2232+
""";
2233+
doFormatTest(input, expected);
2234+
}
2235+
2236+
@Test
2237+
public void tableInsideMarkdownListItemAfterText() {
2238+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2239+
String input =
2240+
"""
2241+
/// - item text
2242+
/// <table>
2243+
/// <tr><td>Foo</td></tr>
2244+
/// </table>
2245+
class Test {}
2246+
""";
2247+
// requestBlankLine() in writeTableOpen() inserts a blank line after item text before <table>
2248+
// inside the list item.
2249+
String expected =
2250+
"""
2251+
///
2252+
/// - item text
2253+
///
2254+
/// <table>
2255+
/// <tr><td>Foo</td></tr>
2256+
/// </table>
21712257
class Test {}
21722258
""";
21732259
doFormatTest(input, expected);

0 commit comments

Comments
 (0)