1313 */
1414package com .google .googlejavaformat .java .javadoc ;
1515
16+ import static com .google .common .base .Verify .verify ;
17+
1618import com .google .common .collect .ArrayListMultimap ;
1719import com .google .common .collect .ImmutableList ;
1820import com .google .common .collect .ImmutableListMultimap ;
1921import com .google .common .collect .ListMultimap ;
22+ import java .util .regex .Matcher ;
23+ import java .util .regex .Pattern ;
2024import org .commonmark .node .BulletList ;
2125import org .commonmark .node .Heading ;
2226import org .commonmark .node .ListItem ;
2327import org .commonmark .node .Node ;
28+ import org .commonmark .node .OrderedList ;
2429import org .commonmark .node .Paragraph ;
2530import org .commonmark .parser .IncludeSourceSpans ;
2631import org .commonmark .parser .Parser ;
@@ -50,7 +55,7 @@ private MarkdownPositions(ImmutableListMultimap<Integer, Token> positionToToken)
5055 static MarkdownPositions parse (String input ) {
5156 Node document = PARSER .parse (input );
5257 ListMultimap <Integer , Token > positionToToken = ArrayListMultimap .create ();
53- new TokenVisitor (positionToToken ).visit (document );
58+ new TokenVisitor (input , positionToToken ).visit (document );
5459 return new MarkdownPositions (ImmutableListMultimap .copyOf (positionToToken ));
5560 }
5661
@@ -59,9 +64,11 @@ ImmutableList<Token> tokensAt(int position) {
5964 }
6065
6166 private static class TokenVisitor {
67+ private final String input ;
6268 private final ListMultimap <Integer , Token > positionToToken ;
6369
64- TokenVisitor (ListMultimap <Integer , Token > positionToToken ) {
70+ TokenVisitor (String input , ListMultimap <Integer , Token > positionToToken ) {
71+ this .input = input ;
6572 this .positionToToken = positionToToken ;
6673 }
6774
@@ -74,8 +81,15 @@ void visit(Node node) {
7481 addSpan (positionToToken , paragraph , PARAGRAPH_OPEN_TOKEN , PARAGRAPH_CLOSE_TOKEN );
7582 case BulletList bulletList ->
7683 addSpan (positionToToken , bulletList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
84+ case OrderedList orderedList ->
85+ addSpan (positionToToken , orderedList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
7786 case ListItem listItem -> {
78- addSpan (positionToToken , listItem , LIST_ITEM_OPEN_TOKEN , LIST_ITEM_CLOSE_TOKEN );
87+ int startPosition = listItem .getSourceSpans ().getFirst ().getInputIndex ();
88+ Matcher matcher =
89+ LIST_ITEM_START_PATTERN .matcher (input ).region (startPosition , input .length ());
90+ verify (matcher .lookingAt ());
91+ Token openToken = new Token (Token .Type .LIST_ITEM_OPEN_TAG , matcher .group (1 ));
92+ addSpan (positionToToken , listItem , openToken , LIST_ITEM_CLOSE_TOKEN );
7993 if (listItem .getFirstChild () instanceof Paragraph paragraph ) {
8094 // A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
8195 // because that would lead us to introduce a line break after the list introduction
@@ -133,12 +147,17 @@ public String toString() {
133147
134148 private static final Parser PARSER =
135149 Parser .builder ().includeSourceSpans (IncludeSourceSpans .BLOCKS_AND_INLINES ).build ();
150+
136151 private static final Token HEADER_OPEN_TOKEN = new Token (Token .Type .HEADER_OPEN_TAG , "" );
137152 private static final Token HEADER_CLOSE_TOKEN = new Token (Token .Type .HEADER_CLOSE_TAG , "" );
138153 private static final Token PARAGRAPH_OPEN_TOKEN = new Token (Token .Type .PARAGRAPH_OPEN_TAG , "" );
139154 private static final Token PARAGRAPH_CLOSE_TOKEN = new Token (Token .Type .PARAGRAPH_CLOSE_TAG , "" );
140155 private static final Token LIST_OPEN_TOKEN = new Token (Token .Type .LIST_OPEN_TAG , "" );
141156 private static final Token LIST_CLOSE_TOKEN = new Token (Token .Type .LIST_CLOSE_TAG , "" );
142- private static final Token LIST_ITEM_OPEN_TOKEN = new Token (Token .Type .LIST_ITEM_OPEN_TAG , "" );
143157 private static final Token LIST_ITEM_CLOSE_TOKEN = new Token (Token .Type .LIST_ITEM_CLOSE_TAG , "" );
158+
159+ // The leading \s here works around what appears to be a CommonMark bug. We shouldn't ever see
160+ // space at the purported start of a list item?
161+ private static final Pattern LIST_ITEM_START_PATTERN =
162+ Pattern .compile ("(?:\\ s*)((-|\\ *|[0-9]+\\ .)\\ s)" );
144163}
0 commit comments