Skip to content

Commit 1b8de42

Browse files
authored
Merge branch '21.X.X' into abashev/intellij-plugin-optimize-imports-with-open-java
2 parents 3f9f609 + e6c195f commit 1b8de42

4 files changed

Lines changed: 55 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ private String preserveIndentation(List<String> lines, int column0) {
102102
return builder.toString();
103103
}
104104

105-
// Wraps and re-indents line comments.
105+
// Wraps and re-indents line comments. A line that ends where the comment was wrapped can end in a Unicode space
106+
// such as U+00A0, which String.trim() keeps and the next run's trimTrailingFrom drops, so it goes here already.
106107
private String indentLineComments(List<String> lines, int column0) {
107108
lines = wrapLineComments(lines, column0);
108109
StringBuilder builder = new StringBuilder();
109-
builder.append(lines.get(0).trim());
110+
builder.append(CharMatcher.whitespace().trimFrom(lines.get(0)));
110111
String indentString = " ".repeat(column0);
111112
for (int i = 1; i < lines.size(); ++i) {
112113
builder.append(lineSeparator)
113114
.append(indentString)
114-
.append(lines.get(i).trim());
115+
.append(CharMatcher.whitespace().trimFrom(lines.get(i)));
115116
}
116117
return builder.toString();
117118
}
@@ -167,15 +168,19 @@ private static boolean isJBangDirective(String text) {
167168
return matcher.lookingAt() && (matcher.group(1) != null || JBANG_DIRECTIVE_NAMES.contains(matcher.group(2)));
168169
}
169170

170-
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = Pattern.compile("^(//+)[^\\s/]");
171+
// With UNICODE_CHARACTER_CLASS, \s also matches a Unicode space such as U+00A0 or U+3000, which is a space
172+
// already. Without it a wrap at U+3000 left a line starting `//` and that space, which the next run padded.
173+
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
174+
Pattern.compile("^(//+)[^\\s/]", Pattern.UNICODE_CHARACTER_CLASS);
171175

172176
// Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot
173177
// contain leading spaces.
174178
private static final Pattern LINE_COMMENT_NO_SPACE_PREFIX =
175179
Pattern.compile("^//+(noinspection|\\$NON-NLS-\\d+\\$)");
176180

177181
/**
178-
* Adds the missing space after the slashes of a line comment: {@code //foo} becomes {@code // foo}. The IDE markers
182+
* Adds the missing space after the slashes of a line comment: {@code //foo} becomes {@code // foo}. Any Unicode
183+
* space counts as one, so a no-break space after the slashes is kept as it is. The IDE markers
179184
* {@code //noinspection} and {@code //$NON-NLS-n$} stay as written, since their tools recognise them only without
180185
* the space. {@link com.palantir.javaformat.doc.Comment#computeFlat()} applies the same rule to a comment that is
181186
* written flat, for which {@link #rewrite} is not called.
@@ -231,8 +236,7 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
231236
while (line.length() + column0 > options.maxLineLength()) {
232237
int idx = options.maxLineLength() - column0;
233238
// only break on whitespace characters, and ignore the leading `// `. Not on a no-break space
234-
// such as U+00A0: the new line would start with `//` and that space, which the next run takes
235-
// for a missing space and pads, and then the comment never settles.
239+
// such as U+00A0, which is there to keep the words on either side of it on one line.
236240
while (idx >= prefix.length()
237241
&& !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) {
238242
idx--;

‎open-java-format/src/test/java/com/palantir/javaformat/java/JavaCommentsHelperTest.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,15 @@ void addMissingSpacePutsABlankAfterTheSlashesWhateverTheirNumber() {
133133

134134
@Test
135135
void addMissingSpaceLeavesSpacedCommentsAndIdeMarkersAlone() {
136-
// A blank or a tab after the slashes is a space already; the IDE markers are recognised only without one.
136+
// A blank, a tab or a Unicode space after the slashes is a space already; the IDE markers are recognised only
137+
// without one.
137138
var comments = List.of(
138139
"// foo",
139140
"// two spaces",
140141
"//\tfoo",
142+
"//\u00a0no-break space",
143+
"///\u00a0no-break space",
144+
"//\u3000ideographic space",
141145
"//",
142146
"///",
143147
"//$NON-NLS-1$ //$NON-NLS-2$",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A Unicode space right after the slashes, such as a no-break space (U+00A0) or an ideographic space (U+3000),
2+
# is the space the comment needs, so it does not get a regular one in front of it (#249).
3+
----------
4+
class T {
5+
// non-breaking space after the prefix
6+
/// non-breaking space after a markdown prefix
7+
// ideographic space after the prefix
8+
//foo
9+
void m() {}
10+
}
11+
----------
12+
class T {
13+
// non-breaking space after the prefix
14+
/// non-breaking space after a markdown prefix
15+
// ideographic space after the prefix
16+
// foo
17+
void m() {}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# A line comment wrapped next to a Unicode space comes out of the first run as it does out of every later one.
2+
# The line that ends at the break loses the no-break space (U+00A0) before it, and the line that starts at an
3+
# ideographic space (U+3000) keeps it after the slashes, without a regular space in front of it (#249).
4+
----------
5+
class T {
6+
// The importer reads the whole export file into memory before it checks the row count, so keep each export small.  Splitting a larger one first is cheaper than retrying it.
7+
int rows;
8+
9+
// 設定ファイルが見つからないときは既定値で起動します。 既定値はこのクラスの定数にまとめてあります。 値を変えるときは、テストの期待値も合わせて更新してください。 古い形式のファイルは読み込み時に新しい形式へ変換されます。 変換に失敗したファイルはそのまま残し、エラーをログに出します。
10+
int defaults;
11+
}
12+
----------
13+
class T {
14+
// The importer reads the whole export file into memory before it checks the row count, so keep each export small.
15+
// Splitting a larger one first is cheaper than retrying it.
16+
int rows;
17+
18+
// 設定ファイルが見つからないときは既定値で起動します。 既定値はこのクラスの定数にまとめてあります。 値を変えるときは、テストの期待値も合わせて更新してください。 古い形式のファイルは読み込み時に新しい形式へ変換されます。
19+
// 変換に失敗したファイルはそのまま残し、エラーをログに出します。
20+
int defaults;
21+
}

0 commit comments

Comments
 (0)