Skip to content
Merged
Show file tree
Hide file tree
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 @@ -95,10 +95,10 @@ private static String stripJavadocBeginAndEnd(String input) {
}

private final CharStream input;
private final NestingCounter braceDepth = new NestingCounter();
private final NestingCounter preDepth = new NestingCounter();
private final NestingCounter codeDepth = new NestingCounter();
private final NestingCounter tableDepth = new NestingCounter();
private final NestingStack braceStack = new NestingStack();
private final NestingStack preStack = new NestingStack();
private final NestingStack codeStack = new NestingStack();
private final NestingStack tableStack = new NestingStack();
private boolean outerInlineTagIsSnippet;
private boolean somethingSinceNewline;

Expand Down Expand Up @@ -162,56 +162,56 @@ private Type consumeToken() throws LexException {
somethingSinceNewline = true;

if (input.tryConsumeRegex(SNIPPET_TAG_OPEN_PATTERN)) {
if (braceDepth.value() == 0) {
braceDepth.increment();
if (braceStack.isEmpty()) {
braceStack.push();
outerInlineTagIsSnippet = true;
return SNIPPET_BEGIN;
}
braceDepth.increment();
braceStack.push();
return LITERAL;
} else if (input.tryConsumeRegex(INLINE_TAG_OPEN_PATTERN)) {
braceDepth.increment();
braceStack.push();
return LITERAL;
} else if (input.tryConsume("{")) {
braceDepth.incrementIfPositive();
braceStack.incrementIfPositive();
return LITERAL;
} else if (input.tryConsume("}")) {
if (outerInlineTagIsSnippet && braceDepth.value() == 1) {
braceDepth.decrementIfPositive();
if (outerInlineTagIsSnippet && braceStack.total() == 1) {
braceStack.popIfNotEmpty();
outerInlineTagIsSnippet = false;
return SNIPPET_END;
}
braceDepth.decrementIfPositive();
braceStack.popIfNotEmpty();
return LITERAL;
}

// Inside an inline tag, don't do any HTML interpretation.
if (braceDepth.isPositive()) {
if (!braceStack.isEmpty()) {
verify(input.tryConsumeRegex(LITERAL_PATTERN));
return LITERAL;
}

if (input.tryConsumeRegex(PRE_OPEN_PATTERN)) {
preDepth.increment();
preStack.push();
return preserveExistingFormatting ? LITERAL : PRE_OPEN_TAG;
} else if (input.tryConsumeRegex(PRE_CLOSE_PATTERN)) {
preDepth.decrementIfPositive();
preStack.popIfNotEmpty();
return preserveExistingFormatting() ? LITERAL : PRE_CLOSE_TAG;
}

if (input.tryConsumeRegex(CODE_OPEN_PATTERN)) {
codeDepth.increment();
codeStack.push();
return preserveExistingFormatting ? LITERAL : CODE_OPEN_TAG;
} else if (input.tryConsumeRegex(CODE_CLOSE_PATTERN)) {
codeDepth.decrementIfPositive();
codeStack.popIfNotEmpty();
return preserveExistingFormatting() ? LITERAL : CODE_CLOSE_TAG;
}

if (input.tryConsumeRegex(TABLE_OPEN_PATTERN)) {
tableDepth.increment();
tableStack.push();
return preserveExistingFormatting ? LITERAL : TABLE_OPEN_TAG;
} else if (input.tryConsumeRegex(TABLE_CLOSE_PATTERN)) {
tableDepth.decrementIfPositive();
tableStack.popIfNotEmpty();
return preserveExistingFormatting() ? LITERAL : TABLE_CLOSE_TAG;
}

Expand Down Expand Up @@ -255,17 +255,17 @@ private Type consumeToken() throws LexException {
}

private boolean preserveExistingFormatting() {
return preDepth.isPositive()
|| tableDepth.isPositive()
|| codeDepth.isPositive()
return !preStack.isEmpty()
|| !tableStack.isEmpty()
|| !codeStack.isEmpty()
|| outerInlineTagIsSnippet;
}

private void checkMatchingTags() throws LexException {
if (braceDepth.isPositive()
|| preDepth.isPositive()
|| tableDepth.isPositive()
|| codeDepth.isPositive()) {
if (!braceStack.isEmpty()
|| !preStack.isEmpty()
|| !tableStack.isEmpty()
|| !codeStack.isEmpty()) {
throw new LexException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ final class JavadocWriter {
private boolean continuingListItemOfInnermostList;

private boolean continuingFooterTag;
private final NestingCounter continuingListItemCount = new NestingCounter();
private final NestingCounter continuingListCount = new NestingCounter();
private final NestingCounter postWriteModifiedContinuingListCount = new NestingCounter();
private final NestingStack continuingListItemStack = new NestingStack();
private final NestingStack continuingListStack = new NestingStack();
private final NestingStack postWriteModifiedContinuingListStack = new NestingStack();
private int remainingOnLine;
private boolean atStartOfLine;
private RequestedWhitespace requestedWhitespace = NONE;
Expand Down Expand Up @@ -102,13 +102,13 @@ void writeFooterJavadocTagStart(Token token) {
* currently know which of those tags are open.
*/
continuingListItemOfInnermostList = false;
continuingListItemCount.reset();
continuingListCount.reset();
continuingListItemStack.reset();
continuingListStack.reset();
/*
* There's probably no need for this, since its only effect is to disable blank lines in some
* cases -- and we're doing that already in the footer.
*/
postWriteModifiedContinuingListCount.reset();
postWriteModifiedContinuingListStack.reset();

if (!wroteAnythingSignificant) {
// Javadoc consists solely of tags. This is frowned upon in general but OK for @Overrides.
Expand Down Expand Up @@ -161,19 +161,19 @@ void writeListOpen(Token token) {

writeToken(token);
continuingListItemOfInnermostList = false;
continuingListCount.increment();
postWriteModifiedContinuingListCount.increment();
continuingListStack.push(2);
postWriteModifiedContinuingListStack.push();

requestNewline();
}

void writeListClose(Token token) {
requestNewline();

continuingListItemCount.decrementIfPositive();
continuingListCount.decrementIfPositive();
continuingListItemStack.popIfNotEmpty();
continuingListStack.popIfNotEmpty();
writeToken(token);
postWriteModifiedContinuingListCount.decrementIfPositive();
postWriteModifiedContinuingListStack.popIfNotEmpty();

requestBlankLine();
}
Expand All @@ -183,11 +183,11 @@ void writeListItemOpen(Token token) {

if (continuingListItemOfInnermostList) {
continuingListItemOfInnermostList = false;
continuingListItemCount.decrementIfPositive();
continuingListItemStack.popIfNotEmpty();
}
writeToken(token);
continuingListItemOfInnermostList = true;
continuingListItemCount.increment();
continuingListItemStack.push(4);
}

void writeHeaderOpen(Token token) {
Expand Down Expand Up @@ -325,7 +325,7 @@ private void writeToken(Token token) {
}

if (requestedWhitespace == BLANK_LINE
&& (postWriteModifiedContinuingListCount.isPositive() || continuingFooterTag)) {
&& (!postWriteModifiedContinuingListStack.isEmpty() || continuingFooterTag)) {
/*
* We don't write blank lines inside lists or footer tags, even in cases where we otherwise
* would (e.g., before a <p> tag). Justification: We don't write blank lines _between_ list
Expand Down Expand Up @@ -418,7 +418,7 @@ enum AutoIndent {
}

private int innerIndent() {
int innerIndent = continuingListItemCount.value() * 4 + continuingListCount.value() * 2;
int innerIndent = continuingListItemStack.total() + continuingListStack.total();
if (continuingFooterTag) {
innerIndent += 4;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.googlejavaformat.java.javadoc;

import java.util.ArrayDeque;
import java.util.Deque;

/**
* Stack for tracking the level of nesting. In the simplest case, each entry is just the integer 1,
* and the stack is effectively a counter. In more complex cases, the entries may depend on context.
* For example, if the stack is keeping track of Javadoc lists, the entries represent indentation
* levels, and those depend on whether the list is an HTML list or a Markdown list.
*/
final class NestingStack {
private int total;
private final Deque<Integer> stack = new ArrayDeque<>();

int total() {
return total;
}

void push() {
push(1);
}

void push(int value) {
stack.push(value);
total += value;
}

void incrementIfPositive() {
if (total > 0) {
push();
}
}

void popIfNotEmpty() {
if (!isEmpty()) {
total -= stack.pop();
}
}

boolean isEmpty() {
return stack.isEmpty();
}

void reset() {
total = 0;
stack.clear();
}
}
Loading