Skip to content
Merged
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 @@ -129,17 +129,15 @@ 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;
};
if (listItem.getFirstChild() 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 Expand Up @@ -258,5 +256,5 @@ public String toString() {
// The leading \s here works around what appears to be a CommonMark bug. We shouldn't ever see
// space at the purported start of a list item?
private static final Pattern LIST_ITEM_START_PATTERN =
Pattern.compile("(?:\\s*)(([-+*]|[0-9]+[.)])\\s)");
Pattern.compile("(?:\\s*)(([-+*]|[0-9]+[.)])(?:\\s|$))");
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public final class JavadocFormattingTest {

private final Formatter formatter = new Formatter();

/**
* Tests that the formatter formats the given input string to the given expected string. Also
* tests that the formatter is idempotent when formatting an already formatted string.
*/
private void doFormatTest(String input, String expected) {
try {
String actual = formatter.formatSource(input);
assertThat(actual).isEqualTo(expected);
String reformatted = formatter.formatSource(actual);
assertWithMessage("When checking idempotency").that(reformatted).isEqualTo(actual);
} catch (FormatterException e) {
throw new AssertionError(e);
}
}

@Test
public void notJavadoc() {
String input =
Expand Down Expand Up @@ -1739,6 +1754,21 @@ class Test {}
doFormatTest(input, expected);
}

@Test
public void markdownEmptyListItem() {
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
String input =
"""
/// A list with an empty item:
/// - `foo`: enabled by default
/// - `bar`: disabled by default
/// -
class Test {}
""";
String expected = input;
doFormatTest(input, expected);
}

@Test
public void markdownFencedCodeBlocks() {
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
Expand Down Expand Up @@ -2066,17 +2096,6 @@ class Test {}
doFormatTest(input, expected);
}

private void doFormatTest(String input, String expected) {
try {
String actual = formatter.formatSource(input);
assertThat(actual).isEqualTo(expected);
String reformatted = formatter.formatSource(actual);
assertWithMessage("When checking idempotency").that(reformatted).isEqualTo(actual);
} catch (FormatterException e) {
throw new AssertionError(e);
}
}

@Test
public void markdownBlankLinesAroundSnippetAndNoMangling() {
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
Expand Down
Loading