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; +}