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 @@ -83,6 +83,7 @@ private String render(List<Token> input, int blockIndent) {
case HTML_COMMENT -> output.writeHtmlComment(token);
case BR_TAG -> output.writeBr(standardizeBrToken(token));
case WHITESPACE -> output.requestWhitespace();
case BLANK_LINE -> output.requestForcedBlankLine();
case FORCED_NEWLINE -> output.writeLineBreakNoAutoIndent();
case LITERAL, INLINE_TAG_OPEN, INLINE_TAG_CLOSE -> output.writeLiteral(token);
case PARAGRAPH_CLOSE_TAG, LIST_ITEM_CLOSE_TAG, OPTIONAL_LINE_BREAK -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.Iterators.peekingIterator;
import static com.palantir.javaformat.java.javadoc.Token.Type.BEGIN_JAVADOC;
import static com.palantir.javaformat.java.javadoc.Token.Type.BLANK_LINE;
import static com.palantir.javaformat.java.javadoc.Token.Type.BLOCKQUOTE_CLOSE_TAG;
import static com.palantir.javaformat.java.javadoc.Token.Type.BLOCKQUOTE_OPEN_TAG;
import static com.palantir.javaformat.java.javadoc.Token.Type.BR_TAG;
Expand Down Expand Up @@ -422,6 +423,8 @@ private static boolean isSuspendedHyphenConjunction(String s) {

/**
* Where the input has two consecutive line breaks between literals, insert a {@code <p>} tag between the literals.
* Where the second literal starts a {@code <div>}, which a {@code <p>} cannot hold, turn the line breaks into a
* {@link Token.Type#BLANK_LINE} instead.
*
* <p>This method must be called after {@link #joinAdjacentLiteralsAndAdjacentWhitespace}, as it assumes that
* adjacent whitespace tokens have already been joined.
Expand All @@ -435,9 +438,15 @@ private static List<Token> inferParagraphTags(List<Token> input) {

if (tokens.peek().getType() == WHITESPACE
&& hasMultipleNewlines(tokens.peek().getValue())) {
output.add(tokens.next());

if (tokens.peek().getType() == LITERAL) {
var whitespace = tokens.next();
var next = tokens.peek();

if (next.getType() != LITERAL) {
output.add(whitespace);
} else if (DIV_OPEN_PATTERN.matcher(next.getValue()).find()) {
output.add(new Token(BLANK_LINE, whitespace.getValue()));
} else {
output.add(whitespace);
output.add(new Token(PARAGRAPH_OPEN_TAG, "<p>"));
}
}
Expand Down Expand Up @@ -645,6 +654,9 @@ private static boolean hasMultipleNewlines(String s) {
private static final Pattern BLOCKQUOTE_OPEN_PATTERN = openTagPattern("blockquote");
private static final Pattern BLOCKQUOTE_CLOSE_PATTERN = closeTagPattern("blockquote");
private static final Pattern BR_PATTERN = openTagPattern("br");
// Tried on a literal, which ends at whitespace: "<div" alone when the tag has attributes. Unlike openTagPattern's
// \b, it does not match a custom element such as <div-custom>.
private static final Pattern DIV_OPEN_PATTERN = compile("^<div(?:[\\s/>]|$)", CASE_INSENSITIVE);
private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile("^[{]@snippet\\b");
private static final Pattern INLINE_TAG_OPEN_PATTERN = compile("^[{]@\\w*");
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.palantir.javaformat.java.javadoc.JavadocWriter.AutoIndent.AUTO_INDENT;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.AutoIndent.NO_AUTO_INDENT;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.BLANK_LINE;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.FORCED_BLANK_LINE;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.NEWLINE;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.NONE;
import static com.palantir.javaformat.java.javadoc.JavadocWriter.RequestedWhitespace.WHITESPACE;
Expand Down Expand Up @@ -69,6 +70,16 @@ void requestWhitespace() {
requestWhitespace(WHITESPACE);
}

/**
* Requests a blank line that, unlike the ones other tokens request, is written inside lists and footer tags too.
* The lexer turns the blank line before a {@code <div>} into this request instead of inferring a {@code <p>}: a
* newline there would leave nothing to keep the {@code <div>} off the line before it when the output is formatted
* again.
*/
void requestForcedBlankLine() {
requestWhitespace(FORCED_BLANK_LINE);
}

void writeBeginJavadoc() {
/*
* JavaCommentsHelper will make sure this is indented right. But it seems sensible enough that,
Expand Down Expand Up @@ -295,6 +306,8 @@ enum RequestedWhitespace {
WHITESPACE,
NEWLINE,
BLANK_LINE,
/** A {@link #BLANK_LINE} that is written inside lists and footer tags too. */
FORCED_BLANK_LINE,
;
}

Expand All @@ -317,7 +330,7 @@ private void writeToken(Token token) {
requestedWhitespace = NEWLINE;
}

if (requestedWhitespace == BLANK_LINE) {
if (requestedWhitespace == BLANK_LINE || requestedWhitespace == FORCED_BLANK_LINE) {
writeBlankLine();
requestedWhitespace = NONE;
} else if (requestedWhitespace == NEWLINE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ enum Type {
* asterisks) or spaces.
*/
WHITESPACE,
/**
* Whitespace with a blank line in it, before a {@code <div>}. {@link JavadocLexer} infers no {@code <p>} there,
* since a {@code <p>} cannot hold a {@code <div>}, so {@link JavadocWriter} keeps the blank line instead, even
* in a list or footer tag.
*/
BLANK_LINE,
/**
* A newline in a {@code <pre>} or {@code <table>} section. We preserve user formatting in these sections,
* including newlines.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,69 @@ void inferParagraphTags() {
doFormatTest(input, expected);
}

@Test
void noParagraphTagInferredBeforeDiv() {
// A <p> cannot hold a <div>, so a blank line before one stays a blank line.
String[] input = {
"/**",
" * Intro.",
" *",
" * <span>Another paragraph.</span>",
" *",
" * <DIV class=\"notice\">Block content.</DIV>",
" *",
" * Following paragraph.",
" *",
" * <div>",
" *",
" * <div-custom>Custom element.</div-custom>",
" *",
" * <p><div>Explicit paragraph.</div>",
" *",
" * <ul>",
" * <li>Item.",
" *",
" * <div>In an item.</div>",
" * </ul>",
" *",
" * @param x Parameter.",
" *",
" * <div>In a block tag.</div>",
" */",
"class Test {}",
};
String[] expected = {
"/**",
" * Intro.",
" *",
" * <p><span>Another paragraph.</span>",
" *",
" * <DIV class=\"notice\">Block content.</DIV>",
" *",
" * <p>Following paragraph.",
" *",
" * <div>",
" *",
" * <p><div-custom>Custom element.</div-custom>",
" *",
" * <p><div>Explicit paragraph.</div>",
" *",
" * <ul>",
" * <li>Item.",
" *",
" * <div>In an item.</div>",
" * </ul>",
" *",
" * @param x Parameter.",
" *",
" * <div>In a block tag.</div>",
" */",
"class Test {}",
};
doFormatTest(input, expected);
doFormatTest(expected, expected);
}

@Test
void paragraphTagNewlines() throws Exception {
String input = new String(
Expand Down
Loading