Skip to content

Commit 3810d50

Browse files
committed
Stop breaking line comments at a no-break space
JavaCommentsHelper wraps a line comment that is too long at the last whitespace before the limit, and CharMatcher.whitespace() counts the no-break space U+00A0 as whitespace. The continuation line then starts with "//" and that no-break space. LINE_COMMENT_MISSING_SPACE_PREFIX, whose \s does not match it, takes that for a comment with no space after the slashes, so the next run pads it, breaks at the no-break space again and leaves the padding behind as an empty "//" line. Every run adds one, and the file never settles (#38, from google/google-java-format#562). Line comments now break only at breaking whitespace, which leaves out U+00A0, U+2007 and U+202F: a no-break space is by definition not a place to break a line. A number grouped with no-break spaces now moves to the next line whole instead of being cut inside. The new golden covers both shapes. The 15,747 files of the JDK 21 sources format exactly as before; none of them contains a no-break space.
1 parent 53ac7d8 commit 3810d50

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
139139
String prefix = lineCommentPrefix(line);
140140
while (line.length() + column0 > options.maxLineLength()) {
141141
int idx = options.maxLineLength() - column0;
142-
// only break on whitespace characters, and ignore the leading `// `
143-
while (idx >= prefix.length() && !CharMatcher.whitespace().matches(line.charAt(idx))) {
142+
// only break on whitespace characters, and ignore the leading `// `. Not on a no-break space
143+
// such as U+00A0: the new line would start with `//` and that space, which the next run takes
144+
// for a missing space and pads, and then the comment never settles.
145+
while (idx >= prefix.length()
146+
&& !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) {
144147
idx--;
145148
}
146149
if (idx <= prefix.length()) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class NbspLineComment {
2+
// Batches are capped: the importer rejects any upstream export file whose total row count is at or above 1 000 000 000 rows, so split larger files first.
3+
int rows;
4+
5+
//String testString = " thisisnotaHYPERLINKandsoitshouldntbetruncatedinsteaditshouldbedroppedthisisnotaHYPERLINKandsoit \"http://www.example.com/\""
6+
String url;
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class NbspLineComment {
2+
// Batches are capped: the importer rejects any upstream export file whose total row count is at or above
3+
// 1 000 000 000 rows, so split larger files first.
4+
int rows;
5+
6+
// String testString =
7+
// " thisisnotaHYPERLINKandsoitshouldntbetruncatedinsteaditshouldbedroppedthisisnotaHYPERLINKandsoit
8+
// \"http://www.example.com/\""
9+
String url;
10+
}

0 commit comments

Comments
 (0)