Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void writeBeginJavadoc() {
} else {
output.append("/// ");
remainingOnLine = JavadocFormatter.MAX_LINE_LENGTH - blockIndent - 4;
atStartOfLine = true;
}
}

Expand Down Expand Up @@ -190,7 +191,7 @@ void writeSnippetEnd(SnippetEnd token) {
}

void writeListOpen(ListOpenTag token) {
if (classicJavadoc) {
if (classicJavadoc && wroteAnythingSignificant) {
requestBlankLine();
}

Expand All @@ -200,7 +201,9 @@ void writeListOpen(ListOpenTag token) {
continuingListStack.push(indent);
postWriteModifiedContinuingListStack.push();

requestNewline();
if (wroteAnythingSignificant) {
requestNewline();
}
}

void writeListClose(ListCloseTag token) {
Expand All @@ -219,7 +222,9 @@ void writeListClose(ListCloseTag token) {
}

void writeListItemOpen(ListItemOpenTag token) {
requestNewline();
if (wroteAnythingSignificant) {
requestNewline();
}

if (continuingListItemOfInnermostList) {
continuingListItemOfInnermostList = false;
Expand Down Expand Up @@ -260,19 +265,15 @@ void writeParagraphOpen(Token token) {
}

void writeBlockquoteOpenOrClose(Token token) {
if (wroteAnythingSignificant) {
requestBlankLine();
}
requestBlankLineForBlockTag();

writeToken(token);

requestBlankLine();
}

void writePreOpen(PreOpenTag token) {
if (wroteAnythingSignificant) {
requestBlankLine();
}
requestBlankLineForBlockTag();

writeToken(token);
}
Expand All @@ -292,9 +293,7 @@ void writeCodeClose(CodeCloseTag token) {
}

void writeTableOpen(TableOpenTag token) {
if (wroteAnythingSignificant) {
requestBlankLine();
}
requestBlankLineForBlockTag();

writeToken(token);
}
Expand Down Expand Up @@ -343,10 +342,8 @@ void writeLiteral(Literal token) {
}

void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
if (wroteAnythingSignificant && !atStartOfLine) {
// A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken.
requestBlankLine();
}
// A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken.
requestBlankLineForBlockTag();
flushWhitespace();
output.append(token.start());
token
Expand All @@ -363,9 +360,7 @@ void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
}

void writeMarkdownTable(MarkdownTable token) {
if (wroteAnythingSignificant && !atStartOfLine) {
requestBlankLine();
}
requestBlankLineForBlockTag();
flushWhitespace();
List<String> lines = token.value().lines().toList();
output.append(lines.get(0));
Expand All @@ -385,6 +380,12 @@ private void requestBlankLine() {
requestWhitespace(BLANK_LINE);
}

private void requestBlankLineForBlockTag() {
if (wroteAnythingSignificant && !atStartOfLine) {
requestBlankLine();
}
}

private void requestNewline() {
requestWhitespace(NEWLINE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ private boolean visitListItem(ListItem listItem) {
verify(matcher.lookingAt());
ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1));
addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
return switch (listItem.getFirstChild()) {
case Paragraph paragraph -> {
// A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
// because that would lead us to introduce a line break after the list introduction
// (the `-` or whatever). So we visit the children and siblings of the Paragraph instead.
visitNodeList(paragraph.getFirstChild());
visitNodeList(paragraph.getNext());
yield true;
}
default -> false;
};
Node firstChild = listItem.getFirstChild();
// Safely handles null and non-Paragraph children.
if (firstChild instanceof Paragraph paragraph) {
// A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
// because that would lead us to introduce a line break after the list introduction
// (the `-` or whatever). So we visit the children and siblings of the Paragraph instead.
visitNodeList(paragraph.getFirstChild());
visitNodeList(paragraph.getNext());
return true;
}
return false;
}

private void visitHeading(Heading heading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1940,11 +1940,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 {}
Expand Down Expand Up @@ -2216,14 +2214,12 @@ public void tableInHtmlListItem() {
/// </ul>
class Test {}
""";
// requestBlankLine() in writeTableOpen() inserts a blank line after <li> before <table> when
// inside a list item.
// TODO(kak): The indentation here is still wrong (`<tr>` and `</table>` should be indented
// inside `<li>`). Also the blank line before </ul> should probably not be there?
String expected =
"""
/// <ul>
/// <li>
///
/// <table>
/// <li><table>
/// <tr><td>Foo</td></tr>
/// </table>
///
Expand All @@ -2233,6 +2229,20 @@ class Test {}
doFormatTest(input, expected);
}

@Test
public void tableInMarkdownListItem() {
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
String input =
"""
/// - <table>
/// <tr><td>Foo</td></tr>
/// </table>
class Test {}
""";
String expected = input;
doFormatTest(input, expected);
}

@Test
public void tableInsideMarkdownListItemAfterText() {
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
Expand All @@ -2244,11 +2254,8 @@ public void tableInsideMarkdownListItemAfterText() {
/// </table>
class Test {}
""";
// requestBlankLine() in writeTableOpen() inserts a blank line after item text before <table>
// inside the list item.
String expected =
"""
///
/// - item text
///
/// <table>
Expand Down
Loading