Skip to content

Keep a Unicode space right after the slashes of a line comment - #251

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/keep-a-no-break-space-right-after-instead-of-pad
Nov 20, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/keep-a-no-break-space-right-after-instead-of-pad

Conversation

@abashev

@abashev abashev commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Closes #249. Ports google/google-java-format#1530; based on 21.X.X, and no other open pull request touches these files.

Of the upstream change's three parts, we had the first already: since #38 line comments wrap only on breaking whitespace, never on a no-break space. This takes the other two:

  • LINE_COMMENT_MISSING_SPACE_PREFIX gets Pattern.UNICODE_CHARACTER_CLASS. Its \s was ASCII-only, so a no-break space (U+00A0) right after // counted as a missing space and a regular space went in front of it. Now any Unicode space counts, and the comment stays as written. Comment.computeFlat() applies the same rule through addMissingSpace, so a trailing comment written flat behaves the same.
  • indentLineComments trims with CharMatcher.whitespace().trimFrom instead of String.trim().

What changes for users

A line comment that starts with // and U+00A0, or another Unicode space such as the ideographic space U+3000 common in Japanese comments, is left as it is. Before, it got // followed by that space. Code formatted before keeps that: the formatter does not take the regular space out again.

Two wrapped comments that changed again on a second run now settle on the first:

  • A comment wrapped at an ideographic space: the wrapped line starts with // and that space, and the second run padded it.
  • A comment wrapped at the regular space after a no-break space, as in .  Next pasted from HTML: the line before the break ended in the no-break space, which trim() keeps and only the second run's trimTrailingFrom dropped.

The JBang directive and shell-line patterns keep their ASCII \s.

Tests

  • lineCommentKeepsTheUnicodeSpaceAfterItsSlashes.case: U+00A0 after // and after ///, and U+3000 after //, are kept; //foo still becomes // foo.
  • lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case: the two wraps above.
  • JavaCommentsHelperTest.addMissingSpaceLeavesSpacedCommentsAndIdeMarkersAlone: three more comments that addMissingSpace leaves unchanged.

All three fail on 21.X.X. The wrap case fails its first run on the trailing no-break space, and with only the trim() change it fails its second run on the ideographic space. Upstream's updated FormatterTest case wraps at a no-break space in the middle of a comment, which ojf-issue-38-nbsp-line-comment has covered since #38, and starts a line with one, which the first new case covers.

With the change, JDK 21: ./gradlew check passes.

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.
@abashev
abashev merged commit e6c195f into 21.X.X Nov 20, 2026
16 checks passed
@abashev
abashev deleted the abashev/keep-a-no-break-space-right-after-instead-of-pad branch November 20, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep a no-break space right after // instead of padding it with a regular space

1 participant