From 3810d50ec77e8dd2fe8dc07e06a3d60250103d30 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Wed, 23 Sep 2026 10:24:05 +0300 Subject: [PATCH] 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. --- .../palantir/javaformat/java/JavaCommentsHelper.java | 7 +++++-- .../java/testdata/ojf-issue-38-nbsp-line-comment.input | 7 +++++++ .../testdata/ojf-issue-38-nbsp-line-comment.output | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.input create mode 100644 open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.output diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java index 4d6316601..7cac97a7a 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java @@ -139,8 +139,11 @@ private List wrapLineComments(List lines, int column0) { String prefix = lineCommentPrefix(line); while (line.length() + column0 > options.maxLineLength()) { int idx = options.maxLineLength() - column0; - // only break on whitespace characters, and ignore the leading `// ` - while (idx >= prefix.length() && !CharMatcher.whitespace().matches(line.charAt(idx))) { + // only break on whitespace characters, and ignore the leading `// `. Not on a no-break space + // such as U+00A0: the new line would start with `//` and that space, which the next run takes + // for a missing space and pads, and then the comment never settles. + while (idx >= prefix.length() + && !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) { idx--; } if (idx <= prefix.length()) { diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.input b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.input new file mode 100644 index 000000000..82fe33b19 --- /dev/null +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.input @@ -0,0 +1,7 @@ +class NbspLineComment { + // 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. + int rows; + + //String testString = " thisisnotaHYPERLINKandsoitshouldntbetruncatedinsteaditshouldbedroppedthisisnotaHYPERLINKandsoit \"http://www.example.com/\"" + String url; +} diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.output b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.output new file mode 100644 index 000000000..a504eee7f --- /dev/null +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ojf-issue-38-nbsp-line-comment.output @@ -0,0 +1,10 @@ +class NbspLineComment { + // 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. + int rows; + + // String testString = + // " thisisnotaHYPERLINKandsoitshouldntbetruncatedinsteaditshouldbedroppedthisisnotaHYPERLINKandsoit + // \"http://www.example.com/\"" + String url; +}