From ffd5425ca1039ff581954427d008de133b25b7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Sat, 4 Apr 2026 10:29:20 -0700 Subject: [PATCH] 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: 894607945 --- .../java/javadoc/MarkdownPositions.java | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java index f66d79904..b83ec72b2 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java @@ -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 @@ -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 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