Skip to content
Open
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 @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableList;
import com.google.googlejavaformat.java.javadoc.JavadocLexer.LexException;
import com.google.googlejavaformat.java.javadoc.Token.BeginJavadoc;
import com.google.googlejavaformat.java.javadoc.Token.BlankLine;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteCloseTag;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteMarker;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteOpenTag;
Expand Down Expand Up @@ -134,6 +135,7 @@ private static String render(
case HtmlComment t -> output.writeHtmlComment(t);
case BrTag t -> output.writeBr(standardizeBrToken(t));
case Whitespace t -> output.requestWhitespaceOrBlankLine(t);
case BlankLine unused -> output.requestBlankLine();
case ForcedNewline unused -> output.writeLineBreakNoAutoIndent();
case MarkdownHardLineBreak unused -> output.writeMarkdownHardLineBreak();
case Literal t -> output.writeLiteral(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.PeekingIterator;
import com.google.googlejavaformat.java.javadoc.Token.BeginJavadoc;
import com.google.googlejavaformat.java.javadoc.Token.BlankLine;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteCloseTag;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteMarker;
import com.google.googlejavaformat.java.javadoc.Token.BlockQuoteOpenTag;
Expand Down Expand Up @@ -236,6 +237,14 @@ private Token readToken() throws LexException {
private Function<String, Token> consumeToken() throws LexException {
boolean preserveExistingFormatting = preserveExistingFormatting();

// Preserve the blank line before a div without inferring a paragraph tag.
if (classicJavadoc
&& !preserveExistingFormatting
&& !contextStack.containsAny(TAG_CONTEXTS)
&& input.tryConsumeRegex(BLANK_LINE_BEFORE_DIV_PATTERN)) {
somethingSinceNewline = false;
return BlankLine::new;
}
if (input.tryConsumeRegex(NEWLINE_PATTERN)) {
somethingSinceNewline = false;
return preserveExistingFormatting ? ForcedNewline::new : Whitespace::new;
Expand Down Expand Up @@ -693,6 +702,8 @@ static boolean hasMultipleNewlines(String s) {
*/
private static final Pattern NEWLINE_PATTERN = compile("[ \t]*\n");
private static final Pattern WHITESPACE_PATTERN = compile("\\s+");
private static final Pattern BLANK_LINE_BEFORE_DIV_PATTERN =
compile("(?:[ \t]*\n){2}[ \t]*(?=<div(?:\\s[^>]*)?/?>)", CASE_INSENSITIVE);
private static final Pattern BLOCKQUOTE_MARKER_PATTERN = compile("> ?");
private static final Pattern BLOCKQUOTE_LINE_PREFIX_PATTERN = compile("\n(?:[ \t]*>[ \t]?)+");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public String toString() {
return output.toString();
}

private void requestBlankLine() {
void requestBlankLine() {
requestWhitespace(BLANK_LINE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ record MarkdownTable(String value) implements Token {}
/// leading spaces and asterisks) or spaces.
record Whitespace(String value) implements Token {}

record BlankLine(String value) implements Token {}

/// A newline in a `<pre>` or `<table>` section. We preserve user formatting in these sections,
/// including newlines.
record ForcedNewline(String value) implements Token {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,45 @@ class Test {}
doFormatTest(input, expected);
}

@Test
public void divAfterBlankLine() {
String input =
"""
/**
* Intro.
*
* <span>Another paragraph.</span>
*
* <DIV class="notice">Block content.</DIV>
*
* Following paragraph.
*
* <div-custom>Custom element.</div-custom>
*
* <p><div>Explicit paragraph.</div>
*/
class Test {}
""";
String expected =
"""
/**
* Intro.
*
* <p><span>Another paragraph.</span>
*
* <DIV class="notice">Block content.</DIV>
*
* <p>Following paragraph.
*
* <p><div-custom>Custom element.</div-custom>
*
* <p><div>Explicit paragraph.</div>
*/
class Test {}
""";
doFormatTest(input, expected);
}

@Test
public void inferParagraphTags() {
String input =
Expand Down