Skip to content

Commit

Permalink
Don’t break right after a HTML code tag that’s followed by a literal
Browse files Browse the repository at this point in the history
If a CODE_OPEN_TAG token is followed by a LITERAL token, break before writing
the CODE_OPEN_TAG token if writing it will result in us eating up the remainder
of the current line.

Partially fixes google#84.
  • Loading branch information
Nikolai Weibull committed Aug 1, 2021
1 parent 184a5ad commit 9f244de
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

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;

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;

Expand Down Expand Up @@ -55,7 +57,9 @@ public static String formatJavadoc(String input, int blockIndent) {

private static String render(List<Token> input, int blockIndent) {
JavadocWriter output = new JavadocWriter(blockIndent);
for (Token token : input) {
ListIterator<Token> it = input.listIterator();
while (it.hasNext()) {
Token token = it.next();
switch (token.getType()) {
case BEGIN_JAVADOC:
output.writeBeginJavadoc();
Expand Down Expand Up @@ -95,7 +99,17 @@ private static String render(List<Token> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ void writePreClose(Token token) {
requestBlankLine();
}

void writeCodeOpenWithBreakBeforeIfAtEndOfLine(Token token) {
writeToken(token, true);
}

void writeCodeOpen(Token token) {
writeToken(token);
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -318,7 +326,9 @@ private void writeToken(Token token) {
* line, a newline won't help. Or it might help but only by separating "<p>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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <code>x</code>
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* <code>x</code>
*/

0 comments on commit 9f244de

Please sign in to comment.