@@ -81,21 +81,17 @@ private static class TokenVisitor {
8181 void visit (Node node ) {
8282 boolean alreadyVisitedChildren = false ;
8383 switch (node ) {
84- case Heading heading ->
85- addSpan (positionToToken , heading , HEADER_OPEN_TOKEN , HEADER_CLOSE_TOKEN );
86- case Paragraph paragraph ->
87- addSpan (positionToToken , paragraph , PARAGRAPH_OPEN_TOKEN , PARAGRAPH_CLOSE_TOKEN );
88- case BulletList bulletList ->
89- addSpan (positionToToken , bulletList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
90- case OrderedList orderedList ->
91- addSpan (positionToToken , orderedList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
84+ case Heading heading -> addSpan (heading , HEADER_OPEN_TOKEN , HEADER_CLOSE_TOKEN );
85+ case Paragraph paragraph -> addSpan (paragraph , PARAGRAPH_OPEN_TOKEN , PARAGRAPH_CLOSE_TOKEN );
86+ case BulletList bulletList -> addSpan (bulletList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
87+ case OrderedList orderedList -> addSpan (orderedList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
9288 case ListItem listItem -> {
9389 int startPosition = listItem .getSourceSpans ().getFirst ().getInputIndex ();
9490 Matcher matcher =
9591 LIST_ITEM_START_PATTERN .matcher (input ).region (startPosition , input .length ());
9692 verify (matcher .lookingAt ());
97- Token openToken = new ListItemOpenTag (matcher .group (1 ));
98- addSpan (positionToToken , listItem , openToken , LIST_ITEM_CLOSE_TOKEN );
93+ ListItemOpenTag openToken = new ListItemOpenTag (matcher .group (1 ));
94+ addSpan (listItem , openToken , LIST_ITEM_CLOSE_TOKEN );
9995 if (listItem .getFirstChild () instanceof Paragraph paragraph ) {
10096 // A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph
10197 // because that would lead us to introduce a line break after the list introduction
@@ -123,26 +119,25 @@ private void visitNodeList(Node node) {
123119 visit (node );
124120 }
125121 }
126- }
127122
128- /**
129- * Adds tokens for the given node, {@code startToken} at the point where the node starts in the
130- * input, and {@code endToken} at the point where it ends. The {@code startToken} goes after any
131- * other tokens at that position and the {@code endToken} goes before any other tokens at that
132- * position. That reflects the structure. For example, at the start of a bullet list, the visitor
133- * we will translate this into {@link ListOpenTag} then {@link ListItemOpenTag} at the start
134- * position, and {@link ListItemCloseTag} then {@link ListCloseTag} (in that order) at the end
135- * position.
136- */
137- private static void addSpan (
138- ListMultimap < Integer , Token > positionToToken , Node node , Token startToken , Token endToken ) {
139- // We could write the first part more simply as a `put`, but we do it this way for symmetry.
140- var first = node . getSourceSpans (). getFirst ();
141- int startPosition = first . getInputIndex ( );
142- positionToToken . get ( startPosition ). addLast ( startToken );
143- var last = node . getSourceSpans (). getLast ();
144- int endPosition = last . getInputIndex () + last . getLength ( );
145- positionToToken . get ( endPosition ). addFirst ( endToken );
123+ /**
124+ * Adds tokens for the given node, {@code startToken} at the point where the node starts in the
125+ * input, and {@code endToken} at the point where it ends. The {@code startToken} goes after any
126+ * other tokens at that position and the {@code endToken} goes before any other tokens at that
127+ * position. That reflects the structure. For example, at the start of a bullet list, the
128+ * visitor we will translate this into {@link ListOpenTag} then {@link ListItemOpenTag} at the
129+ * start position, and {@link ListItemCloseTag} then {@link ListCloseTag} (in that order) at the
130+ * end position.
131+ */
132+ private void addSpan (Node node , Token startToken , Token endToken ) {
133+ // We could write the first part more simply as a `put`, but we do it this way for symmetry.
134+ var first = node . getSourceSpans (). getFirst ();
135+ int startPosition = first . getInputIndex ();
136+ positionToToken . get ( startPosition ). addLast ( startToken );
137+ var last = node . getSourceSpans (). getLast ( );
138+ int endPosition = last . getInputIndex () + last . getLength ();
139+ positionToToken . get ( endPosition ). addFirst ( endToken );
140+ }
146141 }
147142
148143 @ Override
@@ -153,13 +148,13 @@ public String toString() {
153148 private static final Parser PARSER =
154149 Parser .builder ().includeSourceSpans (IncludeSourceSpans .BLOCKS_AND_INLINES ).build ();
155150
156- private static final Token HEADER_OPEN_TOKEN = new HeaderOpenTag ("" );
157- private static final Token HEADER_CLOSE_TOKEN = new HeaderCloseTag ("" );
158- private static final Token PARAGRAPH_OPEN_TOKEN = new ParagraphOpenTag ("" );
159- private static final Token PARAGRAPH_CLOSE_TOKEN = new ParagraphCloseTag ("" );
160- private static final Token LIST_OPEN_TOKEN = new ListOpenTag ("" );
161- private static final Token LIST_CLOSE_TOKEN = new ListCloseTag ("" );
162- private static final Token LIST_ITEM_CLOSE_TOKEN = new ListItemCloseTag ("" );
151+ private static final HeaderOpenTag HEADER_OPEN_TOKEN = new HeaderOpenTag ("" );
152+ private static final HeaderCloseTag HEADER_CLOSE_TOKEN = new HeaderCloseTag ("" );
153+ private static final ParagraphOpenTag PARAGRAPH_OPEN_TOKEN = new ParagraphOpenTag ("" );
154+ private static final ParagraphCloseTag PARAGRAPH_CLOSE_TOKEN = new ParagraphCloseTag ("" );
155+ private static final ListOpenTag LIST_OPEN_TOKEN = new ListOpenTag ("" );
156+ private static final ListCloseTag LIST_CLOSE_TOKEN = new ListCloseTag ("" );
157+ private static final ListItemCloseTag LIST_ITEM_CLOSE_TOKEN = new ListItemCloseTag ("" );
163158
164159 // The leading \s here works around what appears to be a CommonMark bug. We shouldn't ever see
165160 // space at the purported start of a list item?
0 commit comments