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 e27f39b3e..af2f7c806 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 @@ -114,6 +114,7 @@ void writeBeginJavadoc() { } else { output.append("/// "); remainingOnLine = JavadocFormatter.MAX_LINE_LENGTH - blockIndent - 4; + atStartOfLine = true; } } @@ -190,7 +191,7 @@ void writeSnippetEnd(SnippetEnd token) { } void writeListOpen(ListOpenTag token) { - if (classicJavadoc) { + if (classicJavadoc && wroteAnythingSignificant) { requestBlankLine(); } @@ -200,7 +201,9 @@ void writeListOpen(ListOpenTag token) { continuingListStack.push(indent); postWriteModifiedContinuingListStack.push(); - requestNewline(); + if (wroteAnythingSignificant) { + requestNewline(); + } } void writeListClose(ListCloseTag token) { @@ -219,7 +222,9 @@ void writeListClose(ListCloseTag token) { } void writeListItemOpen(ListItemOpenTag token) { - requestNewline(); + if (wroteAnythingSignificant) { + requestNewline(); + } if (continuingListItemOfInnermostList) { continuingListItemOfInnermostList = false; @@ -260,9 +265,7 @@ void writeParagraphOpen(Token token) { } void writeBlockquoteOpenOrClose(Token token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLineForBlockTag(); writeToken(token); @@ -270,9 +273,7 @@ void writeBlockquoteOpenOrClose(Token token) { } void writePreOpen(PreOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLineForBlockTag(); writeToken(token); } @@ -292,9 +293,7 @@ void writeCodeClose(CodeCloseTag token) { } void writeTableOpen(TableOpenTag token) { - if (wroteAnythingSignificant) { - requestBlankLine(); - } + requestBlankLineForBlockTag(); writeToken(token); } @@ -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 @@ -363,9 +360,7 @@ void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) { } void writeMarkdownTable(MarkdownTable token) { - if (wroteAnythingSignificant && !atStartOfLine) { - requestBlankLine(); - } + requestBlankLineForBlockTag(); flushWhitespace(); List lines = token.value().lines().toList(); output.append(lines.get(0)); @@ -385,6 +380,12 @@ private void requestBlankLine() { requestWhitespace(BLANK_LINE); } + private void requestBlankLineForBlockTag() { + if (wroteAnythingSignificant && !atStartOfLine) { + requestBlankLine(); + } + } + private void requestNewline() { requestWhitespace(NEWLINE); } diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java index 088f5ea58..d413456b5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java @@ -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) { 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 7083a878d..24c882a0a 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -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 {} @@ -2216,14 +2214,12 @@ public void tableInHtmlListItem() { /// class Test {} """; - // requestBlankLine() in writeTableOpen() inserts a blank line after
  • before when - // inside a list item. + // TODO(kak): The indentation here is still wrong (`` and `
    ` should be indented + // inside `
  • `). Also the blank line before should probably not be there? String expected = """ ///