Skip to content

Commit 644a3c3

Browse files
authored
Fix markdown docstring wrapping (palantir#1672)
Fix issue when wrapping markdown docstrings resulted in 2 slashes instead of 3
1 parent d106c08 commit 644a3c3

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ private String indentLineComments(List<String> lines, int column0) {
113113
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
114114
Pattern.compile("^(//+)(?!noinspection|\\$NON-NLS-\\d+\\$)[^\\s/]");
115115

116+
private static String lineCommentPrefix(String line) {
117+
int prefixLength = 0;
118+
while (prefixLength < line.length() && line.charAt(prefixLength) == '/') {
119+
prefixLength++;
120+
}
121+
return "/".repeat(prefixLength);
122+
}
123+
116124
private List<String> wrapLineComments(List<String> lines, int column0) {
117125
List<String> result = new ArrayList<>();
118126
for (String line : lines) {
@@ -127,17 +135,19 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
127135
result.add(line);
128136
continue;
129137
}
138+
// Preserve the original slash prefix (e.g. `///` for markdown docstrings) on wrapped lines.
139+
String prefix = lineCommentPrefix(line);
130140
while (line.length() + column0 > options.maxLineLength()) {
131141
int idx = options.maxLineLength() - column0;
132142
// only break on whitespace characters, and ignore the leading `// `
133-
while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) {
143+
while (idx >= prefix.length() && !CharMatcher.whitespace().matches(line.charAt(idx))) {
134144
idx--;
135145
}
136-
if (idx <= 2) {
146+
if (idx <= prefix.length()) {
137147
break;
138148
}
139149
result.add(line.substring(0, idx));
140-
line = "//" + line.substring(idx);
150+
line = prefix + line.substring(idx);
141151
}
142152
result.add(line);
143153
}

‎palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,22 @@ public void blankLinesImportComment() throws FormatterException {
430430
assertThat(ImportOrderer.reorderImports(withoutBlank, Style.GOOGLE)).isEqualTo(withoutBlank);
431431
}
432432

433+
@Test
434+
public void wrapMarkdownDocstringComment() throws Exception {
435+
String input = "class T {\n"
436+
+ " /// one long incredibly unbroken sentence moving from topic to topic so that no-one"
437+
+ " had a chance to interrupt;\n"
438+
+ " void m() {}\n"
439+
+ "}\n";
440+
String expected = "class T {\n"
441+
+ " /// one long incredibly unbroken sentence moving from topic to topic so that no-one"
442+
+ " had a chance\n"
443+
+ " /// to interrupt;\n"
444+
+ " void m() {}\n"
445+
+ "}\n";
446+
assertThat(Formatter.create().formatSource(input)).isEqualTo(expected);
447+
}
448+
433449
@Test
434450
public void dontWrapMoeLineComments() throws Exception {
435451
assertThat(Formatter.create()

0 commit comments

Comments
 (0)