From 7840e55e4dd451f9df2455890f3613fe8b72bd82 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Fri, 20 Nov 2026 23:10:15 +0300 Subject: [PATCH] Keep a Unicode space right after the slashes of a line comment A no-break space (U+00A0) right after `//` counted as a missing space, so the comment got a regular one in front of it. The pattern for the missing space now has Pattern.UNICODE_CHARACTER_CLASS, so \s matches any Unicode space and such a comment stays as written, as in google/google-java-format#1530. Two wrapped comments now come out of the first run as they do out of the second: - A wrap at an ideographic space (U+3000) left a line that starts with `//` and that space, which the next run padded. - A wrap at the regular space after a no-break space left the line before it ending in the no-break space. String.trim() keeps it, and only the next run's trimTrailingFrom dropped it. indentLineComments now trims with CharMatcher.whitespace(), as upstream does. --- .../javaformat/java/JavaCommentsHelper.java | 18 +++++++++------- .../java/JavaCommentsHelperTest.java | 6 +++++- ...ntKeepsTheUnicodeSpaceAfterItsSlashes.case | 18 ++++++++++++++++ ...rappedBesideAUnicodeSpaceIsIdempotent.case | 21 +++++++++++++++++++ 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 open-java-format/src/test/resources/test-cases-java21/lineCommentKeepsTheUnicodeSpaceAfterItsSlashes.case create mode 100644 open-java-format/src/test/resources/test-cases-java21/lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case 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 1a75378db..cd4641069 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 @@ -102,16 +102,17 @@ private String preserveIndentation(List 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 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(); } @@ -167,7 +168,10 @@ 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. @@ -175,7 +179,8 @@ private static boolean isJBangDirective(String text) { 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. @@ -231,8 +236,7 @@ private List wrapLineComments(List 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--; diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/JavaCommentsHelperTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/JavaCommentsHelperTest.java index cf25955c4..afa9a2365 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/JavaCommentsHelperTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/JavaCommentsHelperTest.java @@ -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$", diff --git a/open-java-format/src/test/resources/test-cases-java21/lineCommentKeepsTheUnicodeSpaceAfterItsSlashes.case b/open-java-format/src/test/resources/test-cases-java21/lineCommentKeepsTheUnicodeSpaceAfterItsSlashes.case new file mode 100644 index 000000000..478a734c1 --- /dev/null +++ b/open-java-format/src/test/resources/test-cases-java21/lineCommentKeepsTheUnicodeSpaceAfterItsSlashes.case @@ -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() {} +} diff --git a/open-java-format/src/test/resources/test-cases-java21/lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case b/open-java-format/src/test/resources/test-cases-java21/lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case new file mode 100644 index 000000000..81d3f1f41 --- /dev/null +++ b/open-java-format/src/test/resources/test-cases-java21/lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case @@ -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; +}