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 @@ -240,7 +240,9 @@ private Token readToken() throws LexException {
private Function<String, Token> consumeToken() throws LexException {
boolean preserveExistingFormatting = preserveExistingFormatting();

if (input.tryConsumeRegex(NEWLINE_PATTERN)) {
if ((classicJavadoc || preserveExistingFormatting)
? input.tryConsumeRegex(NEWLINE_PATTERN)
: input.tryConsume("\n")) {
somethingSinceNewline = false;
return preserveExistingFormatting ? ForcedNewline::new : Whitespace::new;
}
Expand Down Expand Up @@ -292,7 +294,7 @@ private Function<String, Token> consumeToken() throws LexException {
if (!somethingSinceNewline && input.tryConsumeRegex(FOOTER_TAG_PATTERN)) {
checkMatchingTags();
somethingSinceNewline = true;
return FooterJavadocTagStart::new;
return value -> new FooterJavadocTagStart(WHITESPACE_PATTERN.matcher(value).replaceAll(" "));
}
somethingSinceNewline = true;

Expand Down Expand Up @@ -388,9 +390,9 @@ private Function<String, Token> consumeToken() throws LexException {
} else if (input.tryConsumeRegex(BR_PATTERN)) {
return BrTag::new;
} else if (input.tryConsumeRegex(MOE_BEGIN_STRIP_COMMENT_PATTERN)) {
return MoeBeginStripComment::new;
return value -> new MoeBeginStripComment(WHITESPACE_PATTERN.matcher(value).replaceAll(" "));
} else if (input.tryConsumeRegex(MOE_END_STRIP_COMMENT_PATTERN)) {
return MoeEndStripComment::new;
return value -> new MoeEndStripComment(WHITESPACE_PATTERN.matcher(value).replaceAll(" "));
} else if (input.tryConsumeRegex(HTML_COMMENT_PATTERN)) {
return HtmlComment::new;
} else if (input.tryConsumeRegex(literalPattern())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ final class JavadocWriter {
private final NestingStack.Int postWriteModifiedContinuingListStack = new NestingStack.Int();
private int remainingOnLine;
private boolean atStartOfLine;
private boolean pendingListItemSpace;
private RequestedWhitespace requestedWhitespace = NONE;
private Token requestedMoeBeginStripComment;
private String indentForMoeEndStripComment = "";
Expand Down Expand Up @@ -127,6 +128,7 @@ void writeBeginJavadoc() {
} else {
output.append("/// ");
remainingOnLine = maxLineLength - blockIndent - 4;
atStartOfLine = true;
}
}

Expand All @@ -148,6 +150,7 @@ void writeFooterJavadocTagStart(FooterJavadocTagStart token) {
* currently know which of those tags are open.
*/
continuingListItemOfInnermostList = false;
pendingListItemSpace = false;
indentStack.reset();
/*
* There's probably no need for this, since its only effect is to disable blank lines in some
Expand Down Expand Up @@ -224,17 +227,31 @@ void writeListClose(ListCloseTag token) {

if (classicJavadoc) {
requestBlankLine();
} else if (token.value().isEmpty()) {
pendingListItemSpace = false;
requestNewline();
atStartOfLine = false;
}
}

void writeListItemOpen(ListItemOpenTag token) {
pendingListItemSpace = false;
requestNewline();

if (continuingListItemOfInnermostList) {
// TODO(cpovirk): consider whether we can handle this only with operations on the indent stack
continuingListItemOfInnermostList = false;
indentStack.popUntil(ListItemIndent.class);
}
if (!token.value().startsWith("<")) {
// Markdown list item marker (as opposed to HTML <li>/<dt>/<dd>).
String marker = token.value().stripTrailing();
writeToken(new ListItemOpenTag(marker));
continuingListItemOfInnermostList = true;
indentStack.push(new ListItemIndent(marker.length() + 1));
pendingListItemSpace = true;
return;
}
writeToken(token);
continuingListItemOfInnermostList = true;
int indent = token.value().length();
Expand Down Expand Up @@ -289,6 +306,8 @@ void writeMarkdownBlockQuoteOpen(MarkdownBlockQuoteOpen token) {

void writeMarkdownBlockQuoteClose() {
indentStack.popUntil(BlockQuoteIndent.class);
requestNewline();
atStartOfLine = false;
}

void writePreOpen(PreOpenTag token) {
Expand Down Expand Up @@ -336,15 +355,7 @@ void writeMoeEndStripComment(MoeEndStripComment token) {

void writeHtmlComment(HtmlComment token) {
requestNewline();

List<String> lines = token.value().lines().toList();
writeToken(new HtmlComment(lines.get(0)));
for (String line : lines.subList(1, lines.size())) {
writeNewline(AutoIndent.NO_AUTO_INDENT);
output.append(line);
remainingOnLine -= line.length();
}

writeToken(token);
requestNewline();
}

Expand All @@ -369,10 +380,12 @@ void writeLiteral(Literal token) {

void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
if (!atStartOfLine) {
// A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken.
// A reminder that atStartOfLine is still true after `> ` or `-` because they are
// StartOfLineTokens.
requestBlankLine();
}
flushWhitespace();
flushPendingListItemSpace();
output.append(token.start());
token
.literal()
Expand All @@ -393,6 +406,7 @@ void writeMarkdownTable(MarkdownTable token) {
requestBlankLine();
}
flushWhitespace();
flushPendingListItemSpace();
List<String> lines = token.value().lines().toList();
output.append(lines.get(0));
for (String line : lines.subList(1, lines.size())) {
Expand Down Expand Up @@ -432,7 +446,7 @@ private void flushWhitespace() {
requestNewline();
}

if (!wroteAnythingSignificant) {
if (!wroteAnythingSignificant || pendingListItemSpace) {
requestedWhitespace = NONE;
}

Expand All @@ -458,8 +472,17 @@ private void flushWhitespace() {
}
}

private void flushPendingListItemSpace() {
if (pendingListItemSpace) {
output.append(" ");
remainingOnLine--;
pendingListItemSpace = false;
}
}

private void writeToken(Token token) {
if (token.value().isEmpty()) {
String value = token.value();
if (value.isEmpty()) {
return;
}

Expand All @@ -479,6 +502,8 @@ private void writeToken(Token token) {
remainingOnLine--;
}

flushPendingListItemSpace();

if (requestedMoeBeginStripComment != null) {
output.append(requestedMoeBeginStripComment.value());
requestedMoeBeginStripComment = null;
Expand All @@ -489,7 +514,18 @@ private void writeToken(Token token) {
return;
}

output.append(token.value());
int newline = value.indexOf('\n');
if (newline == -1) {
output.append(value);
remainingOnLine -= value.length();
} else {
output.append(value, 0, newline);
for (String line : value.substring(newline + 1).split("\n", -1)) {
writeNewline(NO_AUTO_INDENT);
output.append(line);
remainingOnLine -= line.length();
}
}

if (!(token instanceof StartOfLineToken)) {
atStartOfLine = false;
Expand All @@ -505,12 +541,12 @@ private void writeToken(Token token) {
* Possibly the "width" question has no right answer:
* http://denisbider.blogspot.com/2015/09/when-monospace-fonts-arent-unicode.html
*/
remainingOnLine -= token.length();
requestedWhitespace = NONE;
wroteAnythingSignificant = true;
}

private void writeNewlineStart() {
pendingListItemSpace = false;
output.append("\n");
appendSpaces(blockIndent + (classicJavadoc ? 1 : 0));
output.append(classicJavadoc ? "*" : "///");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private boolean visitListItemOrBlockQuote(
Pattern startPattern,
Function<String, Token> openTokenFactory,
Token closeToken) {
Matcher matcher = startPattern.matcher(input).region(start, input.length());
Matcher matcher = startPattern.matcher(input).region(start, endPosition(node));
verify(matcher.lookingAt());
Token openToken = openTokenFactory.apply(matcher.group(1));
addSpan(node, openToken, closeToken, matcher.start(1));
Expand Down Expand Up @@ -346,5 +346,5 @@ private static final class CodeLink extends Link {
private static final Pattern BLOCKQUOTE_START_PATTERN = Pattern.compile("(> ?)");

private static final Pattern LIST_ITEM_START_PATTERN =
Pattern.compile("(([-+*]|[0-9]+[.)])(?:\\s|$))");
Pattern.compile("(([-+*]|[0-9]+[.)])[ \\t]?)");
}
Loading
Loading