Skip to content

Commit 63b5d18

Browse files
cushongoogle-java-format Team
authored andcommitted
Avoid breaking line comments on non-breaking spaces.
`JavaCommentsHelper.wrapLineComments` previously wrapped line comments on `CharMatcher.whitespace()` (which matches U+00A0 NO-BREAK SPACE) while `LINE_COMMENT_MISSING_SPACE_PREFIX` matched ASCII `\s` (which does not match U+00A0). Wrapping a comment at `\u00a0` produced a continuation line starting with `//\u00a0`, and subsequent passes inserted a space (`// \u00a0`) and wrapped at `idx = 3`, inserting a new empty `//` line on every run. Break line comments only on `CharMatcher.breakingWhitespace()`, enable `Pattern.UNICODE_CHARACTER_CLASS` on `LINE_COMMENT_MISSING_SPACE_PREFIX`, and use `CharMatcher.whitespace().trimFrom(...)` in `indentLineComments`. Fixes #562 PiperOrigin-RevId: 1024644076
1 parent 60656e5 commit 63b5d18

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

‎core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,18 @@ private String preserveIndentation(List<String> lines, int column0) {
112112
private String indentLineComments(Tok tok, List<String> lines, int column0) {
113113
lines = wrapLineComments(tok, lines, column0);
114114
StringBuilder builder = new StringBuilder();
115-
builder.append(lines.get(0).trim());
116115
String indentString = options.indentString(column0);
117-
for (int i = 1; i < lines.size(); ++i) {
118-
builder.append(lineSeparator).append(indentString).append(lines.get(i).trim());
116+
for (int i = 0; i < lines.size(); ++i) {
117+
if (i > 0) {
118+
builder.append(lineSeparator).append(indentString);
119+
}
120+
builder.append(CharMatcher.whitespace().trimFrom(lines.get(i)));
119121
}
120122
return builder.toString();
121123
}
122124

123-
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = Pattern.compile("^(//+)[^\\s/]");
125+
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
126+
Pattern.compile("^(//+)[^\\s/]", Pattern.UNICODE_CHARACTER_CLASS);
124127

125128
// Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot
126129
// contain leading spaces.
@@ -149,8 +152,8 @@ private List<String> wrapLineComments(Tok tok, List<String> lines, int column0)
149152
}
150153
while (line.length() + column0 > options.maxLineLength()) {
151154
int idx = options.maxLineLength() - column0;
152-
// only break on whitespace characters, and ignore the leading `// `
153-
while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) {
155+
// only break on breaking whitespace characters, and ignore the leading `// `
156+
while (idx >= 2 && !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) {
154157
idx--;
155158
}
156159
if (idx <= 2) {

‎core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,10 @@ class T {
13601360
String expected =
13611361
"""
13621362
class T {
1363-
// Copyright (C) 2017 Interactive Brokers LLC. All rights reserved. This code is subject to the
1364-
//\u00a0terms and conditions of the IB API Non-Commercial License or the IB API Commercial License, as
1365-
// applicable.
1366-
// \u00a0non-breaking space after comment prefix
1363+
// Copyright (C) 2017 Interactive Brokers LLC. All rights reserved. This code is subject to
1364+
// the\u00a0terms and conditions of the IB API Non-Commercial License or the IB API Commercial License,
1365+
// as applicable.
1366+
//\u00a0non-breaking space after comment prefix
13671367
}
13681368
""";
13691369
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);

0 commit comments

Comments
 (0)