Repository navigation
Keep a Unicode space right after the slashes of a line comment - #251
Merged
abashev merged 1 commit intoNov 20, 2026
Merged
Conversation
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
deleted the
abashev/keep-a-no-break-space-right-after-instead-of-pad
branch
November 20, 2026 20:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_PREFIXgetsPattern.UNICODE_CHARACTER_CLASS. Its\swas 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 throughaddMissingSpace, so a trailing comment written flat behaves the same.indentLineCommentstrims withCharMatcher.whitespace().trimFrominstead ofString.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:
//and that space, and the second run padded it.. Nextpasted from HTML: the line before the break ended in the no-break space, whichtrim()keeps and only the second run'strimTrailingFromdropped.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;//foostill becomes// foo.lineCommentWrappedBesideAUnicodeSpaceIsIdempotent.case: the two wraps above.JavaCommentsHelperTest.addMissingSpaceLeavesSpacedCommentsAndIdeMarkersAlone: three more comments thataddMissingSpaceleaves unchanged.All three fail on
21.X.X. The wrap case fails its first run on the trailing no-break space, and with only thetrim()change it fails its second run on the ideographic space. Upstream's updatedFormatterTestcase wraps at a no-break space in the middle of a comment, whichojf-issue-38-nbsp-line-commenthas covered since #38, and starts a line with one, which the first new case covers.With the change, JDK 21:
./gradlew checkpasses.