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 @@ -236,15 +236,29 @@ private static String classicCommentText(String input) {
// The end result should not strip the `*` from `* foo`.
List<String> processedLines = new ArrayList<>();
processedLines.add(lines.get(0));
for (String line : lines.subList(1, lines.size())) {
List<String> continuations = lines.subList(1, lines.size());
int starPrefixLength =
continuations.stream()
.map(CLASSIC_PREFIX_PATTERN::matcher)
.filter(Matcher::find)
.mapToInt(Matcher::end)
.min()
.orElse(0);
int nonStarMinLeading =
continuations.stream()
.filter(line -> !CLASSIC_PREFIX_PATTERN.matcher(line).find())
.filter(line -> NOT_SPACE_OR_TAB.matchesAnyOf(line))
.mapToInt(NOT_SPACE_OR_TAB::indexIn)
.min()
.orElse(0);
int nonStarStripCount = Math.min(starPrefixLength, nonStarMinLeading);
for (String line : continuations) {
Matcher m = CLASSIC_PREFIX_PATTERN.matcher(line);
if (m.find()) {
processedLines.add(m.replaceFirst(""));
} else {
// Input line did not have leading `*`. In that case, it's hard to know what is supposed to
// be indentation of the comment as a whole and what is supposed to be indentation of the
// content. We just strip all leading whitespace.
processedLines.add(line.stripLeading());
processedLines.add(
line.length() >= nonStarStripCount ? line.substring(nonStarStripCount) : "");
}
}
// Unlike Markdown comments, stripping common leading whitespace is not mandated by any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,43 @@ class Test {}
doFormatTest(input, expected);
}

@Test
public void preCodeWithoutLeadingStarPreservesIndent() {
String input =
"""
/**
* <pre>{@code
class Demo {
// Comment
static final int X = 1;

public static void example() {
int y = 2;
}
}
* }</pre>
*/
class Test {}
""";
String expected =
"""
/**
* <pre>{@code
* class Demo {
* // Comment
* static final int X = 1;
*
* public static void example() {
* int y = 2;
* }
* }
* }</pre>
*/
class Test {}
""";
doFormatTest(input, expected);
}

// TODO: b/346668798 - Test the following Markdown constructs, and make the tests work as needed.
// We can assume that the CommonMark parser correctly handles Markdown, so the question is whether
// they are subsequently mishandled by our formatting logic. So for example the CommonMark parser
Expand Down
Loading