Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ private String preserveIndentation(List<String> lines, int column0) {
return builder.toString();
}

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

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

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

/**
* Adds the missing space after the slashes of a line comment: {@code //foo} becomes {@code // foo}. The IDE markers
* Adds the missing space after the slashes of a line comment: {@code //foo} becomes {@code // foo}. Any Unicode
* space counts as one, so a no-break space after the slashes is kept as it is. The IDE markers
* {@code //noinspection} and {@code //$NON-NLS-n$} stay as written, since their tools recognise them only without
* the space. {@link com.palantir.javaformat.doc.Comment#computeFlat()} applies the same rule to a comment that is
* written flat, for which {@link #rewrite} is not called.
Expand Down Expand Up @@ -231,8 +236,7 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
while (line.length() + column0 > options.maxLineLength()) {
int idx = options.maxLineLength() - column0;
// 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.
// such as U+00A0, which is there to keep the words on either side of it on one line.
while (idx >= prefix.length()
&& !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) {
idx--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ void addMissingSpacePutsABlankAfterTheSlashesWhateverTheirNumber() {

@Test
void addMissingSpaceLeavesSpacedCommentsAndIdeMarkersAlone() {
// A blank or a tab after the slashes is a space already; the IDE markers are recognised only without one.
// A blank, a tab or a Unicode space after the slashes is a space already; the IDE markers are recognised only
// without one.
var comments = List.of(
"// foo",
"// two spaces",
"//\tfoo",
"//\u00a0no-break space",
"///\u00a0no-break space",
"//\u3000ideographic space",
"//",
"///",
"//$NON-NLS-1$ //$NON-NLS-2$",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A Unicode space right after the slashes, such as a no-break space (U+00A0) or an ideographic space (U+3000),
# is the space the comment needs, so it does not get a regular one in front of it (#249).
----------
class T {
// non-breaking space after the prefix
/// non-breaking space after a markdown prefix
// ideographic space after the prefix
//foo
void m() {}
}
----------
class T {
// non-breaking space after the prefix
/// non-breaking space after a markdown prefix
// ideographic space after the prefix
// foo
void m() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# A line comment wrapped next to a Unicode space comes out of the first run as it does out of every later one.
# The line that ends at the break loses the no-break space (U+00A0) before it, and the line that starts at an
# ideographic space (U+3000) keeps it after the slashes, without a regular space in front of it (#249).
----------
class T {
// 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.
int rows;

// 設定ファイルが見つからないときは既定値で起動します。 既定値はこのクラスの定数にまとめてあります。 値を変えるときは、テストの期待値も合わせて更新してください。 古い形式のファイルは読み込み時に新しい形式へ変換されます。 変換に失敗したファイルはそのまま残し、エラーをログに出します。
int defaults;
}
----------
class T {
// 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.
int rows;

// 設定ファイルが見つからないときは既定値で起動します。 既定値はこのクラスの定数にまとめてあります。 値を変えるときは、テストの期待値も合わせて更新してください。 古い形式のファイルは読み込み時に新しい形式へ変換されます。
// 変換に失敗したファイルはそのまま残し、エラーをログに出します。
int defaults;
}
Loading