Skip to content

Commit 21f3702

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Improve code block handling within lists.
A code block can appear in a list, either at the start of a list item, like this: ~~~java - ``` ... ``` ~~~ or as a continuation of a list item, like this: ~~~java - something ``` ... ``` ~~~ PiperOrigin-RevId: 905293622
1 parent a8ca4c7 commit 21f3702

4 files changed

Lines changed: 31 additions & 8 deletions

File tree

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ void writeLiteral(Literal token) {
335335
}
336336

337337
void writeMarkdownFencedCodeBlock(MarkdownFencedCodeBlock token) {
338+
if (wroteAnythingSignificant && !atStartOfLine) {
339+
// A reminder that atStartOfLine is still true after `-␣` because it is a StartOfLineToken.
340+
requestBlankLine();
341+
}
338342
flushWhitespace();
339343
output.append(token.start());
340344
token

core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.googlejavaformat.java.javadoc.Token.MarkdownFencedCodeBlock;
3131
import com.google.googlejavaformat.java.javadoc.Token.ParagraphCloseTag;
3232
import com.google.googlejavaformat.java.javadoc.Token.ParagraphOpenTag;
33+
import java.util.Objects;
3334
import java.util.regex.Matcher;
3435
import java.util.regex.Pattern;
3536
import org.commonmark.node.BulletList;
@@ -128,12 +129,16 @@ private void visitFencedCodeBlock(FencedCodeBlock fencedCodeBlock) {
128129
// indentation gets subtracted from FencedCodeBlock.getLiteral(), which is the actual text
129130
// represented by the code block.
130131
int start = startPosition(fencedCodeBlock) + fencedCodeBlock.getFenceIndent();
132+
int closingLength =
133+
Objects.requireNonNullElse(
134+
fencedCodeBlock.getClosingFenceLength(), fencedCodeBlock.getOpeningFenceLength());
135+
// We have observed getClosingFenceLength() returning null in some cases.
131136
MarkdownFencedCodeBlock token =
132137
new MarkdownFencedCodeBlock(
133138
input.substring(start, endPosition(fencedCodeBlock)),
134139
fencedCodeBlock.getFenceCharacter().repeat(fencedCodeBlock.getOpeningFenceLength())
135140
+ fencedCodeBlock.getInfo(),
136-
fencedCodeBlock.getFenceCharacter().repeat(fencedCodeBlock.getClosingFenceLength()),
141+
fencedCodeBlock.getFenceCharacter().repeat(closingLength),
137142
fencedCodeBlock.getLiteral());
138143
positionToToken.get(start).addLast(token);
139144
}

core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ default int length() {
3838

3939
/**
4040
* Tokens that are always pinned to the following token. For example, {@code <p>} in {@code <p>Foo
41-
* bar} (never {@code <p> Foo bar} or {@code <p>\nFoo bar}).
41+
* bar} (never {@code <p> Foo bar} or {@code <p>\nFoo bar}); or {#code
42+
* <li>} or {@code -␣} in {@code <li>Foo bar} or {@code -␣Foo bar}.
4243
*
43-
* <p>This is not the only kind of "pinning" that we do: See also the joining of Literal tokens
44-
* done by the lexer. The special pinning here is necessary because these tokens are not of type
45-
* Literal (because they require other special handling).
44+
* <p>This is not the only kind of "pinning" that we do: See also the joining of Literal
45+
* tokens done by the lexer. The special pinning here is necessary because these tokens are
46+
* not of type Literal (because they require other special handling).
4647
*/
4748
interface StartOfLineToken {}
4849

core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,9 +1739,14 @@ public void markdownFencedCodeBlocks() {
17391739
/// ```
17401740
///
17411741
/// - flibbertigibbet
1742+
/// ```
1743+
/// code block in a list after text with no blank line intervening (one will be inserted)
1744+
/// ```
1745+
///
1746+
/// - flibbertigibbet
17421747
///
17431748
/// ```
1744-
/// code block in a list after text
1749+
/// code block in a list after text with a blank line intervening
17451750
/// ```
17461751
///
17471752
/// ~~~java
@@ -1767,8 +1772,16 @@ class Test {}
17671772
/// in a list
17681773
/// ```
17691774
///
1770-
/// - flibbertigibbet```
1771-
/// code block in a list after text
1775+
/// - flibbertigibbet
1776+
///
1777+
/// ```
1778+
/// code block in a list after text with no blank line intervening (one will be inserted)
1779+
/// ```
1780+
///
1781+
/// - flibbertigibbet
1782+
///
1783+
/// ```
1784+
/// code block in a list after text with a blank line intervening
17721785
/// ```
17731786
///
17741787
/// ~~~java

0 commit comments

Comments
 (0)