Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t break right after an HTML code tag that’s followed by a literal #638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>
*/