diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java index 5addc6770..9578e2be6 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java @@ -16,6 +16,7 @@ import static com.google.googlejavaformat.java.javadoc.JavadocLexer.lex; import static com.google.googlejavaformat.java.javadoc.Token.Type.BR_TAG; +import static com.google.googlejavaformat.java.javadoc.Token.Type.LITERAL; import static com.google.googlejavaformat.java.javadoc.Token.Type.PARAGRAPH_OPEN_TAG; import static java.util.regex.Pattern.CASE_INSENSITIVE; import static java.util.regex.Pattern.compile; @@ -23,6 +24,7 @@ import com.google.common.collect.ImmutableList; import com.google.googlejavaformat.java.javadoc.JavadocLexer.LexException; import java.util.List; +import java.util.ListIterator; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,7 +57,9 @@ public static String formatJavadoc(String input, int blockIndent) { private static String render(List input, int blockIndent) { JavadocWriter output = new JavadocWriter(blockIndent); - for (Token token : input) { + ListIterator it = input.listIterator(); + while (it.hasNext()) { + Token token = it.next(); switch (token.getType()) { case BEGIN_JAVADOC: output.writeBeginJavadoc(); @@ -95,7 +99,17 @@ private static String render(List input, int blockIndent) { output.writePreClose(token); break; case CODE_OPEN_TAG: - output.writeCodeOpen(token); + if (it.hasNext()) { + Token nextToken = it.next(); + it.previous(); + if (nextToken.getType() == LITERAL) { + output.writeCodeOpenWithBreakBeforeIfAtEndOfLine(token); + } else { + output.writeCodeOpen(token); + } + } else { + output.writeCodeOpen(token); + } break; case CODE_CLOSE_TAG: output.writeCodeClose(token); diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java index 9c5838aeb..82377b89b 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java @@ -204,6 +204,10 @@ void writePreClose(Token token) { requestBlankLine(); } + void writeCodeOpenWithBreakBeforeIfAtEndOfLine(Token token) { + writeToken(token, true); + } + void writeCodeOpen(Token token) { writeToken(token); } @@ -288,6 +292,10 @@ enum RequestedWhitespace { } private void writeToken(Token token) { + writeToken(token, false); + } + + private void writeToken(Token token, boolean breakBeforeIfAtEndOfLine) { if (requestedMoeBeginStripComment != null) { requestNewline(); } @@ -318,7 +326,9 @@ private void writeToken(Token token) { * line, a newline won't help. Or it might help but only by separating "

veryverylongword," * which goes against our style.) */ - if (!atStartOfLine && token.length() + (needWhitespace ? 1 : 0) > remainingOnLine) { + if (!atStartOfLine + && token.length() + (needWhitespace ? 1 : 0) + (breakBeforeIfAtEndOfLine ? 1 : 0) + > remainingOnLine) { writeNewline(); } if (!atStartOfLine && needWhitespace) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.input new file mode 100644 index 000000000..deb067ece --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.input @@ -0,0 +1,3 @@ +/** + * Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x + */ diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.output new file mode 100644 index 000000000..28898be64 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I84.output @@ -0,0 +1,4 @@ +/** + * Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + * x + */