Skip to content

Commit 9ce1e82

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Make addSpan an instance method of TokenVisitor.
Since the callers are all inside `TokenVisitor` too, they no longer need to pass the `positionToToken` field. PiperOrigin-RevId: 894319740
1 parent 9552873 commit 9ce1e82

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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());
9793
ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1));
98-
addSpan(positionToToken, listItem, openToken, LIST_ITEM_CLOSE_TOKEN);
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

0 commit comments

Comments
 (0)