Skip to content

Commit e573100

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Don't wrap line comments that start with /// .
Until we have full support for Markdown Javadoc, this at least prevents us from misguidedly destroying it by using only `// ` for the continuation lines. PiperOrigin-RevId: 885030098
1 parent 2de7336 commit e573100

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private String indentLineComments(List<String> lines, int column0) {
108108
return builder.toString();
109109
}
110110

111+
/** Probably a markdown comment, so don't try to wrap it. */
112+
private static final Pattern MARKDOWN_JAVADOC_PREFIX = Pattern.compile("^///(\\s|$)");
113+
111114
// Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot
112115
// contain leading spaces.
113116
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
@@ -116,6 +119,13 @@ private String indentLineComments(List<String> lines, int column0) {
116119
private List<String> wrapLineComments(List<String> lines, int column0) {
117120
List<String> result = new ArrayList<>();
118121
for (String line : lines) {
122+
if (MARKDOWN_JAVADOC_PREFIX.matcher(line).find()) {
123+
// Don't try to wrap comments that might be markdown javadoc.
124+
// This is fairly approximate: a /// comment is only javadoc if it precedes a javadocable
125+
// program element. But even if this isn't javadoc, it's not a disaster if we don't wrap it.
126+
result.add(line);
127+
continue;
128+
}
119129
// Add missing leading spaces to line comments: `//foo` -> `// foo`.
120130
Matcher matcher = LINE_COMMENT_MISSING_SPACE_PREFIX.matcher(line);
121131
if (matcher.find()) {

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,21 +1507,18 @@ public void simpleMarkdown() {
15071507
" }",
15081508
"}",
15091509
};
1510-
// TODO(emcmanus): Fix the formatter so it doesn't produce this nonsense:
1511-
// - wrapped lines should start with /// not //
1512-
// - blank line before @param
1510+
// TODO(emcmanus): Actually format the javadoc. For now, we just leave `///` lines alone, unlike
1511+
// `//` lines which get wrapped.
15131512
String[] expected = {
15141513
"package com.example;",
15151514
"",
15161515
"/// # Heading",
15171516
"///",
15181517
"/// A very long line of text, long enough that it will need to be wrapped to fit within the"
1519-
+ " maximum",
1520-
"// line length.",
1518+
+ " maximum line length.",
15211519
"class Test {",
15221520
" /// Another very long line of text, also long enough that it will need to be wrapped to"
1523-
+ " fit within",
1524-
" // the maximum line length.",
1521+
+ " fit within the maximum line length.",
15251522
" /// @param <T> a generic type",
15261523
" <T> T method() {",
15271524
" return null;",

0 commit comments

Comments
 (0)