1414
1515package com .google .googlejavaformat .java .javadoc ;
1616
17+ import static com .google .common .base .Preconditions .checkArgument ;
1718import static com .google .common .base .Preconditions .checkState ;
1819import static com .google .googlejavaformat .java .javadoc .JavadocLexer .lex ;
1920import static java .util .regex .Pattern .CASE_INSENSITIVE ;
6061import com .google .googlejavaformat .java .javadoc .Token .TableCloseTag ;
6162import com .google .googlejavaformat .java .javadoc .Token .TableOpenTag ;
6263import com .google .googlejavaformat .java .javadoc .Token .Whitespace ;
64+ import java .util .ArrayList ;
6365import java .util .List ;
6466import java .util .regex .Matcher ;
6567import java .util .regex .Pattern ;
@@ -90,7 +92,7 @@ public static String formatJavadoc(String input, int blockIndent) {
9092 default ->
9193 throw new IllegalArgumentException ("Input does not start with /** or ///: " + input );
9294 };
93- String inputForLexer = classicJavadoc ? input : ( "///" + markdownCommentText (input ) );
95+ String inputForLexer = classicJavadoc ? classicCommentText ( input ) : markdownCommentText (input );
9496 ImmutableList <Token > tokens ;
9597 try {
9698 tokens = lex (inputForLexer , classicJavadoc );
@@ -208,6 +210,45 @@ private static boolean oneLineJavadoc(String line, int blockIndent) {
208210
209211 private static final CharMatcher NOT_SPACE_OR_TAB = CharMatcher .noneOf (" \t " );
210212
213+ private static final Pattern CLASSIC_PREFIX_PATTERN = Pattern .compile ("^[ \\ t]*[*][ \\ t]?" );
214+
215+ private static String stripJavadocBeginAndEnd (String input ) {
216+ checkArgument (input .startsWith ("/**" ), "Missing /**: %s" , input );
217+ checkArgument (input .endsWith ("*/" ) && input .length () > 4 , "Missing */: %s" , input );
218+ return input .substring ("/**" .length (), input .length () - "*/" .length ());
219+ }
220+
221+ /**
222+ * Returns the given classic Javadoc comment after removing the leading ∕✱✱, trailing ✱∕, and any
223+ * leading asterisks and common leading whitespace on each line.
224+ */
225+ private static String classicCommentText (String input ) {
226+ String stripped = stripJavadocBeginAndEnd (input );
227+ List <String > lines = stripped .lines ().toList ();
228+ if (lines .isEmpty ()) {
229+ // Can't happen: it would only happen for `/***/`, but we filter out comments starting `/***`.
230+ return "" ;
231+ }
232+ // The first line is handled specially in case we have something like `/** * foo\n * bar\n */`.
233+ // The end result should not strip the `*` from `* foo`.
234+ List <String > processedLines = new ArrayList <>();
235+ processedLines .add (lines .get (0 ));
236+ for (String line : lines .subList (1 , lines .size ())) {
237+ Matcher m = CLASSIC_PREFIX_PATTERN .matcher (line );
238+ if (m .find ()) {
239+ processedLines .add (m .replaceFirst ("" ));
240+ } else {
241+ // Input line did not have leading `*`. In that case, it's hard to know what is supposed to
242+ // be indentation of the comment as a whole and what is supposed to be indentation of the
243+ // content. We just strip all leading whitespace.
244+ processedLines .add (line .stripLeading ());
245+ }
246+ }
247+ // Unlike Markdown comments, stripping common leading whitespace is not mandated by any
248+ // specification. But it's not forbidden either.
249+ return stripCommonLeadingWhitespace (processedLines );
250+ }
251+
211252 /**
212253 * Returns the given string with the leading /// and any common leading whitespace removed from
213254 * each line. The resultant string can then be fed to a standard Markdown parser.
@@ -219,6 +260,10 @@ private static String markdownCommentText(String input) {
219260 .peek (line -> checkState (line .contains ("///" ), "Line does not contain ///: %s" , line ))
220261 .map (line -> line .substring (line .indexOf ("///" ) + 3 ))
221262 .toList ();
263+ return stripCommonLeadingWhitespace (lines );
264+ }
265+
266+ private static String stripCommonLeadingWhitespace (List <String > lines ) {
222267 int leadingSpace =
223268 lines .stream ()
224269 .filter (line -> NOT_SPACE_OR_TAB .matchesAnyOf (line ))
0 commit comments