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 @@ -454,8 +454,9 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
}

/// Join literal tokens separated by whitespace when breaking between them would change the
/// meaning of the comment (for example, placing `@` or a Markdown block marker at the start of a
/// wrapped line) or when joining a hyphenated word broken across a line.
/// meaning of the comment (for example, placing `@`, a Markdown block marker, or an OpenJDK
/// paragraph-ending token at the start of a wrapped line) or when joining a hyphenated word
/// broken across a line.
///
/// This method must be called after [#joinAdjacentLiteralsAndAdjacentWhitespace], as it assumes
/// that adjacent literal tokens and adjacent whitespace tokens (including ignored
Expand All @@ -479,7 +480,7 @@ private static ImmutableList<Token> joinLiteralsAcrossWhitespace(
i++;
while (isSameParagraphWhitespace(input.get(i))
&& input.get(i + 1) instanceof Literal next
&& couldStartMarkdownBlock(input, i + 1)) {
&& couldStartMarkdownBlockOrEndParagraph(input, i + 1)) {
tagText.append(" ").append(next.value());
i += 2;
}
Expand Down Expand Up @@ -510,12 +511,12 @@ && isWordStart(nextValue)
i += 2;
continue;
}
if (!classicJavadoc && couldStartMarkdownBlock(input, i + 1)) {
if (!classicJavadoc && couldStartMarkdownBlockOrEndParagraph(input, i + 1)) {
/*
* In Markdown Javadoc, joining a block-marker literal onto the preceding literal
* prevents line wrapping from placing the marker at the start of a continuation line,
* where CommonMark would parse it as a list, block quote, heading, code fence,
* thematic break, table separator, or HTML block.
* In Markdown Javadoc, joining a block-marker or paragraph-ending literal onto the
* preceding literal prevents line wrapping from placing it at the start of a continuation
* line, where CommonMark would parse it as a new block or OpenJDK's
* DocTreeMaker.SentenceBreaker would end the summary paragraph early.
*/
accumulated.append(" ").append(nextValue);
i += 2;
Expand All @@ -536,19 +537,26 @@ private static boolean isSameParagraphWhitespace(Token token) {
return token instanceof Whitespace whitespace && !hasMultipleNewlines(whitespace.value());
}

private static boolean couldStartMarkdownBlock(List<Token> input, int index) {
private static boolean couldStartMarkdownBlockOrEndParagraph(List<Token> input, int index) {
String s = input.get(index).value();
return switch (s.charAt(0)) {
case '>' -> true;
case '+' -> s.equals("+");
// OpenJDK's DocTreeMaker.SentenceBreaker ends the first-sentence paragraph on
// `\n {0,3}[-+*#=_>]`, and its normalizeMarkdown replaces `.` with `-` inside `<...>`,
// `[...]`, `(...)`, and `` `...` `` before matching that regex. Pinning any token starting
// with these characters avoids truncating method, type, and `@deprecated` summaries (and
// leaving unclosed code spans), and also subsumes CommonMark bullet lists (`-`, `+`, `*`),
// block quotes (`>`), ATX headings (`#`), setext heading underlines (`-`, `=`), thematic
// breaks (`---`, `***`, `___`), and `-`-prefixed GFM table separators.
case '-', '+', '*', '#', '=', '_', '>', '.' -> true;
case '~' -> s.startsWith("~~~");
case '<' -> MARKDOWN_HTML_BLOCK_START_PATTERN.matcher(s).lookingAt();
case '-', ':' -> MARKDOWN_TABLE_SEPARATOR_PATTERN.matcher(s).matches();
case ':' -> MARKDOWN_TABLE_SEPARATOR_PATTERN.matcher(s).matches();
case '|' ->
s.equals("|")
? isFollowedByTableSeparator(input, index)
: MARKDOWN_TABLE_SEPARATOR_PATTERN.matcher(s).matches();
case '*', '_', '=', '#', '0', '1', '`' -> MARKDOWN_BLOCK_MARKER_PATTERN.matcher(s).matches();
case '0', '1' -> MARKDOWN_ORDERED_LIST_ITEM_PATTERN.matcher(s).matches();
case '`' -> MARKDOWN_BACKTICK_FENCE_PATTERN.matcher(s).matches();
default -> false;
};
}
Expand Down Expand Up @@ -814,18 +822,14 @@ static boolean hasMultipleNewlines(String s) {
private static final Pattern MARKDOWN_HARD_LINE_BREAK_PATTERN = compile("\\\\[ \t]*\n");
private static final Pattern BACKSLASH_PLUS_CHARACTER_PATTERN = compile("\\\\.");

// Bullet list `-`, setext heading underlines (`-`, `--`), thematic breaks (`---`), and GFM table
// separator tokens (`|-|`, `|---`, `:--`, `--:`, etc.).
// GFM table separator tokens (`|-|`, `|---`, `:--`, `--:`, `---`, etc.).
private static final Pattern MARKDOWN_TABLE_SEPARATOR_PATTERN = compile("[:|-]*-[:|-]*");

// Whole whitespace-delimited tokens that CommonMark may parse as a block start if placed at the
// start of a wrapped line:
// - `*+`, `_+`, `=+`: bullet list `*`, thematic breaks, setext heading underlines
// - `#{1,6}`: ATX headings
// - `0{0,8}1[.)]`: ordered list items (only start number 1 can interrupt a paragraph)
// - `` `{3,}[^`]* ``: backtick code fence openers (no subsequent backtick on the same token)
private static final Pattern MARKDOWN_BLOCK_MARKER_PATTERN =
compile("\\*+|_+|=+|#{1,6}|0{0,8}1[.)]|`{3,}[^`]*");
// Ordered list items starting at 1 (only start number 1 can interrupt a paragraph).
private static final Pattern MARKDOWN_ORDERED_LIST_ITEM_PATTERN = compile("0{0,8}1[.)]");

// Backtick code fence openers (three or more backticks with no closing backtick in the token).
private static final Pattern MARKDOWN_BACKTICK_FENCE_PATTERN = compile("`{3,}[^`]*");

// CommonMark HTML block types 1-6, which can interrupt a paragraph (type 7 cannot).
private static final Pattern MARKDOWN_HTML_BLOCK_START_PATTERN =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3554,12 +3554,12 @@ void j() {}
String expected =
"""
class T {
/// Returns the index of the first occurrence of the specified target element, or returns a value
/// -1 if absent.
/// Returns the index of the first occurrence of the specified target element, or returns a
/// value -1 if absent.
void a() {}

/// Returns the index of the first occurrence of the specified target element, or adds an offset
/// +1 if present.
/// Returns the index of the first occurrence of the specified target element, or adds an
/// offset +1 if present.
void b() {}

/// Returns the number of elements in the range when the maximum allowed capacity is capped up to
Expand All @@ -3570,12 +3570,12 @@ void c() {}
/// 100. Next line.
void d() {}

/// Returns the formatted identifier for the element, which is described in detail at the link
/// #method() below.
/// Returns the formatted identifier for the element, which is described in detail at the
/// link #method() below.
void e() {}

/// Returns the value produced by applying the mapping function from the given input argument `x
/// -> y` in the stream.
/// Returns the value produced by applying the mapping function from the given input argument
/// `x -> y` in the stream.
void f() {}

/// Returns an inline formatted element rendered inside the enclosing paragraph using a simple
Expand Down Expand Up @@ -3655,62 +3655,61 @@ void m() {}
String expected =
"""
class T {
/// Command for local testing: `bazel test --test_strategy=local --nocache_test_results
/// --test_arg='--env=QA' //foo/bar:BazTest`
/// Command for local testing: `bazel
/// test --test_strategy=local --nocache_test_results --test_arg='--env=QA' //foo/bar:BazTest`
void a() {}

/// Records failure summary artifacts when pre-run setup (such as `workspace_setup_cli
/// --mode=prepare`) throws `cause`.
/// Records failure summary artifacts when pre-run setup (such as
/// `workspace_setup_cli --mode=prepare`) throws `cause`.
void b() {}

/// Produces a query of the form (**OR** (**EQ** search_term *id*) (**Q_SUBSTRING** search_term
/// *name*)).
/// Produces a query of the form (**OR** (**EQ** search_term *id*) (**Q_SUBSTRING**
/// search_term *name*)).
void c() {}

/// Returns the normalized status value when the input record matches a known condition or
/// **nothing** otherwise.
/// Returns the normalized status value when the input record matches a known condition
/// or **nothing** otherwise.
void d() {}

/// Returns the formatted identifier for the element matching the requested search key against
/// _name_ below.
/// Returns the formatted identifier for the element matching the requested search key
/// against _name_ below.
void e() {}

/// Returns the internal sentinel key reserved by the runtime for tracking generated metadata
/// _internal_id here.
/// Returns the internal sentinel key reserved by the runtime for tracking generated
/// metadata _internal_id here.
void f() {}

/// Evaluates the spreadsheet cell formula expression represented by the canonical example text
/// =A1+B1 here.
/// Evaluates the spreadsheet cell formula expression represented by the canonical example
/// text =A1+B1 here.
void g() {}

/// Returns the broad category of attribute type (e.g., bool, unique enum, repeatable enum, url,
/// ...).
/// Returns the broad category of attribute type (e.g., bool, unique enum, repeatable enum,
/// url, ...).
void h() {}

/// Generates the certificate chain by interleaving ordered entries `[prod_0, dev_0, prod_1,
/// ...]`.
/// Generates the certificate chain by interleaving ordered entries `[prod_0, dev_0,
/// prod_1, ...]`.
void i() {}

/// Convenience helper for `ServiceRegistry.getDefaultInstanceForTesting()
/// .useDefaultAuthentication(policy)`.
/// Convenience helper for
/// `ServiceRegistry.getDefaultInstanceForTesting() .useDefaultAuthentication(policy)`.
void j() {}

/// Summary.
///
/// @deprecated Use the replacement option configured via the command line flag
/// --replacement_flag_name instead.
/// @deprecated Use the replacement option configured via the command line
/// flag --replacement_flag_name instead.
@Deprecated
void k() {}

/// Summary.
///
/// @deprecated
/// --very_long_flag_name_that_exceeds_the_remaining_line_length_and_would_otherwise_wrap_onto_line_two
/// @deprecated --very_long_flag_name_that_exceeds_the_remaining_line_length_and_would_otherwise_wrap_onto_line_two
@Deprecated
void l() {}

/// > Produces a query of the form (**OR** (**EQ** search_term *id*) (**Q_SUBSTRING** search_term
/// > *name*)).
/// > Produces a query of the form (**OR** (**EQ** search_term *id*) (**Q_SUBSTRING**
/// > search_term *name*)).
void m() {}
}
""";
Expand Down
Loading