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 @@ -123,12 +123,13 @@ void visit(Node node) {

// Returns true if this method visited the children of the given ListItem.
private boolean visitListItem(ListItem listItem) {
int startPosition = listItem.getSourceSpans().getFirst().getInputIndex();
int startPosition =
listItem.getSourceSpans().getFirst().getInputIndex() + listItem.getMarkerIndent();
Matcher matcher =
LIST_ITEM_START_PATTERN.matcher(input).region(startPosition, input.length());
verify(matcher.lookingAt());
ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1));
addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN, startPosition);
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
Expand Down Expand Up @@ -215,8 +216,12 @@ private void visitNodeList(Node node) {
* end position.
*/
private void addSpan(Node node, Token startToken, Token endToken) {
addSpan(node, startToken, endToken, startPosition(node));
}

private void addSpan(Node node, Token startToken, Token endToken, int startPosition) {
// We could write the first part more simply as a `put`, but we do it this way for symmetry.
positionToToken.get(startPosition(node)).addLast(startToken);
positionToToken.get(startPosition).addLast(startToken);
positionToToken.get(endPosition(node)).addFirst(endToken);
}

Expand Down Expand Up @@ -253,8 +258,6 @@ public String toString() {
private static final ListCloseTag LIST_CLOSE_TOKEN = new ListCloseTag("");
private static final ListItemCloseTag LIST_ITEM_CLOSE_TOKEN = new ListItemCloseTag("");

// 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("(([-+*]|[0-9]+[.)])(?:\\s|$))");
}
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,26 @@ class Test {}
doFormatTest(input, expected);
}

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

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