Skip to content

Commit 140839c

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 e9e9d5f commit 140839c

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,20 @@ private static String render(List<Token> input, int blockIndent, boolean classic
144144
* should include them as part of its own postprocessing? Or even the writer could make sense.
145145
*/
146146

147-
private static Token standardizeBrToken(Token token) {
147+
private static BrTag standardizeBrToken(BrTag token) {
148148
return standardize(token, STANDARD_BR_TOKEN);
149149
}
150150

151-
private static Token standardizePToken(Token token) {
151+
private static ParagraphOpenTag standardizePToken(ParagraphOpenTag token) {
152152
return standardize(token, STANDARD_P_TOKEN);
153153
}
154154

155-
private static Token standardize(Token token, Token standardToken) {
155+
private static <T extends Token> T standardize(T token, T standardToken) {
156156
return SIMPLE_TAG_PATTERN.matcher(token.value()).matches() ? standardToken : token;
157157
}
158158

159-
private static final Token STANDARD_BR_TOKEN = new BrTag("<br>");
160-
private static final Token STANDARD_P_TOKEN = new ParagraphOpenTag("<p>");
159+
private static final BrTag STANDARD_BR_TOKEN = new BrTag("<br>");
160+
private static final ParagraphOpenTag STANDARD_P_TOKEN = new ParagraphOpenTag("<p>");
161161
private static final Pattern SIMPLE_TAG_PATTERN = compile("^<\\w+\\s*/?\\s*>", CASE_INSENSITIVE);
162162

163163
private static final Pattern ONE_CONTENT_LINE_PATTERN = compile(" */[*][*]\n *[*] (.*)\n *[*]/");

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,25 @@
2323
import static com.google.googlejavaformat.java.javadoc.JavadocWriter.RequestedWhitespace.NONE;
2424
import static com.google.googlejavaformat.java.javadoc.JavadocWriter.RequestedWhitespace.WHITESPACE;
2525

26+
import com.google.googlejavaformat.java.javadoc.Token.CodeCloseTag;
27+
import com.google.googlejavaformat.java.javadoc.Token.CodeOpenTag;
28+
import com.google.googlejavaformat.java.javadoc.Token.FooterJavadocTagStart;
29+
import com.google.googlejavaformat.java.javadoc.Token.HeaderCloseTag;
30+
import com.google.googlejavaformat.java.javadoc.Token.HeaderOpenTag;
31+
import com.google.googlejavaformat.java.javadoc.Token.HtmlComment;
32+
import com.google.googlejavaformat.java.javadoc.Token.ListCloseTag;
33+
import com.google.googlejavaformat.java.javadoc.Token.ListItemOpenTag;
34+
import com.google.googlejavaformat.java.javadoc.Token.ListOpenTag;
35+
import com.google.googlejavaformat.java.javadoc.Token.Literal;
36+
import com.google.googlejavaformat.java.javadoc.Token.MoeBeginStripComment;
37+
import com.google.googlejavaformat.java.javadoc.Token.MoeEndStripComment;
38+
import com.google.googlejavaformat.java.javadoc.Token.PreCloseTag;
39+
import com.google.googlejavaformat.java.javadoc.Token.PreOpenTag;
40+
import com.google.googlejavaformat.java.javadoc.Token.SnippetBegin;
41+
import com.google.googlejavaformat.java.javadoc.Token.SnippetEnd;
2642
import com.google.googlejavaformat.java.javadoc.Token.StartOfLineToken;
43+
import com.google.googlejavaformat.java.javadoc.Token.TableCloseTag;
44+
import com.google.googlejavaformat.java.javadoc.Token.TableOpenTag;
2745

2846
/**
2947
* Stateful object that accepts "requests" and "writes," producing formatted Javadoc.
@@ -73,7 +91,7 @@ private void requestWhitespace(RequestedWhitespace requestedWhitespace) {
7391
this.requestedWhitespace = max(requestedWhitespace, this.requestedWhitespace);
7492
}
7593

76-
void requestMoeBeginStripComment(Token token) {
94+
void requestMoeBeginStripComment(MoeBeginStripComment token) {
7795
// We queue this up so that we can put it after any requested whitespace.
7896
requestedMoeBeginStripComment = checkNotNull(token);
7997
}
@@ -100,7 +118,7 @@ void writeEndJavadoc() {
100118
}
101119
}
102120

103-
void writeFooterJavadocTagStart(Token token) {
121+
void writeFooterJavadocTagStart(FooterJavadocTagStart token) {
104122
// Close any unclosed lists (e.g., <li> without <ul>).
105123
// TODO(cpovirk): Actually generate </ul>, etc.?
106124
/*
@@ -132,7 +150,7 @@ void writeFooterJavadocTagStart(Token token) {
132150
continuingFooterTag = true;
133151
}
134152

135-
void writeSnippetBegin(Token token) {
153+
void writeSnippetBegin(SnippetBegin token) {
136154
requestBlankLine();
137155
writeToken(token);
138156
/*
@@ -146,7 +164,7 @@ void writeSnippetBegin(Token token) {
146164
*/
147165
}
148166

149-
void writeSnippetEnd(Token token) {
167+
void writeSnippetEnd(SnippetEnd token) {
150168
/*
151169
* We don't request a newline here because we have preserved all newlines that existed in the
152170
* input. TODO: b/323389829 - Improve upon that. Specifically:
@@ -164,7 +182,7 @@ void writeSnippetEnd(Token token) {
164182
requestBlankLine();
165183
}
166184

167-
void writeListOpen(Token token) {
185+
void writeListOpen(ListOpenTag token) {
168186
if (classicJavadoc) {
169187
requestBlankLine();
170188
}
@@ -178,7 +196,7 @@ void writeListOpen(Token token) {
178196
requestNewline();
179197
}
180198

181-
void writeListClose(Token token) {
199+
void writeListClose(ListCloseTag token) {
182200
if (classicJavadoc) {
183201
requestNewline();
184202
}
@@ -193,7 +211,7 @@ void writeListClose(Token token) {
193211
}
194212
}
195213

196-
void writeListItemOpen(Token token) {
214+
void writeListItemOpen(ListItemOpenTag token) {
197215
requestNewline();
198216

199217
if (continuingListItemOfInnermostList) {
@@ -206,15 +224,15 @@ void writeListItemOpen(Token token) {
206224
continuingListItemStack.push(indent);
207225
}
208226

209-
void writeHeaderOpen(Token token) {
227+
void writeHeaderOpen(HeaderOpenTag token) {
210228
if (wroteAnythingSignificant) {
211229
requestBlankLine();
212230
}
213231

214232
writeToken(token);
215233
}
216234

217-
void writeHeaderClose(Token token) {
235+
void writeHeaderClose(HeaderCloseTag token) {
218236
writeToken(token);
219237

220238
requestBlankLine();
@@ -242,39 +260,39 @@ void writeBlockquoteOpenOrClose(Token token) {
242260
requestBlankLine();
243261
}
244262

245-
void writePreOpen(Token token) {
263+
void writePreOpen(PreOpenTag token) {
246264
requestBlankLine();
247265

248266
writeToken(token);
249267
}
250268

251-
void writePreClose(Token token) {
269+
void writePreClose(PreCloseTag token) {
252270
writeToken(token);
253271

254272
requestBlankLine();
255273
}
256274

257-
void writeCodeOpen(Token token) {
275+
void writeCodeOpen(CodeOpenTag token) {
258276
writeToken(token);
259277
}
260278

261-
void writeCodeClose(Token token) {
279+
void writeCodeClose(CodeCloseTag token) {
262280
writeToken(token);
263281
}
264282

265-
void writeTableOpen(Token token) {
283+
void writeTableOpen(TableOpenTag token) {
266284
requestBlankLine();
267285

268286
writeToken(token);
269287
}
270288

271-
void writeTableClose(Token token) {
289+
void writeTableClose(TableCloseTag token) {
272290
writeToken(token);
273291

274292
requestBlankLine();
275293
}
276294

277-
void writeMoeEndStripComment(Token token) {
295+
void writeMoeEndStripComment(MoeEndStripComment token) {
278296
writeLineBreakNoAutoIndent();
279297
appendSpaces(indentForMoeEndStripComment);
280298

@@ -284,7 +302,7 @@ void writeMoeEndStripComment(Token token) {
284302
requestNewline();
285303
}
286304

287-
void writeHtmlComment(Token token) {
305+
void writeHtmlComment(HtmlComment token) {
288306
requestNewline();
289307

290308
writeToken(token);
@@ -302,7 +320,7 @@ void writeLineBreakNoAutoIndent() {
302320
writeNewline(NO_AUTO_INDENT);
303321
}
304322

305-
void writeLiteral(Token token) {
323+
void writeLiteral(Literal token) {
306324
writeToken(token);
307325
}
308326

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

Lines changed: 31 additions & 36 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());
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

Comments
 (0)