|
42 | 42 | import static com.google.googlejavaformat.java.javadoc.Token.Type.PARAGRAPH_OPEN_TAG;
|
43 | 43 | import static com.google.googlejavaformat.java.javadoc.Token.Type.PRE_CLOSE_TAG;
|
44 | 44 | import static com.google.googlejavaformat.java.javadoc.Token.Type.PRE_OPEN_TAG;
|
| 45 | +import static com.google.googlejavaformat.java.javadoc.Token.Type.SNIPPET_BEGIN; |
| 46 | +import static com.google.googlejavaformat.java.javadoc.Token.Type.SNIPPET_END; |
45 | 47 | import static com.google.googlejavaformat.java.javadoc.Token.Type.TABLE_CLOSE_TAG;
|
46 | 48 | import static com.google.googlejavaformat.java.javadoc.Token.Type.TABLE_OPEN_TAG;
|
47 | 49 | import static com.google.googlejavaformat.java.javadoc.Token.Type.WHITESPACE;
|
@@ -97,6 +99,7 @@ private static String stripJavadocBeginAndEnd(String input) {
|
97 | 99 | private final NestingCounter preDepth = new NestingCounter();
|
98 | 100 | private final NestingCounter codeDepth = new NestingCounter();
|
99 | 101 | private final NestingCounter tableDepth = new NestingCounter();
|
| 102 | + private boolean outerInlineTagIsSnippet; |
100 | 103 | private boolean somethingSinceNewline;
|
101 | 104 |
|
102 | 105 | private JavadocLexer(CharStream input) {
|
@@ -158,13 +161,26 @@ private Type consumeToken() throws LexException {
|
158 | 161 | }
|
159 | 162 | somethingSinceNewline = true;
|
160 | 163 |
|
161 |
| - if (input.tryConsumeRegex(INLINE_TAG_OPEN_PATTERN)) { |
| 164 | + if (input.tryConsumeRegex(SNIPPET_TAG_OPEN_PATTERN)) { |
| 165 | + if (braceDepth.value() == 0) { |
| 166 | + braceDepth.increment(); |
| 167 | + outerInlineTagIsSnippet = true; |
| 168 | + return SNIPPET_BEGIN; |
| 169 | + } |
| 170 | + braceDepth.increment(); |
| 171 | + return LITERAL; |
| 172 | + } else if (input.tryConsumeRegex(INLINE_TAG_OPEN_PATTERN)) { |
162 | 173 | braceDepth.increment();
|
163 | 174 | return LITERAL;
|
164 | 175 | } else if (input.tryConsume("{")) {
|
165 | 176 | braceDepth.incrementIfPositive();
|
166 | 177 | return LITERAL;
|
167 | 178 | } else if (input.tryConsume("}")) {
|
| 179 | + if (outerInlineTagIsSnippet && braceDepth.value() == 1) { |
| 180 | + braceDepth.decrementIfPositive(); |
| 181 | + outerInlineTagIsSnippet = false; |
| 182 | + return SNIPPET_END; |
| 183 | + } |
168 | 184 | braceDepth.decrementIfPositive();
|
169 | 185 | return LITERAL;
|
170 | 186 | }
|
@@ -239,7 +255,10 @@ private Type consumeToken() throws LexException {
|
239 | 255 | }
|
240 | 256 |
|
241 | 257 | private boolean preserveExistingFormatting() {
|
242 |
| - return preDepth.isPositive() || tableDepth.isPositive() || codeDepth.isPositive(); |
| 258 | + return preDepth.isPositive() |
| 259 | + || tableDepth.isPositive() |
| 260 | + || codeDepth.isPositive() |
| 261 | + || outerInlineTagIsSnippet; |
243 | 262 | }
|
244 | 263 |
|
245 | 264 | private void checkMatchingTags() throws LexException {
|
@@ -400,6 +419,7 @@ private static ImmutableList<Token> optionalizeSpacesAfterLinks(List<Token> inpu
|
400 | 419 | * <p>Also trim leading and trailing blank lines, and move the trailing `}` to its own line.
|
401 | 420 | */
|
402 | 421 | private static ImmutableList<Token> deindentPreCodeBlocks(List<Token> input) {
|
| 422 | + // TODO: b/323389829 - De-indent {@snippet ...} blocks, too. |
403 | 423 | ImmutableList.Builder<Token> output = ImmutableList.builder();
|
404 | 424 | for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
|
405 | 425 | if (tokens.peek().getType() != PRE_OPEN_TAG) {
|
@@ -528,6 +548,7 @@ private static boolean hasMultipleNewlines(String s) {
|
528 | 548 | private static final Pattern BLOCKQUOTE_OPEN_PATTERN = openTagPattern("blockquote");
|
529 | 549 | private static final Pattern BLOCKQUOTE_CLOSE_PATTERN = closeTagPattern("blockquote");
|
530 | 550 | private static final Pattern BR_PATTERN = openTagPattern("br");
|
| 551 | + private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile("^[{]@snippet\\b"); |
531 | 552 | private static final Pattern INLINE_TAG_OPEN_PATTERN = compile("^[{]@\\w*");
|
532 | 553 | /*
|
533 | 554 | * We exclude < so that we don't swallow following HTML tags. This lets us fix up "foo<p>" (~400
|
|
0 commit comments