Skip to content
Merged
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 @@ -81,21 +81,17 @@ private static class TokenVisitor {
void visit(Node node) {
boolean alreadyVisitedChildren = false;
switch (node) {
case Heading heading ->
addSpan(positionToToken, heading, HEADER_OPEN_TOKEN, HEADER_CLOSE_TOKEN);
case Paragraph paragraph ->
addSpan(positionToToken, paragraph, PARAGRAPH_OPEN_TOKEN, PARAGRAPH_CLOSE_TOKEN);
case BulletList bulletList ->
addSpan(positionToToken, bulletList, LIST_OPEN_TOKEN, LIST_CLOSE_TOKEN);
case OrderedList orderedList ->
addSpan(positionToToken, orderedList, LIST_OPEN_TOKEN, LIST_CLOSE_TOKEN);
case Heading heading -> addSpan(heading, HEADER_OPEN_TOKEN, HEADER_CLOSE_TOKEN);
case Paragraph paragraph -> addSpan(paragraph, PARAGRAPH_OPEN_TOKEN, PARAGRAPH_CLOSE_TOKEN);
case BulletList bulletList -> addSpan(bulletList, LIST_OPEN_TOKEN, LIST_CLOSE_TOKEN);
case OrderedList orderedList -> addSpan(orderedList, LIST_OPEN_TOKEN, LIST_CLOSE_TOKEN);
case ListItem listItem -> {
int startPosition = listItem.getSourceSpans().getFirst().getInputIndex();
Matcher matcher =
LIST_ITEM_START_PATTERN.matcher(input).region(startPosition, input.length());
verify(matcher.lookingAt());
ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1));
addSpan(positionToToken, listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
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 @@ -123,26 +119,25 @@ private void visitNodeList(Node node) {
visit(node);
}
}
}

/**
* Adds tokens for the given node, {@code startToken} at the point where the node starts in the
* input, and {@code endToken} at the point where it ends. The {@code startToken} goes after any
* other tokens at that position and the {@code endToken} goes before any other tokens at that
* position. That reflects the structure. For example, at the start of a bullet list, the visitor
* we will translate this into {@link ListOpenTag} then {@link ListItemOpenTag} at the start
* position, and {@link ListItemCloseTag} then {@link ListCloseTag} (in that order) at the end
* position.
*/
private static void addSpan(
ListMultimap<Integer, Token> positionToToken, Node node, Token startToken, Token endToken) {
// We could write the first part more simply as a `put`, but we do it this way for symmetry.
var first = node.getSourceSpans().getFirst();
int startPosition = first.getInputIndex();
positionToToken.get(startPosition).addLast(startToken);
var last = node.getSourceSpans().getLast();
int endPosition = last.getInputIndex() + last.getLength();
positionToToken.get(endPosition).addFirst(endToken);
/**
* Adds tokens for the given node, {@code startToken} at the point where the node starts in the
* input, and {@code endToken} at the point where it ends. The {@code startToken} goes after any
* other tokens at that position and the {@code endToken} goes before any other tokens at that
* position. That reflects the structure. For example, at the start of a bullet list, the
* visitor we will translate this into {@link ListOpenTag} then {@link ListItemOpenTag} at the
* start position, and {@link ListItemCloseTag} then {@link ListCloseTag} (in that order) at the
* end position.
*/
private void addSpan(Node node, Token startToken, Token endToken) {
// We could write the first part more simply as a `put`, but we do it this way for symmetry.
var first = node.getSourceSpans().getFirst();
int startPosition = first.getInputIndex();
positionToToken.get(startPosition).addLast(startToken);
var last = node.getSourceSpans().getLast();
int endPosition = last.getInputIndex() + last.getLength();
positionToToken.get(endPosition).addFirst(endToken);
}
}

@Override
Expand Down
Loading